In. NET, when all the server controls are committed to the server, the __doPostBack function is called, so the flexibility to use this function is a great help for us.
For example, when we write the program often need to dynamically generate some control, the simplest way is through a string, such as String Strbutton = <input type = "button" id= "Button1", and then output to the page, But if we need this control to perform some of the functions of the server, it will be more difficult. Here we can use the function of borrowing __dopostback to do it. Next I take an example to illustrate how to invoke it.
Now that we're running on the server side, we can declare an invisible LinkButton control, which we usually want a control to be invisible, It is common to set the Visible property to false. But here we set the Text property of LinkButton to NULL, which is why this LinkButton is not visible (why not set the Visible property directly, I will explain below), Next we can write some server-side code in the LinkButton. Then it's how to invoke the functionality inside of LinkButton through our dynamically generated client controls, and we can do that with a JavaScript function.
function Excuteonserver () { // The first parameter is the ID number of the control you wish to submit to the server, and the second parameter is the event argument __dopostback (' Linkbuttonid ', ' ); }
Next we only need to write the onclick= "Javascript:excuteonserver () in the OnClick event of the dynamically generated button control, so that when we click on this dynamically generated client control, He will execute the code in the LinkButton.
This enables dynamically generated client controls to be submitted to the server-side functionality.
The last thing to say is why you want the LinkButton control to be invisible, Not done by the Visible property. Because when we set the Visible property to False, the browser does not put the control on the page when parsing, that is, the control does not exist, so when we call the __doPostBack function, The control will not be found.
Here is a commonly used function _dopostback, this function if it is an ASP. NET Render page is automatically generated this function, such as a control with the AutoPostBack property, and its property is true of the page, A DataGrid page with an edit column.
__doPostBack is to send control information to the server by __eventtarget,__eventargument two hidden controls, __eventtarget the name of the control to invoke, and if the control to invoke is a child control, use ' $ ' or ': ' Split parent control: child control, __eventargument is the argument when the event is invoked
The following shows how to invoke a background event:
1. New Project
2. Drag into a server-side Button1, a DropDownList1 and a client button
3. Set DropDownList1 's AutoPostBack property to True,button1 's visible as false
4. Double-click Button1, and Write down Response.Write ("Hello:") in the event;
5. Find the client button in the HTML of the page, write onclick= "__doPostBack (' Button1 ',") "
6. Compile, run, click button does not appear "Hello"
7. Check the source code and find that there are more lines
<Scriptlanguage= "JavaScript"><!--function__doPostBack (Eventtarget, eventargument) {vartheform;if(Window.navigator.appName.toLowerCase (). IndexOf ("Netscape") > -1) {theform=document.forms["Form1"]; }Else {theform=document. Form1; }Theform.__eventtarget.value=Eventtarget.split ("$"). Join (":");Theform.__eventargument.value=eventargument;theform.submit (); }</Script><inputtype= "hidden"value="" /><inputtype= "hidden"value="" />
Further learning JS: __dopostback function