Some special items in request. Form

Source: Internet
Author: User
Some special items in request. Form (convert)
Webjxcom prompt: How do I know which button is pressed during page_load?

How do I know which button is pressed during page_load?

One form runat = Server
There are many buttons in it
Bunton1, bunton2, bunton3...

How do I know which button is returned?

The problem is too vague. Are you using a server control or a client? The server directly writes its click event. The client uses Js.

On the server side, I want to know which function it will execute during page_load.
I know that the Click Event of a button can be written.

Are you new?
Page_load is executed during the temporary page folder.

Submit by ox all back with their own name...

The click event of the button is PostBack. page_load is only triggered when the page is loaded. I still don't understand what you want. Can I paste some code to see it?

Let's just say:
Protected void page_load (Object sender, eventargs E)
{
// Is it possible for me to obtain the method to be executed next? Is it btn1 or btn2, or is it impossible to get it?
}
Protected void btn1_click (Object sender, eventargs E)
{

}
Protected void btn2_click (Object sender, eventargs E)
{

}

Impossible
Because after the page_load event, execute the click event in the button.
Since page_load is the first, it is impossible to know what will happen in the future.

Impossible
Because after the page_load event, execute the click event in the button.
Since page_load is the first, it is impossible to know what will happen in the future.
So what mechanism does. Net itself use to know what will happen next?

So what mechanism does. Net itself use to know what will happen next?
Through eventargs e, you only need to press it to generate events, which can be captured at this time. When you pageload, the button is not pressed. Who knows? You think it is God, so your logic is confusing. First, let's take a look at the net event and delegate it.

There is a page. PostBack in pageload.

You can set the commandargs attribute of the button.
Override protected void oninit (eventargs E)
{
This. btn1.command + = new commandeventhandler (this. dobtnclick );
}
Public Virtual void dobtnclick (Object sender, commandeventargs E)
{
String BTN = E. commandargument. tostring ();
Switch (BTN ){}

}

On the 7th floor, I said:
Impossible
Because after the page_load event, execute the click event in the button.
Since page_load is the first, it is impossible to know what will happen in the future.

Then the landlord asked on the eighth floor:
So what mechanism does. Net itself use to know what will happen next?
I am also very depressed when I see this... But I don't know how to answer this question.

Suddenly, during debugging last night, I found some special items in request. Form: page. Request. Form ["_ eventtarget"]

In my opinion, it is not difficult to determine who is causing the current PostBack in page_load.
Analyze the generated HTML source code and find that the linkbutton is generated like this:

<A id = "linkbutton1" href = "javascript :__ dopostback ('linkbutton1','') "> button2 </a>

The dopostback function is as follows:

Function _ dopostback (eventtarget, eventargument ){
If (! Theform. onsubmit | (theform. onsubmit ()! = False )){
Theform. _ eventtarget. value = eventtarget;
Theform. _ eventargument. value = eventargument;
Theform. Submit ();
}
}

You can also find these in the form hidden mark:

<Input type = "hidden" name = "_ eventtarget" id = "_ eventtarget" value = ""/>
<Input type = "hidden" name = "_ eventargument" id = "_ eventargument" value = ""/>

That is to say, if the linkbutton ID is submitted to the server, you cannot determine the PostBack in page_load on the server.

However, when button and imagebutton are submitted, the value of page. Request. Form ["_ eventtarget"] is null.
However, since the ID of the linkbutton must be submitted to the client when it is submitted, the button should also be submitted.
The client code of the button is:

<Input type = "Submit" name = "button1" value = "button1" id = "button1"/>

That is to say, when a button is submitted, it is also an input tag. When a form is submitted, it must also be submitted.

As long as the string we submit in the request. Form loop, use this string findcontrol.
The following is the function used to search for controls in page_load.

Private string getpostbackcontrolname ()
{
Control control = NULL;
String ctrlname = page. Request. Params ["_ eventtarget"];
If (ctrlname! = NULL & ctrlname! = String. Empty)
{
Control = page. findcontrol (ctrlname );
}
Else
{
Control C;
Foreach (string CTL in page. Request. Form)
{
If (CTL. endswith (". X") | CTL. endswith (". Y "))
{
C = page. findcontrol (CTL. substring (0, CTL. Length-2 ));
}
Else
{
C = page. findcontrol (CTL );
}
If (C is system. Web. UI. webcontrols. Button |
C is system. Web. UI. webcontrols. imagebutton)
{
Control = C;
Break;
}
}
}
If (control! = NULL)
Return control. ID;
Else
Return string. empty;
}

OK. No problem for the time being. But what if the button is in the gridview, repeater, and other controls?

This has to be used again: page. Request. Params ["_ eventargument"]

In the figure above, click "select" in the second row in the gridview, and submit it to the page on the server. request. the value of Params ["_ eventargument"] is "select $1". Obviously, if you click the first row, the value should be "select $0"
So there is no problem finding the linkbutton in the gridview. Is there a difference between the button, imagebutton, and linkbutton?
The answer is no.
Because linkbutton, button, and imagebutton both use JavaScript _ dopostback
The HTML tags they generate may look like the following:

<A href = "javascript :__ dopostback ('gridview1', 'edit $ 0')"> edit </a>
<Input type = "button" value = "edit" onclick = "javascript :__ dopostback ('gridview1', 'edit $ 1')"/>
<Input type = "image" src = "" alt = "edit" onclick = "javascript :__ dopostback ('gridview1', 'edit $1 ') "Style =" border-width: 0px; "/>

So,Conclusion:

In page_load, you can determine who is causing the current PostBack, and the methods for searching them are given in the code above;
If the control is in another control, such as the gridview, repeater, and datalist, the request is required. params ["_ eventargument"] to help determine the control name. Otherwise. form ["_ eventtarget"] will obtain the ID of the data control such as gridview1 and repeater1, rather than the PostBack button.

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.