2012-07-04 12 views
5

में अपडेट पैनेल के अंदर कौन सा बटन क्लिक किया गया था, मैं एएसपी.NET UserControl के पेज लाइफसाइक्ल मुद्दे के आसपास अपना सिर प्राप्त करने का प्रयास कर रहा हूं। मेरे पास एक अपडेट पैनेल है जिसमें दो बटन हैं। अब, पेज_लोड ईवेंट में मुझे यह देखने के लिए एक चेक बनाने की आवश्यकता है कि दो बटनों में से किस पर क्लिक किया गया था।एएसपी.नेट निर्धारित करता है कि पेज लोड इवेंट

मुझे पता है कि मुझे इसके लिए क्लिक इवेंट का उपयोग करना चाहिए, लेकिन यह गतिशील रूप से जोड़े गए नियंत्रणों के साथ एक जटिल पृष्ठ चक्र का मामला है और इसी तरह, इसलिए, यह एक विकल्प नहीं है, दुर्भाग्यवश :-(

मैं Request.Form["__EVENTTARGET"] मूल्य पर जांच करने की कोशिश की है, लेकिन जब से बटन एक UpdatePanel के अंदर हैं मूल्य एक खाली स्ट्रिंग है (कम से कम मुझे लगता है कि क्यों यह रिक्त है कि है)

तो bascially, वहाँ किसी भी है Page_Load ईवेंट में अपडेट बटन में कौन सा बटन क्लिक किया गया था, यह देखने का तरीका?

अग्रिम धन्यवाद।

शुभकामनाएँ

,

बो

उत्तर

9

आप नियंत्रण की आईडी जो इस विधि द्वारा Page_Load घटना में पोस्टबैक की वजह से प्राप्त कर सकते हैं।

protected void Page_Load(object sender, EventArgs e) 
    { 
      Textbox1.Text = getPostBackControlID();  
    } 

    private string getPostBackControlID() 
    { 
     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 ctrlname = Page.Request.Params["__EVENTTARGET"]; 
     if (ctrlname != null && ctrlname != String.Empty) 
     { 
      control = Page.FindControl(ctrlname); 
     } 
     // if __EVENTTARGET is null, the control is a button type and we need to 
     // iterate over the form collection to find it 
     else 
     { 
      string ctrlStr = String.Empty; 
      Control c = null; 
      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")) 
       { 
        ctrlStr = ctl.Substring(0, ctl.Length - 2); 
        c = Page.FindControl(ctrlStr); 
       } 
       else 
       { 
        c = Page.FindControl(ctl); 
       } 
       if (c is System.Web.UI.WebControls.Button || 
         c is System.Web.UI.WebControls.ImageButton) 
       { 
        control = c; 
        break; 
       } 
      } 
     } 
     return control.ID; 
    } 
} 
+1

+1 ... अच्छा जवाब लेकिन केवल दुखद है कि उसने इसे स्वीकार नहीं किया है ... –

संबंधित मुद्दे