如何以编程方式设置单击按钮
我在 ASP .Net 中使用 LinkButton(将其视为按钮)。我需要使用后面的 C# 代码以编程方式单击该按钮。我怎样才能实现这个目标..?
I'm using LinkButton(Consider it as Button) in ASP .Net. I need to click that button programmatically using C# code behind. How can I achieve this..?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要将事件处理程序用作方法,它们的唯一目的是处理事件。所有功能都应该封装在可以从事件处理程序内部以及您需要的任何地方调用的方法中。
然后,您也可以从页面的加载事件中调用此方法:
如何将 GridView 记录中的 ID 作为
CommandArgument
传递:Don't use event-handlers as methods, their only purpose is to handle events. All functionality should be encapsulated in methods that can be called from within the event-handler as well as from wherever you need it.
Then you can call this method for example from page's load event as well:
How to pass the ID from the GridView record as
CommandArgument
:好吧,我不太确定你想在这里实现什么目标。但第一种方法是简单地调用事件处理程序的方法代码。
但如果您有兴趣将响应发送回客户端并将按钮单击作为新请求,您可以尝试一下
ASPX:
C#:
但是存在 1 个问题。您可能会遇到事件验证错误。在这种情况下,您可能必须禁用事件验证,如下所示:
我不推荐它。还有其他方法可以克服它..但这是一个不同的谷歌搜索:)
Well, i'm not quite sure what you're trying to achieve here. but 1 method is to simply call the event handler method code.
But if you're interested in sending the response back to the client and taking the button click as a new request, you can try this out
ASPX:
C#:
However there's 1 problem. You might run in to an event validation error. in which case you might have to disable event validation as follows:
I wouldn't recommend it. there are other ways to overcome it..but that's a different google search :)
附注
从代码隐藏中调用事件处理程序是一种不好的做法。事件处理程序旨在触发某些客户端操作。
p.s.
invoking event handlers from code behind is a bad practice. event handlers are meant to fire on some client side actions.