siri Posted December 13, 2007 Share Posted December 13, 2007 Hi,How to Insert Javascript code for asp.net server controls like <asp:button> ,dropdown and so on. Link to comment Share on other sites More sharing options...
aspnetguy Posted December 13, 2007 Share Posted December 13, 2007 you can insert javascript in 2 ways.1. attach javascript to eventASPX <asp:Button id="Button1" runat="server" text="Button 1"/> Codebehind Button1.Attributes.Add("onclick","yourJavaScriptFunction()"); 2. Register a Client Script this.RegisterClientScriptBlock("scriptKey","<script type=\"text/javascript\">/*your javascript code here*/</script>"); Link to comment Share on other sites More sharing options...
jesh Posted December 13, 2007 Share Posted December 13, 2007 And a third way:ASPX ScriptLiteral1.Text = "<script type=\"text/javascript\">document.getElementById(\"Button1\").onclick=myclickhandler;</script>"; But aspnetguy's first solution is probably the best. Link to comment Share on other sites More sharing options...
aspnetguy Posted December 13, 2007 Share Posted December 13, 2007 Ok here is a fourth way <asp:Button id="Button1" runat="server" text="Button 1" OnClientClick="yourFunction()" /> Link to comment Share on other sites More sharing options...
siri Posted December 14, 2007 Author Share Posted December 14, 2007 Ok here is a fourth way<asp:Button id="Button1" runat="server" text="Button 1" OnClientClick="yourFunction()" /> Hi ASPNETGuy,The above code works but still it causes postback.How can PostBack be avoided ? Link to comment Share on other sites More sharing options...
aspnetguy Posted December 14, 2007 Share Posted December 14, 2007 if oyu want to avoid postback you have a couple options. You can not use a server control in the first place anf just use <input type="button" value="Button 1" id="Button1" onclick="yourFunction()"/> or you can tell the button not to postback ...JS codefunction youFunction() { if(something) { return true' } else { return false; }}...ASPX...<asp:Button id="Button1" runat="server" text="Button 1" OnClientClick="return yourFunction()" /> If your js function returns false it will not postback and the serverside function will not fire. Link to comment Share on other sites More sharing options...
jesh Posted December 14, 2007 Share Posted December 14, 2007 Also keep in mind that the Button control renders out as a submit button (<input type="submit">) which will submit your form by default.Another option would be to use a LinkButton or a HyperLink if you need the control to run at server and then style that link to look like a button. Link to comment Share on other sites More sharing options...
siri Posted December 15, 2007 Author Share Posted December 15, 2007 if oyu want to avoid postback you have a couple options. You can not use a server control in the first place anf just use<input type="button" value="Button 1" id="Button1" onclick="yourFunction()"/> or you can tell the button not to postback ...JS codefunction youFunction() { if(something) { return true' } else { return false; }}...ASPX...<asp:Button id="Button1" runat="server" text="Button 1" OnClientClick="return yourFunction()" /> If your js function returns false it will not postback and the serverside function will not fire. Hi aspnet guy,I have used the following code:<asp:Button id="Button2" runat="server" text="Button 1" OnClientClick="demo()" />Here is my javascript ocde:<script type = "text/javascript"> function demo() { return window.confirm("Are you sure"); } </script>When I click on cancel even then it postbacks to the server . how to avoid it ? Link to comment Share on other sites More sharing options...
aspnetguy Posted December 17, 2007 Share Posted December 17, 2007 use this <asp:Button id="Button2" runat="server" text="Button 1" OnClientClick="return demo()" /> Link to comment Share on other sites More sharing options...
siri Posted December 17, 2007 Author Share Posted December 17, 2007 Hi aspnet guy,I have used the following code:<asp:Button id="Button2" runat="server" text="Button 1" OnClientClick="demo()" />Here is my javascript ocde:<script type = "text/javascript"> function demo() { return window.confirm("Are you sure"); } </script>When I click on cancel even then it postbacks to the server . how to avoid it ?Thanks for the code. It's working Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.