2013-08-18 38 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) 
相关问题