PageLoadでは、フィードバックページを引き起こすコントロールを検出します.
Determining the Control that Caused a PostBack
Many times you might need to perform some action on ASP.NET postback based on the control that caused the postback to occur.Some scenaros for this might include a form with manregions,each having it's Curtor a button for the アクションis clicked.Another scenaro might be to set focus back to the control that caused the postback.
The re are two parts to the process of determining which control caused the postback.First,you access the_u uEVENTTARGET element of the form.If you've ever looked a the antomy of an AS.NET page(and if you haven't why are you reading this),you'll notice that a hidden input(is added to the form named__ku)EVENTTARGET.This hidden input is set to the control that was clicked in the_udopostback JavaScript function and the n the form is submitted.Looking at how the server controls are and HTML tags you can see that the_u u 0026 quot;doPostBack function is caled by the controls to cause a postback、passing the name of the control to the function.You can access this hidden input from yourcode- behind as it is submitted with the form andcan be found the Params or Formcollctes.Thisthe first part partto to to to to to thethethethethetheststststininininininininconconconconconconconconththththththththththththththththththththththththththththththththththththththththththththththththththththththththandandandandandandandandandandandandandandandandandandandit asneed.
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if (ctrlname != null && ctrlname != string.Empty)
{
return this.Page.FindControl(ctrlname);
}
This will work,but you'll soon find that something is missing.Although this will work for CheckBoxs,Drop DownLists,LinkButtons,etc,this dot work for But on controls.This.This where the second But.Buth.If you again take a look at how the server controls render as HTML,you'll see that the buttons dot call the_0026 quot;doPostBack Javascript function so the_uEVENTTARGET is never set.Instead,the Buttons render a s simple input type=「submit」tags.All the button does is cause the form to submit.That's it.However,you can still get it,just the different.infont. is what causes the form to submit,it is added to the items in the Form collection,along with all the other values from the submitted form.It is import to note,that other input type=「submit」tags on the form are not added to the Form collection unless it the one that caused the form to submit.If you were to look in the Form collection for anything a button then the that will be what castementEVENTTARGET、then if that is blank look for a button in the Form collection then you will find what caused the postback.The complette code follows:public static Control GetPostBackControl(Page page)
{
Control control = null;
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if (ctrlname != null && ctrlname != string.Empty)
{
control = page.FindControl(ctrlname);
}
else
{
foreach (string ctl in page.Request.Form)
{
Control c = page.FindControl(ctl);
if (c is System.Web.UI.WebControls.Button)
{
control = c;
break;
}
}
}
return control;
}
This method tars a parameter which is a reference to the Page,it then uses that to look for the control that caused the postback.You can easityuse use use this as follows:Control c = PageUtility.GetPostBackControl(this.Page);
if (c != null)
{
//...
}
You can now do some specific action based on the control that caused the postback or use an earlier post I made to set focus to the control.