मुझे लगता है कि पोस्ट बैक कंट्रोल पाने के लिए पहले से ही कुछ बेहतरीन सलाह और विधियां सुझाती हैं। हालांकि मुझे पोस्ट बैक कंट्रोल आईडी पुनर्प्राप्त करने के लिए एक और वेब पेज (Mahesh blog) मिला।
मैं इसे यहाँ एक छोटे संशोधन के साथ पोस्ट करेंगे, यह एक विस्तार वर्ग की जाए। उम्मीद है कि यह इस तरह से अधिक उपयोगी है।
/// <summary>
/// Gets the ID of the post back control.
///
/// See: http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx
/// </summary>
/// <param name = "page">The page.</param>
/// <returns></returns>
public static string GetPostBackControlId(this Page page)
{
if (!page.IsPostBack)
return string.Empty;
Control control = null;
// first we will check the "__EVENTTARGET" because if post back made by the controls
// which used "_doPostBack" function also available in Request.Form collection.
string controlName = page.Request.Params["__EVENTTARGET"];
if (!String.IsNullOrEmpty(controlName))
{
control = page.FindControl(controlName);
}
else
{
// if __EVENTTARGET is null, the control is a button type and we need to
// iterate over the form collection to find it
// ReSharper disable TooWideLocalVariableScope
string controlId;
Control foundControl;
// ReSharper restore TooWideLocalVariableScope
foreach (string ctl in page.Request.Form)
{
// handle ImageButton they having an additional "quasi-property"
// in their Id which identifies mouse x and y coordinates
if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
{
controlId = ctl.Substring(0, ctl.Length - 2);
foundControl = page.FindControl(controlId);
}
else
{
foundControl = page.FindControl(ctl);
}
if (!(foundControl is IButtonControl)) continue;
control = foundControl;
break;
}
}
return control == null ? String.Empty : control.ID;
}
अद्यतन (2016/07/22): Button
और ImageButton
के लिए प्रकार की जांच IButtonControl
देखने के लिए तीसरे पक्ष के नियंत्रण से Postbacks मान्यता प्राप्त करने की अनुमति बदल दिया है।
स्रोत
2010-08-18 07:14:36
इसके लिए धन्यवाद ... पुन: उपयोग के लिए बिल्कुल सही स्निपेट। – trgraglia
मेरे पास एक उन्नत परिदृश्य है जहां यह काम नहीं कर रहा है - http://stackoverflow.com/questions/14486733/how-to-check-whether-postback-caused-by-a- गतिशील- लिंक- बटन – Lijo
क्या यह नहीं है अगर एक से अधिक बटन या छवि बटन हैं तो असफल हो जाएं? ऐसा लगता है कि यह केवल पहले को वापस लौटाएगा। –