我试图找出点击了哪个按钮,这段代码在 IE 中运行得很好,但如果我在 Chrome、Firefox 或 Safari 中它不会执行任何操作。在 Firefox 中使用 firebug 时,我查看了表单详细信息,它显示 EVENTTARGET 没有任何值,它只是空白。我怎样才能让它在 FF、Chrome 和 Safari 上工作?
Method:
Control postbackControlInstance = null;
string postbackControlName = page.Request.Params.Get("__EVENTTARGET");
if (postbackControlName != null && postbackControlName != string.Empty)
{
postbackControlInstance = page.FindControl(postbackControlName);
}
else
{
for (int i = 0; i < page.Request.Form.Keys.Count; i++)
{
postbackControlInstance = page.FindControl(page.Request.Form.Keys[i]);
if (postbackControlInstance is System.Web.UI.WebControls.Button)
{
return postbackControlInstance;
}
}
}
if (postbackControlInstance == null)
{
for (int i = 0; i < page.Request.Form.Count; i++)
{
if ((page.Request.Form.Keys[i].EndsWith(".x")) || (page.Request.Form.Keys[i].EndsWith(".y")))
{
postbackControlInstance = page.FindControl(page.Request.Form.Keys[i].Substring(0, page.Request.Form.Keys[i].Length - 2));
return postbackControlInstance;
}
}
}
return postbackControlInstance;
代码调用方法:
if (Page.IsPostBack)
{
try
{
Control cause = GetPostBackControl(Page);
string statecause = cause.ID;
if (statecause == "buttonName1")
{
search(statecause);
}
else if (statecause == "buttonNAME2")
{
resetfields();
}
}
catch { }
}
最好的方法是确定要覆盖哪个导致回发的控件protected
Page.RaisePostBackEvent
方法。 ASP.NET 基础结构使用此方法来通知导致回发的服务器控件它应该处理传入的回发事件:
public class MyPage : Page
{
protected override void RaisePostBackEvent(
IPostBackEventHandler sourceControl,
string eventArgument
)
{
// here is the control that caused the postback
var postBackControl = sourceControl;
base.RaisePostBackEvent(sourceControl, eventArgument);
}
}
您提供的代码应该适用于客户端时的场景__doPostBack
函数被渲染到页面(例如,如果您只使用一个按钮,例如<asp:Button runat="server" ID="btnSubmit" Text="submit" UseSubmitBehavior="true" />
它不会被渲染)仅。
如果即使在这种情况下__doPostBack
函数被渲染,但是__EVENTTARGET
参数为空意味着默认行为__doPostBack
在大多数情况下,自定义/不兼容的 JavaScript 代码会违反函数的要求。在这种情况下,即使 ASP.NET 基础结构也无法正确处理回发事件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)