How to which control effect to post back? ASP.net C#
How to which control effect to post back?
When we developing web base application we want to know which control effect to post back. In here we can do simple approach to find out control which effect to post back.
This is the simple method which can be found out the control. In here we have to pass the page as the parameter. In this example I assume that there are few buttons on my web page and I want to know which button force to post back.
///Method to find out control force to post back.
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;
}
////
Method call
Control _ctlPostback =GetPostBackControl(this.Page);
By using this one you can do what ever thing according to your requirements.
In here i am use switch case to find out control.
if (_ctlPostback != null){
switch(_ctlPostback.ClientID.ToString())
{
#region
case "ctl00_plcContent__butAddRuletoProduct":///your button Client ID...
{
///YOUR CODE....
break;
}
}
}



0 comments:
Post a Comment