2011-09-01 12 views
26

मैं दो बटन:किस नियंत्रण ने पोस्टबैक का कारण बनाया?

<asp:Button ID="Button1" runat="server" Text="Button" /> 
<asp:Button ID="Button2" runat="server" Text="Button" /> 

मैं Pageload पर कैसे निर्धारित कर सकते हैं जो इस दो में से एक की वजह से पोस्टबैक? क्या कोई छोटा समाधान है क्योंकि मुझे पता है कि केवल दो नियंत्रण हैं जो इस पोस्टबैक का कारण बन सकते हैं?

/// <summary> 
/// Retrieves the control that caused the postback. 
/// </summary> 
/// <param name="page"></param> 
/// <returns></returns> 
private Control GetControlThatCausedPostBack(Page page) 
{ 
    //initialize a control and set it to null 
    Control ctrl = null; 

    //get the event target name and find the control 
    string ctrlName = page.Request.Params.Get("__EVENTTARGET"); 
    if (!String.IsNullOrEmpty(ctrlName)) 
     ctrl = page.FindControl(ctrlName); 

    //return the control to the calling method 
    return ctrl; 
} 
+3

http://aspnetnova.blogspot.com/2009/04/find-post-back-co ntrol-in-aspnet-page-c.html –

उत्तर

43

आप नियंत्रण पोस्टबैक की वजह से प्राप्त करने के लिए इस विधि का उपयोग कर सकते हैं हालांकि यह किसी और को भी मदद करता है

<asp:Button ID="Button1" runat="server" Text="Button" 
    OnClientClick = "SetSource(this.id)" /> 

    <asp:ImageButton ID="ImageButton1" runat="server" 
    OnClientClick = "SetSource(this.id)" /> 

      <script type = "text/javascript"> 
      function SetSource(SourceID) 
      { 
    var hidSourceID = 
    document.getElementById("<%=hidSourceID.ClientID%>"); 
    hidSourceID.value = SourceID; 
    } 
    </script> 

on code behind you can get the ID of the function using : 
if (IsPostBack) 
{ 
    string CtrlID = string.Empty; 
    if (Request.Form[hidSourceID.UniqueID] != null && 
     Request.Form[hidSourceID.UniqueID] != string.Empty) 
    { 
    CtrlID = Request.Form[hidSourceID.UniqueID]; 
    } 
} 
+0

नियंत्रण नियंत्रण खाली होने के बाद से पुनरावर्तक के भीतर यह नियंत्रण नहीं ढूंढ रहा है। कोई विचार यह कैसे ढूंढें? पहले पृष्ठ में – djmj

+0

"पृष्ठ"। अनुरोध पृष्ठ होना चाहिए। – LarryBud

+0

@LarryBud: अच्छा पकड़, धन्यवाद। –

11

http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx

private string getPostBackControlName() 
    { 
     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; 
    } 
+0

[यह संस्करण] (http://stackoverflow.com/a/3509755/2415524) 2016-07-22 के रूप में अपडेट किया गया है। – mbomb007

0

यह नियंत्रण के नाम पर है कि pageload.This में पोस्टबैक वजह से खोजने के लिए मदद करता है मुझे मदद मिली:

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