I tried the suggestion in post Which Control Caused the Postback but it doesn't work in my case. The control name is prefixed with the UserControl name. Is there a way to grab a reference to a dynamically added control on a UserControl?
Asked
Active
Viewed 1,455 times
3 Answers
1
Try this:
if (IsPostBack)
{
var targetID = Request.Form["__EVENTTARGET"];
if (!String.IsNulOrEmpty(targetID)
{
var targetControl = this.Page.FindControl(targetID);
}
}

Faraday
- 2,904
- 3
- 23
- 46
-
It does not work for a control whose _EVENTTARGET is in this form: UserControlName$ControlID. – Metaphor Oct 08 '13 at 14:06
0
You could have your usercontrol to expose events. Your aspx page code behind should have a method which is called on one such event.
public delegate void EventFiredHandler(object sender);
public class MyUserControl: UserControl
{
public event EventFiredHandler EventFired;
//Let all your button clicks in usercontrol share this event sink
void Button1_Click(object sender, EventArgs e)
{
if(EventFired != null)
{
EventFired(sender);
}
}
}
In your aspx markup you write something like:
<uc1:MyUserControl runat="server"
EventFired="UCControl_EventFired"></uc1:MyUserControl>
In the aspx code behind:
protected void UCControl_EventFired(object sender)
{
//Obtaining reference of control in usercontrol
// which caused event
Button btn = (Button) sender;
}
0
None of answers posted till now are correct. This one is. I use these function in page load event to check for buttons clicks etc and don't use control events at all, because they happen too late for my taste and you have to bind data twice.
// Returns true if postback is from current control or one of child controls of current control.
// Returns false if postback is from any other controls (siblings or parents of the siblings) or if there is no postback.
public bool IsPostBackFromCurrentControl()
{
return IsPostBackFromControlOrItsChildren(this);
}
// Returns true if postback is from the control or one of child controls of the control.
// Returns false if postback is from any other controls (siblings, parents of the siblings or parent of current control) or if there is no postback.
public bool IsPostBackFromControlOrItsChildren(Control ctl)
{
if (IsPostBack)
{
string eventTarget = Request["__EVENTTARGET"];
if (eventTarget.StartsWith(ctl.UniqueID) || eventTarget.StartsWith(ctl.ClientID))
return true;
else
return false;
}
return false;
}
// Returns true if postback is from the control.
// Returns false if postback is from any other controls (siblings, parents of the siblings or parent or child of current control) or if there is no postback.
public bool IsPostBackFromControl(Control ctl)
{
if (IsPostBack)
{
string eventTarget = Request["__EVENTTARGET"];
if (eventTarget == ctl.UniqueID || eventTarget == ctl.ClientID)
return true;
else
return false;
}
return false;
}
Use this helper function, to see how to read raw postback data:
public void ShowAllPostBackData()
{
if (IsPostBack)
{
string[] keys = Request.Form.AllKeys;
Literal ctlAllPostbackData = new Literal();
ctlAllPostbackData.Text = "<div class='well well-lg' style='border:1px solid black;z-index:99999;position:absolute;'><h3>All postback data:</h3><br />";
for (int i = 0; i < keys.Length; i++)
{
ctlAllPostbackData.Text += "<b>" + keys[i] + "</b>: " + Request[keys[i]] + "<br />";
}
ctlAllPostbackData.Text += "</div>";
this.Controls.Add(ctlAllPostbackData);
}
}

Tone Škoda
- 1,463
- 16
- 20