Any clues why the button click event doesn’t insert script succesfully?
Here is the really straightforward code.
C#:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e){
ClientScript.RegisterStartupScript(Page.GetType(), "scriptalert", "<script type=\"text/javascript\">alert('testLOAD');</script>;", false);
btnAdd.Click += new EventHandler(btnAdd_Clicked);
}
protected void btnAdd_Clicked(Object sender,System.EventArgs e){
ClientScript.RegisterStartupScript(Page.GetType(), "scriptfunc", "<script type=\"text/javascript\">func();</script>;", false);
}
}
HTML:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
alert("testSCRIPT");
function func() {
alert("testFUNC");
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ToolkitScriptManager1" runat="server" />
<div id="View" style="height: 50%; width:20%; ">
blahblahblah
</div>
<div id="Addd" style="height: 50%; width:100%;">
<asp:UpdatePanel ID="UpdateAdd" runat="server">
<ContentTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Content>
On page load I get two alerts firing, namely “testLOAD” and “testSCRIPT”, but when I press the button nothing happens (there is supposed to be a third alert)… Checked in debugger, the event is called and runs, but when I view page source nothing was inserted.
I have a suspicion that the problem might lie elsewhere, since copy-pasting this into a new page without a master page (i.e. without ASP:Content tags) works. I haven’t had any problems with other script insertions though, so why this one?
I found what was wrong ^.^
There was a stray semicolon inside the script i was inserting, which made it break :/
Such a silly error caused so much frustration… sorry guys!