Hi..
In the first Page_Load, I am activating a javascript function to make an ImageButton change between 2 images with an interval.
This works fine.
Now when pressing this imagebutton, the Page_Load event will fire the second time and all this is happening in an updatepanel which means a partial postback.
The question is how to execute the javascript function stopbk() in the Page_Load event in this scenario as I cant just put it in the "Onload". How can I make that work?
Code is below:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["SecondLoad"] != null)
{
//How to activate stopbk() ?
Label1.Text = "Second Load";
}
if (Session["SecondLoad"] == null)
{
Label1.Text = "First Load";
Session["SecondLoad"] = "dummy";
body1.Attributes.Add("Onload", "startbk()");
}
}
protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
{
//
}
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<script type="text/javascript">
var TimerProps = {
refTimer: null,
index: 0
}
function startTimer() {
if (TimerProps.index > 1) {
TimerProps.index = 0;
document.getElementById('<%= ImageButton6.ClientID %>').src = "http://www.abc.com/images/inboxKuvert.jpg";
TimerProps.index += 1;
}
else {
document.getElementById('<%= ImageButton6.ClientID %>').src = "http://www.abc.com/images/Transparent.gif";
TimerProps.index += 1;
}
}
function startbk() {
TimerProps.refTimer = setInterval("startTimer()", 550)
}
function stopbk() {
clearInterval(TimerProps.refTimer)
}
</script>
<asp:ImageButton ID="ImageButton6" runat="server" Height="12px" Width="16px"
ImageUrl="http://www.abc.com/images/inboxKuvert.jpg" onclick="ImageButton6_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text="-"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
Thank you,
I have tried to understand the example but are not 100% sure of how the parameters should be set.
My attempt is as below but are not sure if I have done it correctly, I think I miss something?
Example:
RegisterStartupScript( Control control, Type type, string key, string script, bool addScriptTags );
My attempt:
RegisterStartupScript(ImageButton6, GetType(), "???", "stopbk()", true);
It seems to work with the example below:
Thank you!
protected void Page_Load(object sender, EventArgs e)
{
if (Session["SecondLoad"] != null)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "stopbk", "stopbk();", true);
Label1.Text = "Second Load";
}
if (Session["SecondLoad"] == null)
{
Label1.Text = "First Load";
Session["SecondLoad"] = "dummy";
ScriptManager.RegisterStartupScript(this, this.GetType(), "startbk", "startbk();", true);
}
}
protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
{
//
}
<script type="text/javascript">
var TimerProps = {
refTimer: null,
index: 0
}
function startTimer() {
if (TimerProps.index > 1) {
TimerProps.index = 0;
document.getElementById('<%= ImageButton6.ClientID %>').src = "http://www.abc.com/images/inboxKuvert.jpg";
TimerProps.index += 1;
}
else {
document.getElementById('<%= ImageButton6.ClientID %>').src = "http://www.abc.com/images/Transparent.gif";
TimerProps.index += 1;
}
}
function startbk() {
TimerProps.refTimer = setInterval("startTimer()", 550)
}
function stopbk() {
clearInterval(TimerProps.refTimer)
}
</script>