简体   繁体   English

调用按钮serveride的click事件

[英]Calling click event of a button serverside

How can I click or load the click() event of a button in codebehind? 如何在代码隐藏中单击或加载按钮的click()事件?
I already tried 我已经试过了

      btn.Click();

but this gives an error. 但这会给出错误。 I am using ASP.NET 我正在使用ASP.NET

I will assume that you have a button called Button1 and that you have double clicked it to create an event handler. 我假设你有一个名为Button1的按钮,你双击它来创建一个事件处理程序。

To simulate the button click in code, you simply call the event handler: 要模拟代码中的按钮单击,只需调用事件处理程序:

Button1_Click(object sender, EventArgs e). Button1_Click(对象发送者,EventArgs e)。

eg in your Page Load Event 例如在你的页面加载事件中

protected void Page_Load(object sender, EventArgs e)
{
    //This simulates the button click from within your code.
    Button1_Click(Button1, EventArgs.Empty);
}

protected void Button1_Click(object sender, EventArgs e)
{
    //Do some stuff in the button click event handler.
}

If you don't need the context provided by the sender and eventargs then you can just refactor to create a method (eg DoStuff()) and make your event handler just call that. 如果您不需要发送方和eventargs提供的上下文,那么您可以重构以创建方法(例如DoStuff())并使您的事件处理程序只调用它。 Then when you want to call the same functionality from elsewhere you can just call DoStuff(). 然后,当您想从其他地方调用相同的功能时,您可以调用DoStuff()。 It really depends on whether you want to actually simulate a click or not. 这实际上取决于您是否要实际模拟点击。 If you want to simulate a click then other methods are better. 如果要模拟单击,则其他方法更好。

Do you wish to call all the event handlers attached to the button, or just one? 您是否希望将所有事件处理程序都连接到按钮上,或只是一个?

If just one, call the handler: 如果只有一个,请调用处理程序:

 btn.btn_Click(btn, new EventArgs());

If all of them, trigger the event: 如果全部触发事件:

 var tmpEvent = btn.Click;
 if (tmpEvent != null)
      tmpEvent(btn, new EventArgs());

试试这个:

YourButton_Click(sender, e);

It's been a long time ago, but here is my solution 这是很久以前的事了,但这是我的解决方案

You can use: 您可以使用:

btn.PerformClick();

Edit try this: 编辑试试这个:

protected void Page_Init(object sender, EventArgs e)
{
      Button1.Click += new EventHandler(Button1_Click);
}

 void Button1_Click(object sender, EventArgs e)
{
}

in the .cs page 在.cs页面中

Just call the buttons click function. 只需调用按钮单击功能即可。 http://forums.asp.net/t/1178012.aspx http://forums.asp.net/t/1178012.aspx

Instead of trying to call the event just call the function that event uses. 而不是尝试调用事件只是调用事件使用的函数。

Example: btn.Click() calls btn_Click(object sender, EventArgs e) 示例:btn.Click()调用btn_Click(对象发送者,EventArgs e)

so you should call the function btn_Click(btn,new EventArgs()); 所以你应该调用函数btn_Click(btn,new EventArgs());

protected void Page_Load(object sender, EventArgs e)
{
   functionName();
}
protected void btn_Click(object sender, EventArgs e)
{
   functionName();
}

public void functionName()
{

//function code goes here, and call this function where ever you want

}

try this one....... 试试这个.......

 YourButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

这适合我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM