Learn more about JS: _ dopostback Function

Source: Internet
Author: User

In. net, when all server controls are submitted to the server, the _ dopostback function is called. Therefore, flexible use of this function is of great help to us.

For exampleProgramThe simplest method is to use a string, such as string strbutton = <input type = "button" id = "button1">, output to the page, but it is difficult if we need this control to execute some server functions. here we can use the borrow _ dopostback function to complete the process. next, I will give an example to illustrate how to call it.

To run on the server side, we can declare an invisible linkbutton control. Generally, we want a control to be invisible and set the visible attribute to false. however, here we set the text attribute of the linkbutton to null to make the linkbutton invisible (why do we set it like this instead of directly setting the visible attribute? I will explain it below ), next, we can write some servers in the linkbutton.CodeAnd then how to use the client control we dynamically generate to call the functions in the linkbutton. We can use a javasFunctions

Function excuteonserver ()

{

// The first parameter is the ID of the control you want to submit to the server, and the second parameter is the event parameter.

_ Dopostback ('linkbuttonid ','');

}

Next, we only need to dynamically generate the onWrite on in the Click EventClick = "javasResponse: excuteonserver (); ", so that when we click this dynamically generated client control, it will execute the code in the linkbutton.

In this way, the dynamically generated client control is submitted to the server.

The last thing we want to talk about is why the linkbutton control is not visible through the visible attribute. when we set the visible attribute to false, the browser will not place this control on the page during parsing, that is, this control does not exist, therefore, when we call the _ dopostback function, we cannot find the control.

Here we will introduce a common function _ dopostback, if it is ASP. net render generates this function automatically. For example, a page with the autopostback property and its property is true contains a DataGrid page with the edit column.

_ Dopostback sends control information to the server through the two hidden controls _ eventtarget ,__ eventargument. __eventtarget is the name of the control to be called. If the control to be called is a child control, use '$' or ':' To separate the parent control: Child control, __eventargument, which is a parameter when an event is called.

The following shows how to call background events:

1. Create a project

2. Drag a server button1, A dropdownlist1, and a client button

3. Set the autopostback attribute of dropdownlist1 to true, while that of button1 to false.

4. Double-click button1 and write Response. Write ("Hello:") in the event :");

5. Find the client button in the HTML of the page and write it to onClick = "_ dopostback ('button1 ','')"

6. Compile, run, and click the button to see "hello"

7. ViewSource codeAnd the following lines are added.

<Script language = "javasOther ">

<! --

Function _ dopostback (eventtarget, eventargument ){

VaR theform;

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>

<Input type = "hidden" value = ""/>

<Input type = "hidden" value = ""/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.