Different behavior for the two submit buttons?

I am trying to have two submit buttons on my form - one accepts meetings; the other rejects them. They will have different forms of behavior. How can I do this in my C # code?

The functionality I want is essential

if(isPost) {
   if(//accept button pressed(Request.Form[???])) 
   {

   } 
   else 
   {


   }
}

      

Here is my HTML:

<button name="accept" type="submit">Accept</button>
<div class="spacer"></div>
<button name="decline" type="submit">Decline</button>
<div class="spacer"></div>

      

Simple enough, but I can't find a test for this on the internet or in any documentation. Does anyone know what I would like to do for this?

+3


source to share


6 answers


Give each button element the same name (in this example, "SubmitButton") but a different value, then check for different values ​​in your server, for example:

<button type="submit" name="SubmitButton" value="Accept">Accept</button>
<button type="submit" name="SubmitButton" value="Decline">Decline</button>

      

Then in your server code:



string buttonClicked = Request.Form["SubmitButton"]
if(buttonClicked == "Accept")
{
    // Accept code
}
else if(buttonClicked == "Decline")
{
    // Decline code
}

      

Be aware that this will not work in some earlier versions of IE, although you may have to do a little javascript on the client before the post request is made.

+1


source


As far as I remember you have a controller action similar to this:



public ActionResult MyAction(string accept, string decline)
{
    if (!string.IsNullOrEmpty(accept))
    {
        //do something
    }
    else 
    {
        //do something else
    }
}

      

+1


source


Assuming you are using MVC3 you would do something like

@using (Html.BeginForm("Accept","Meeting")) 
{
   <input type="submit" value="Accept" /> 
}
@using (Html.BeginForm("Decline","Meeting")) 
{
   <input type="submit" value="Decline" /> 
}

      

Then you just get your confirmation and rejection code in your meeting controller's Accept and Decline actions, respectively. There is no need for an if statement.

Controller example

public class MeetingController : Controller
    {   
        [HttpPost]
        public ActionResult Accept()
        {
            //Do accept stuff
            return View();
        }

        [HttpPost]
        public ActionResult Decline()
        {
            //Do decline stuff
            return View();
        }

    }

      

+1


source


Typically, in code in C #, you will search for a button by clicking a button. You will have an event handler for each button, and the code will react depending on which button was clicked.

this.btnRejectAppointment.Click += new System.EventHandler(this.btnRejectAppointment_Click);

      

And then your method

private void btnRejectAppointment_Click(object sender, System.EventArgs e)
{
    //code to reject appt.
}

      

And you will have ONLY for another button.

0


source


One possible solution is to use two type buttons LinkButton

, but specify slightly different ones PostBackUrl

:

<!-- Accept meeting button -->
<asp:LinkButton ID="acceptMeeting" runat="server"
                PostBackUrl="MeetingsManager.aspx?Mode=Accept" />

<!-- Decline meeting button -->    
<asp:LinkButton ID="declineMeeting" runat="server"
                PostBackUrl="MeetingsManager.aspx?Mode=Decline" />

      

And in the code behind:

public enum Mode
{
  Unknown,
  Accept,
  Decline
}

protected void Page_Load(object sender, EventArgs args)
{
    Mode currentMode = Mode.Unknown;
    var rawMode = Request.QueryString["Mode"];
    if (rawMode != null)
    {
       currentMode = (Mode)Enum.Parse(
                                 typeof(Mode), 
                                 rawMode.ToString())
    }
}

      

0


source


ASP.NET Button Control has an OnClick event, so add a different handler to each of your buttons:

<asp:Button OnClick="MyHandler1" ... />

      

in the code behind:

protected void MyHandler1(object sender, EventArgs args)
{ /* do stuff */ }

      

0


source







All Articles