please check this example:
<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Page_Load(object sender, EventArgs e) { // This will drop the __doPostBack function on page. Page.ClientScript.GetPostBackEventReference(this, String.Empty); if (Page.IsPostBack) { lblTarget.Text = Request.Form["__EVENTTARGET"]; lblArgument.Text = Request.Form["__EVENTARGUMENT"]; } }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <atlas:ScriptManager ID="sm" runat="server"></atlas:ScriptManager> <div> <input type="button" id="myButton" value="ClickMe" /> <br /> <span>Postback target: </span> <asp:Label ID="lblTarget" runat="server"></asp:Label> <br /> <span>Posback argument: </span> <asp:Label ID="lblArgument" runat="server"></asp:Label> </div> </form> <script type="text/xml-script"> <page> <components> <application load="application_load" /> </components> </page> </script> <script type="text/javascript"> function application_load() { var myButton = new Sys.UI.Button($('myButton')); myButton.set_id('myButton'); myButton.click.add(Function.createDelegate(this, clickHandler)); myButton.initialize(); function clickHandler(sender, e) { var postBack = new Sys.WebForms.PostBackAction(); postBack.set_target(sender.get_id()); postBack.set_eventArgument('myArgument'); postBack.performAction(); } } </script></body></html>
If you need only to cause a post back use this line in your javascript code:
__doPostBack();
If you need data from your client script code then use this:
__doPostBack('X','Y');
X --> This is some name you make up that allows you to identify what caused the post back on the server-side code.
Y--> This is the data you are passing back from your javascript. If you need more than one value passed back just separate the values with a comma or something. ie. '42,53' Then you can separate the string in your server side code.
The Server Side of things.
If you are only causing a post back then you don't need to worry about any server-side code. If you did pass data back put the following code in your Page_Load event and use the If statement to see what fired the post back since other controls will also use the javascript post back.
If Request.Form("__EVENTTARGET") = X Then
dim s as String = Request.Form("__EVENTARGUMENT")
'do something with your data
End IF
Hope this helps.
hello.
btw, x is normally used for sending the client id of the server side control that started the postback and y is used to send info associated with that action. for instance, if you have a button which has id bt, setting x to bt will automatically invoke a previouslly configured method on the server which was configured to handle the click event of that button.
No comments:
Post a Comment