EVENTTARGET问题determing发件人 [英] EVENTTARGET Problem determing sender
问题描述
我试图找出哪个按钮被点击,这code工作在IE浏览器就好了,但如果我的Chrome,Firefox或Safari它没有做任何事情。当在Firefox使用萤火虫,我看了看表说明,它表明EVENTTARGET没有价值的只是空白。我怎样才能得到这个在FF工作,铬,和Safari?
方法:
控制postbackControlInstance = NULL; 字符串postbackControlName = page.Request.Params.Get(__ EVENTTARGET);
如果(postbackControlName = NULL&放大器;!&安培;!postbackControlName =的String.Empty)
{
postbackControlInstance = page.FindControl(postbackControlName);
}
其他
{
的for(int i = 0; I< page.Request.Form.Keys.Count;我++)
{
postbackControlInstance = page.FindControl(page.Request.Form.Keys [I]);
如果(postbackControlInstance是System.Web.UI.WebControls.Button)
{
返回postbackControlInstance;
}
}
}
如果(postbackControlInstance == NULL)
{
的for(int i = 0; I< page.Request.Form.Count;我++)
{
如果((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 [Ⅰ]。长度 - 2));
返回postbackControlInstance;
}
}
}
返回postbackControlInstance;
code调用方式:
如果(Page.IsPostBack)
{
尝试
{
控制病因= GetPostBackControl(页);
字符串statecause = cause.ID;
如果(statecause ==buttonName1)
{
搜索(statecause);
}
否则,如果(statecause ==buttonNAME2)
{
重新设置领域();
}
}
赶上{}
}
最好的办法是确定什么原因引起的控制回发是覆盖保护
Page.RaisePostBackEvent
方法。此方法使用ASP.NET基础建设,以通知引起它应处理传入的回发事件回发的服务器控件:
公共类我的页面:页
{
保护覆盖无效RaisePostBackEvent(
IPostBackEventHandler sourceControl,
串eventArgument
)
{
//这里是导致回发的控制
VAR postBackControl = sourceControl; base.RaisePostBackEvent(sourceControl,eventArgument);
}
}
您提供的应该scenarious工作,当客户端 __ doPostBack
功能呈现给网页(code例如,如果你使用像只有一个按钮< ASP:按钮=服务器ID =btnSubmit按钮的文本=提交UseSubmitBehavior =真/>
不会被渲染)仅<。 / p>
如果即使在当 __ doPostBack
功能呈现,但案件 __ EVENTTARGET
参数为空则意味着在 __ doPostBack
函数的默认行为是通过自定义的/不兼容的javascript code。在大多数情况下侵犯。在这种情况下,即使ASP.NET的基础设施将无法妥善处理回发事件。
I am trying to figure out what button was clicked, this code works just fine in IE, but if I Chrome, Firefox, or Safari it doesnt do anything. When using firebug in firefox, I looked at the Form Details, and it shows that EVENTTARGET has no value its just blank. How can I get this to work on FF, Chrome, and 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;
Code Calling Method:
if (Page.IsPostBack)
{
try
{
Control cause = GetPostBackControl(Page);
string statecause = cause.ID;
if (statecause == "buttonName1")
{
search(statecause);
}
else if (statecause == "buttonNAME2")
{
resetfields();
}
}
catch { }
}
The best way is to determine what control that caused the postback is to override protected
Page.RaisePostBackEvent
method. This method is used by ASP.NET infrastucture to notify the server control that caused the postback that it should handle an incoming postback event:
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);
}
}
The code you provided should work for scenarious when the client-side __doPostBack
function is rendered to the page (e.g. if you use the only one button like <asp:Button runat="server" ID="btnSubmit" Text="submit" UseSubmitBehavior="true" />
it won't be rendered) only.
If even in the case when the __doPostBack
function is rendered, but __EVENTTARGET
parameter is empty it means that the default behaviour of the __doPostBack
function is violated by custom/incompatible javascript code in the most cases. In this case even ASP.NET infrastructure will be unable to handle post back events properly.
这篇关于EVENTTARGET问题determing发件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!