Reputation: 399
I am working on an ASP.NET page with a few different bits of functionality. My first button works just fine, and looks like this:
<form method="post">
<h2>View an Existing Reservation</h2>
<p><sub>Or just enter your email to recover your reservations</sub></p>
<div>
<div class="form-row">
<div class="col-lg-6 col-md-6 col-sm-12">
<label for="name">Reservation ID:</label>
<asp:TextBox runat="server" ID="ID" Name="ID" CssClass="form-control"></asp:TextBox><br />
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
<label for="email">Email</label>
<asp:TextBox runat="server" ID="Email" TextMode="Email" CssClass="form-control"></asp:TextBox><br />
</div>
</div>
<div class="form-row justify-content-around">
<asp:Button ID="Button2" Text="Edit" onclick="Button2_Click" runat="server" CssClass="btn btn-primary"/>
</div>
</div>
</form>
with the code behind of:
public void Button2_Click(object sender, EventArgs e){}
In the next button, I had the exact same setup, replacing "Button2" with "Button3." SO just doesn't allow me to post too much code at once.
The page doesn't access the code behind.
What I have Tried so far:
using Button3 to try accessing Button2_Click(). This does not work either (i.e., no function runs)
using "OnClick" instead of "onclick." This also does not run the function.
Checking the designer.cs - all of my designer.cs files simply show an empty partial class, and all work just fine. I'm updating them using the VS2019 compiler.
Is there anything missing from the second button that needs to be there?
I solved the problem by simply changing how I was doing it (i.e., conditional logic in Button2_Click() to have different behavior based on input). If anyone wants to take a crack at the root problem to help anyone with this problem in the future, be my guest.
Upvotes: 1
Views: 2100
Reputation: 399
Not really an answer to the root of the question, but if you have two forms operating on the same (or less than each other) inputs, then depending on your business needs, this will work:
<form method="post">
<h2>View an Existing Reservation</h2>
<p><sub>Or just enter your email to recover your reservations</sub></p>
<div>
<div class="form-row">
<div class="col-lg-6 col-md-6 col-sm-12">
<label for="name">Reservation ID:</label>
<asp:TextBox runat="server" ID="ID" Name="ID" CssClass="form-control"></asp:TextBox><br />
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
<label for="email">Email</label>
<asp:TextBox runat="server" ID="Email" TextMode="Email" CssClass="form-control"></asp:TextBox><br />
</div>
</div>
<div class="form-row justify-content-around">
<asp:Button ID="Button2" Text="Edit" onclick="Button2_Click" runat="server" CssClass="btn btn-primary"/>
</div>
</div>
</form>
public void Button2_Click(object sender, EventArgs e)
{
var keys = Request.Params.AllKeys;
double id = Request.Form["ctl00$MainContent$ctl00$ID"] != "" ? double.Parse(Request.Form["ctl00$MainContent$ctl00$ID"].ToString()) : -1;
string email = Request.Form["ctl00$MainContent$ctl00$Email"].ToString();
if(id == -1) // some default value if the field is left empty
{
resend(email); // the function supposed to be run by the smaller form
return;
}
var obj = Request.Form;
Response.Redirect(String.Format("View.aspx?id={0}&email={1}", id, email));
}
Basically, by seeing if the value not required for the smaller form (in this case, the resend() function) is empty, we see what the user's intent was - by inputting both values, they get one function, and by only submitting one value, they get another function. This is scalable to as many functions as you want, although it'd start to be a pain eventually.
Upvotes: 1