简体   繁体   中英

handling multiple submit buttons in one form ASP.Net

I'm trying to develop some web page in Asp.Net and C# which almost look like facebook login page where user can sign in at top of the page and sign up at the bottom, how should I do that? and when the focuses are on the textboxs of sign in part and the user hit the Enter it should go to Sign in method,

I already searched in stackoverflow about it and it seems all questions which already asked were about Asp.Net MVC which I'm not using.

You can assign a single event to multiple button clicks -

topButton.Click += SignInButtonClick;
bottomButton.Click += SignInButtonClick;

void SignInButtonClick(object sender, EventArgs e)
{
    //your code here

}

I presume in your case the same logic will run for both buttons, you could find out which button was clicked using sender argument that is passed into the function.

As an example, let's say I have an aspx page and I add a button to it called 'Button1' I can then add a click event via Visual Studio and it will create a methiod to handle the click event for me called Button1_Click . The method will be automatically linked to the button as the AutoEventWireup up property in c# is set to true by default .

Now, if I add a second button and call it 'Button2' but I want that button to fire the same event handler as the one used for Button1, I could add this code to the pages 'Page_Load' event handler

Button2.Click += Button1_Click;

Both buttons would then cause the 'Button1_Click' method to run when clicked.

you rather use html Controls then Asp.net Control and can use Jquery to perform the loginb or signup

    $('#login').click(function(event)
    {
      // call
      // $.ajax(),$.get or $.post method for ajax call
     });
    $('#signup').click(function(event)
    {
      // call
      // $.ajax(),$.get or $.post method for ajax call
     });
    </script>

    <form id="form1">
    <input type="button" value="login" id="login"/>
    </form>
    <form id="form2">
    <input type="button" value="signup" id="signup"/>
    </form>

you can connect multiple buttons to the same method by doing something like in the page load:

lbTest1.Click += Test_Click;
lbTest2.Click += Test_Click;

void Test_Click(object sender, EventArgs e) 
{ 
    //your code here 
} 

you can use the enter button to fire the method of a button by wrapping both the textbox as the button in a panel and set the defaultbutton property of the panel.

The simplest solution is to wrap the related controls (eg the signup controls) in a Panel control, and set the DefaultButton property on the Panel. You can read about it in this tutorial

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