调用按钮而不实际单击按钮C# [英] Calling a button without actually clicking the button C#

查看:80
本文介绍了调用按钮而不实际单击按钮C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,



我的aspx文件中有一个名为Button2的按钮。 .cs文件有一个在单击按钮时调用的函数。



Greetings,

I have a button in my aspx file called Button2. The .cs file has a function which is called when the button is clicked.

protected void Button2_Click(object sender, EventArgs e)
   {
       Response.Redirect("Login.aspx");
   }





如何在不实际点击按钮的情况下调用此功能?

任何人都可以帮我解决这个问题吗?

提前致谢。



我尝试了什么:





How can I call this function without actually clicking the button?
Can Anyone Help me out in figuring this out?
Thanks in advance.

What I have tried:

private void comRFID_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    SomeSub();

    if ((strbuffer[11].ToString() == strCard1[11]))
    {
        UserID = "870104-07-5448";
        Send_Beep();
        System.Diagnostics.Debug.WriteLine(("UserId: " + UserID));
        comRFID.Close();
        Button2.PerformClick();


    }

    else
    {
     Response.Write("<script>alert('Card Undefined, Please Register Your Card.');</script>");
        Send_cmd();
    }

}



我尝试执行点击方法但似乎不起作用。


I tried perform click method but seems like it doesn't work.

推荐答案

不是调用按钮的Click事件,而是将该按钮的代码放入私有方法,并从Click事件中调用该方法。然后,您可以从其他代码中调用相同的方法。



或者,如果要调用Button2.Click,则必须提供相应的参数。由于你不打算使用发件人或电子邮件,你可以将它们作为空值传递。



选项1中的这些是更好的方法。





Rather than calling the Click event of the button, put the code from that button into a private method and call that method from the Click event. You can then call the same method from your other code.

Alternatively, if you are going to call the Button2.Click then you must provide the appropriate parameters. As you are not going to use either sender or e you could just pass them as nulls.

Option 1 of these is the better approach.


protected void Button2_Click(object sender, EventArgs e)
{
    CallRedirect();
}
private void CallRedirect()
{
    Response.Redirect("Login.aspx");
}
private void comRFID_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        SomeSub();

        if ((strbuffer[11].ToString() == strCard1[11]))
        {
            UserID = "870104-07-5448";
            Send_Beep();
            System.Diagnostics.Debug.WriteLine(("UserId: " + UserID));
            comRFID.Close();
            CallRedirect();
        }
        else
        {
         Response.Write("<script>alert('Card Undefined, Please Register Your Card.');</script>");
            Send_cmd();
        }

    }


这篇关于调用按钮而不实际单击按钮C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆