在回发时,如何检查哪个控件导致 Page_Init 事件中的回发
在回发时,如何检查哪个控件导致 Page_Init 事件中的回发。
protected void Page_Init(object sender, EventArgs e)
{
//need to check here which control cause postback?
}
谢谢
On postback, how can I check which control cause postback in Page_Init event.
protected void Page_Init(object sender, EventArgs e)
{
//need to check here which control cause postback?
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我看到已经有一些关于如何获得回发控制的很好的建议和方法。但是我找到了另一个网页(Mahesh blog),其中包含一种方法检索回发控件 ID。
我将把它发布在这里并进行一些修改,包括使其成为一个扩展类。希望它以这种方式更有用。
更新 (2016-07-22): 对
Button
和ImageButton
的类型检查更改为查找IButtonControl
以允许来自第三方控件的回发被识别。I see that there is already some great advice and methods suggest for how to get the post back control. However I found another web page (Mahesh blog) with a method to retrieve post back control ID.
I will post it here with a little modification, including making it an extension class. Hopefully it is more useful in that way.
Update (2016-07-22): Type check for
Button
andImageButton
changed to look forIButtonControl
to allow postbacks from third party controls to be recognized.这里有一些代码可能适合您(取自Ryan Farley 的博客< /a>)
Here's some code that might do the trick for you (taken from Ryan Farley's blog)
如果您需要检查哪个控件导致了回发,那么您可以直接将
["__EVENTTARGET"]
与您感兴趣的控件进行比较:这假设您只是要比较以下结果无论如何,任何
GetPostBackControl(...)
扩展方法。它可能无法处理所有情况,但如果它有效,那就更简单了。另外,您不会在页面中搜寻您一开始不关心的控件。If you need to check which control caused the postback, then you could just directly compare
["__EVENTTARGET"]
to the control you are interested in:This assumes you're just going to be comparing the result from any
GetPostBackControl(...)
extension method anyway. It may not handle EVERY situation, but if it works it is simpler. Plus, you won't scour the page looking for a control you didn't care about to begin with.直接在表单参数中或
编辑:检查控件是否导致回发(手动):
您还可以迭代所有控件并使用上述代码检查其中一个控件是否导致回发。
Either directly in form parameters or
Edit: To check if a control caused a postback (manually):
You could also iterate through all the controls and check if one of them caused a postBack using the above code.
假设它是一个服务器控件,您可以使用
Request["ButtonName"]
来查看是否单击了特定按钮:
if (Request["ButtonName"] != null)
Assuming it's a server control, you can use
Request["ButtonName"]
To see if a specific button was clicked:
if (Request["ButtonName"] != null)
除了之前的答案之外,要使用
Request.Params["__EVENTTARGET"]
您必须设置选项:An addition to previous answers, to use
Request.Params["__EVENTTARGET"]
you have to set the option:要获取控件的确切名称,请使用:
To get exact name of control, use: