47
protected void timer1_Tick(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in rpChat.Items)
        {
            TextBox txt = item.FindControl("txtChatMessage") as TextBox;
            if (txt != null)
            {
                message[i] = txt.Text;
                i--;
            }
        }
        lblStatusChat.Text = "";
        RepeaterBind();
        string javaScript = "<script language=JavaScript>\n" + "alert('Button1_Click client-side');\n" + "</script>";

        Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", javaScript);
    }

timer_click trigggers and update panel. And the alert message doesnt show up on timer_tick event

Cœur
  • 37,241
  • 25
  • 195
  • 267
sly_Chandan
  • 3,437
  • 12
  • 54
  • 83

6 Answers6

117

When you use an UpdatePanel, then you can not call JavaScript using ClientScript as you have tried to. You have to use ScriptManager.RegisterStartupScript instead.

So change your

Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", javaScript);

to

ScriptManager.RegisterStartupScript(updatePanelId,updatePanelId.GetType(), "alert", javaScript, true);
TylerH
  • 20,799
  • 66
  • 75
  • 101
Akash KC
  • 16,057
  • 6
  • 39
  • 59
10

You need to user ScriptManager class because you are register script when doing postback and using updatepanel

MSDN: ScriptManager.RegisterStartupScript

ScriptManager.RegisterStartupScript method used to add client script to a page when the control is wrapped inside an UpdatePanel.

ASPX page

<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Label ID="lblDisplayDate" runat="server" Text="Label"></asp:Label>
         <asp:Button ID="btnPostback" runat="server" onclick="btnPostback_Click" 
        Text="ClickMe" />
    </ContentTemplate>
</asp:UpdatePanel>
</div>

CodeBehind Register StartUp Script

protected void btnPostback_Click(object sender, EventArgs e)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append(@"<script language='javascript'>");
    sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
    sb.Append(@"lbl.style.color='red';");
    sb.Append(@"</script>");

    ScriptManager.RegisterStartupScript(btnPostback,this.GetType(), "JSCR", sb.ToString(),false);

}

Detail : Add JavaScript programmatically using RegisterStartupScript during an Asynchronous postback

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
9

In my case ScriptManager.RegisterStartupScript didn't work too.

Not work:

ScriptManager.RegisterStartupScript(Me, 
  Me.GetType(), 
  String.Format("Data{0}", Me.ID), 
  "<script>alert(111);</script>", 
  False)

Work:

ScriptManager.RegisterStartupScript(Me.Page, 
  Me.GetType(), 
  String.Format("Data{0}", Me.ID), 
  "<script>alert(111);</script>", 
  False)

Me in the example is my custom control inherited from System.Web.UI.WebControls.WebParts.WebPart

Afnan Ahmad
  • 2,492
  • 4
  • 24
  • 44
feeeper
  • 2,865
  • 4
  • 28
  • 42
2

It must be un little bit later but I found the soluce here http://forums.asp.net/p/1117430/5483679.aspx/1

You have to use System.Web.UI.ScriptManager.RegisterClientScriptBlock instead of Page.ClientScript.RegisterStartupScript

Kaly
  • 47
  • 1
  • 8
0

In case Page seems inaccessible from the class you are in.

Page page = HttpContext.Current.CurrentHandler as Page;
        

ScriptManager.RegisterStartupScript(page,page.GetType(), Guid.NewGuid().ToString(), yourScriptVariable, true);
0

None of the above worked for me. This is worked.

ScriptManager.RegisterClientScriptBlock(TryCast(sender, Control), Me.[GetType](), "alert", message, True)

I found it at

https://www.aspsnippets.com/Articles/ClientScriptRegisterClientScriptBlock-and-ClientScriptRegisterStartupScript-not-working-in-AJAX-UpdatePanel-in-ASPNet.aspx

Ruby Kousinovali
  • 337
  • 2
  • 20