Welcome to AspAdvice Sign in | Join | Help

manik.net

alles über c# / microsoft asp.net und viel, viel mehr
Getting the control that caused the postback / Das Control ermitteln, welches den Postback ausgelöst hat

protected Control GetPostBackControl()
{
System.Collections.Specialized.
NameValueCollection postCollection = System.Web.HttpContext.Current.Request.Form;
string controlID = postCollection.Get("__EVENTTARGET");

if (string.IsNullOrEmpty(controlID) == false)
{
return Page.FindControl(controlID);
}

foreach (string key in postCollection.Keys)
{
Control control = Page.FindControl(key);

if (control is Button)
{
return control;
}
}
return null;
}

Control GetPostBackControl()
{
System.Collections.Specialized.
NameValueCollection postCollection = System.Web.HttpContext.Current.Request.Form;
string controlID = postCollection.Get("__EVENTTARGET");

if (string.IsNullOrEmpty(controlID) == false)
{
return Page.FindControl(controlID);
}

foreach (string key in postCollection.Keys)
{
Control control = Page.FindControl(key);

if (control is Button)
{
return control;
}
}
return null;
}

if (string.IsNullOrEmpty(controlID) == false)
{
return Page.FindControl(controlID);
}

foreach (string key in postCollection.Keys)
{
Control control = Page.FindControl(key);

if (control is Button)
{
return control;
}
}
return null;
}

foreach (string key in postCollection.Keys)
{
Control control = Page.FindControl(key);

if (control is Button)
{
return control;
}
}
return null;
}

if (control is Button)
{
return control;
}
}
return null;
}

Der untere Teil ist notwendig da Button-Controls nicht die Javascript Funktion "__doPostBack" auslösen, sondern das Form im klassischen ASP(HTML) Stil per Form Submit zurück an den Server schicken. Somit sind Buttons nicht als __EVENTTARGET vermerkt. Etwas schlecht gelöst im ASP.net Framework meiner Meinung nach...
 

Posted: Tuesday, October 21, 2008 1:10 PM by manik
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Enter the code you see below

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS