my code :
aspx:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" onclick="btnCheck_Click" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
.CS file:
protected void btnCheck_Click(object sender, EventArgs e)
{
string script = "<script>alert('test');</script>";
Response.Write(script);
this.Label1.Text = "error";
}
but it's throw javascript exception when i run it .how to write javascript code in .cs file?
Well, your best option is "don't". But if you must, then use the ScriptManager functions for registering scripts and script blocks.They're in the documentation.
here is yours as a working example
the .aspx. Have a look at buttons client eventOnClientClick
<asp:ScriptManager ID="sm1" runat="server" /><asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Test()" /></ContentTemplate></asp:UpdatePanel>
the codebehind
protected override void OnInit(EventArgs e){if (sm1.SupportsPartialRendering){sm1.EnablePartialRendering =true;}string script ="function Test() {alert('Hello');}";ScriptManager.RegisterClientScriptBlock(this,typeof(Page),"Test", script,true);base.OnInit(e);}protected void Page_Load(object sender, EventArgs e){}
No comments:
Post a Comment