Jump to content

Insert ClientScript for Asp.net server Controls


siri

Recommended Posts

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...