简体   繁体   中英

How to detect which button is clicked and if two are clicked then call the method?

 protected void Button8_Click(object sender, EventArgs e)
    {


        if (TextBox2.Text == "P" && TextBox3.Text == "E" && TextBox4.Text == "N")
        {
            MessageBox.Show("You Earn 100 Coins");


           bool Button8_Click = true;
             bool Btn = true;
             if (Btn == true && Button8_Click == true)
             {
                 // Button11_Click1(sender, e);
                 TextBox5.Text = sc();
                 Session["s"] = TextBox5.Text.ToString();
             }
             else if (Btn == false && Button8_Click == true)
             {
                 TextBox5.Text = sr();
                 Session["s"] = TextBox5.Text.ToString();
             }

            Session["s"] = TextBox5.Text.ToString();
            Response.Redirect("WebForm2.aspx");
        }

        }

What I want is if btn11 and btn8 both are clicked then call sc method if only one btn8 is clicked call sr method.

You can't do it simultaneously due to events aren't asynchronous. You have to store the first click on Button8 in variable and the second click on Button11 will check the variable to call the right method.

You are in WEB process so you can't say to HTTP request to manage two event in only one request ;-).

If you want to do that you can do it with AJAX request. The principle is that you call the same WebMethod with a lock inside allows you to assign a variable for first button and after that on the second call you can't check the value of variable and call the right method.

Let's me illustrate to you a quickly sample of what you must have

public static readonly object Lock = new object();

public static bool isBtn8Clicked = false;

[WebMethod]
public void CallMeBaby(string btnName){
    lock (TypeOfMyPage.obj) {
        isBtn8Clicked = true;
    }

    //Your business code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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