回发时,如何检查哪些控件会导致在Page_Init事件中回发。在回发上,如何检查哪些控件会导致在Page_Init事件中回发
protected void Page_Init(object sender, EventArgs e)
{
//need to check here which control cause postback?
}
感谢
回发时,如何检查哪些控件会导致在Page_Init事件中回发。在回发上,如何检查哪些控件会导致在Page_Init事件中回发
protected void Page_Init(object sender, EventArgs e)
{
//need to check here which control cause postback?
}
感谢
我看到已经有一些很棒的建议和方法建议如何获得回发控制。然而,我发现另一个网页(Mahesh blog)与检索回发控制ID的方法。
我会在这里稍微修改一下,包括使其成为扩展类。希望这种方式更有用。
/// <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年7月22日):为Button
和ImageButton
类型检查改为寻找IButtonControl
允许来自第三方控件回传被认可。
直接在表单参数或
string controlName = this.Request.Params.Get("__EVENTTARGET");
编辑:要检查控制造成回发(手动):
// input Image with name="imageName"
if (this.Request["imageName"+".x"] != null) ...;//caused postBack
// Other input with name="name"
if (this.Request["name"] != null) ...;//caused postBack
你可以也遍历所有的控件,并检查是否有一个m使用上面的代码导致了postBack。
我已经检查,其不为我工作。 – 2010-07-04 17:35:39
那么,你必须知道所有可能导致回发的控件,并检查参数集合('Request [controlName]')中是否有值。 – 2010-07-04 18:11:21
@JaroslavJandek谢谢你的回答。我有一个需要在页面OnLoad事件中检测按钮点击的实例,但是由于某种原因,Request [“__ EVENTTARGET”]是空的(即使它不是与其他控件导致回发)。 UniqueID]完美无缺+1 :) – 2012-07-13 14:01:49
假设它是一个服务器控件,您可以使用Request["ButtonName"]
要查看是否被点击一个特定的按钮:if (Request["ButtonName"] != null)
所有异步回发请求[name]为空 – 2013-01-18 09:50:57
下面是一些代码,可能会为你做的伎俩(从Ryan Farley's blog拍摄)
public static Control GetPostBackControl(Page page)
{
Control control = null;
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if (ctrlname != null && ctrlname != string.Empty)
{
control = page.FindControl(ctrlname);
}
else
{
foreach (string ctl in page.Request.Form)
{
Control c = page.FindControl(ctl);
if (c is System.Web.UI.WebControls.Button)
{
control = c;
break;
}
}
}
return control;
}
如果您需要检查其控制造成回传,那么你可以只直接比较["__EVENTTARGET"]
来控制你有兴趣:
if (specialControl.UniqueID == Page.Request.Params["__EVENTTARGET"])
{
/*do special stuff*/
}
这是假设你只是被从比较的结果无论如何,任何GetPostBackControl(...)
扩展方法。它可能无法处理所有情况,但如果它起作用,则更简单。另外,你不会在页面上寻找你不关心的控件。
这是一个很好和简单的伎俩。帮助我的情况,当时只是想处理一些非常具体的控制回发。 – Marcel 2018-01-25 14:32:52
if (Request.Params["__EVENTTARGET"] != null)
{
if (Request.Params["__EVENTTARGET"].ToString().Contains("myControlID"))
{
DoWhateverYouWant();
}
}
的除了以前的答案,使用Request.Params["__EVENTTARGET"]
你要设置的选项:
buttonName.UseSubmitBehavior = false;
为了得到控制的确切名称,用途:
string controlName = Page.FindControl(Page.Request.Params["__EVENTTARGET"]).ID;
此代码不起作用.... – 2015-04-08 10:07:50
感谢您的支持......完美的重复使用代码段。 – trgraglia 2011-12-22 08:22:59
我有一个先进的方案,它不工作 - http://stackoverflow.com/questions/14486733/how-to-check-whether-postback-caused-by-a-dynamic-link-button – Lijo 2013-01-23 18:47:29
不是这个吗?如果有多个按钮或图像按钮会失败?看起来它只会返回找到的第一个。 – 2014-05-02 21:03:46