2013-08-18 34 views
2

我有這樣的aspx:確定哪個控件導致回發

<asp:ImageButton ID="check" runat="server" ImageUrl="../img/process.png" OnClick="check_Click" CausesValidation="false" UseSubmitBehavior="false"/> 
現在 Page_Load

我想確定是PostBack造成check或根本沒有,所以我跟着this問題的方法與此代碼:

if(FindControl(Page.Request.Params.Get("__EVENTTARGET"))!=check)//if not caused by "check" 
    //do something 

但是Page.Request.Params.Get("__EVENTTARGET")是空的(我正在使用我的ImageButtonUpdatePanel

我怎樣才能達到我的目標?

+0

您可以試試,看看它在IE開發者工具。它會告訴你哪個請求發送了404或任何錯誤代碼。造成這個問題的原因。它還將幫助您瞭解哪個請求爲您提供回覆。時間流逝!在IE中按F12。 –

+0

不好意思;現在我很忙;我會在接下來的幾個小時裏查看答案 –

+0

這個答案是更短的:) http://stackoverflow.com/questions/7269271/which-control-caused-the-postback –

回答

2
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
     return; 
    Control control = null; 
    string controlName = Page.Request.Params["__EVENTTARGET"]; 
    if (!String.IsNullOrEmpty(controlName)) 
    { 
     control = Page.FindControl(controlName); 
    } 
    else 
    { 
     string controlId; 
     Control foundControl; 
     foreach (string ctl in Page.Request.Form) 
     { 
      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 Button || foundControl is ImageButton)) continue; 
      control = foundControl; 
      break; 
     } 
    } 
    Label1.Text = control.ID; // label1 must be in UpdatePanel 
} 
1

試試這個:

Control ctrl = null; 

string target = Page.Request.Params.Get("__EVENTTARGET"); 
if (!String.IsNullOrEmpty(target)) 
    ctrl = page.FindControl(target); 

if(ctrl == check){ 
    //check is the control that caused postback 
} 

**更新**

好吧,原來ImageButtons不同的作用有點。使用此爲您的標記:

<asp:ImageButton onClientClick="setTarget(this.id)" ID="check" runat="server" ImageUrl="../img/process.png" OnClick="check_Click" CausesValidation="false" UseSubmitBehavior="false"/> 

<asp:HiddenField ID="targetId" runat="server" /> 

現在創建一個JavaScript函數,將填充我們的隱藏字段的字段發起回發的ID:

function SetSource(id) 
{ 
    var targetId= 
    document.getElementById("<%=targetId.ClientID%>"); 
    targetId.value = id; 
} 

最後,我們檢查它在我們的回傳:

Control ctrl = null; 

if (Request.Form[targetId.UniqueID] != null && 
    Request.Form[targetId.UniqueID] != string.Empty) 
{ 
    ctrl = Page.FindControl(Request.Form[targetId.UniqueID]); 
} 

if(ctrl == check){ 
//check is the control that caused postback 
} 

REF:http://www.aspsnippets.com/Articles/How-to-find-the-control-that-caused-PostBack-in-ASP.Net.aspx

+0

現在我用這個,不能用'ImageButton' –

+0

目標爲空? –

+0

什麼是目標?如果你的意思是'ctrl',那麼它是'null'。 –

1

由於這是一個更新面板,然後嘗試通過ScriptManager所獲得的價值,就像這樣:

protected void Page_Load(object sender, EventArgs e) 
{ 
    var updatePanelControlIdThatCausedPostBack = String.Empty; 
    var scriptManager = ScriptManager.GetCurrent(Page); 

    if (scriptManager != null) 
    { 
     var smUniqueId = scriptManager.UniqueID; 
     var smFieldValue = Request.Form[smUniqueId]; 

     if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains("|")) 
     { 
      updatePanelControlIdThatCausedPostBack = smFieldValue.Split('|')[1]; 
     } 
    } 

    if (!String.IsNullOrEmpty(updatePanelControlIdThatCausedPostBack)) 
    { 
     // Do something with control ID value that caused UpdatePanel postback here 
    } 
} 
0

如果控制導致PostBackID將不null請求集合中,Form.Request[controlID]

Image的情況下,請求集合中有兩個項目,一個用於x座標,另一個用於y座標,用於鼠標單擊圖像中的確切位置。

所以做:

if(Form.Request["Img1.x"] != null) 
相關問題