I'm new to ASP.Net. I am trying to trigger action based on button click but the even is not getting triggered.
This is the button definintion in login.aspx:
<button type="submit" value="submit" class="btn_3" onclick="createAccount_Clicked" runat="server">
Create Account
</button>
This is the intended button click event inside login.aspx.cs:
protected void createAccount_Clicked(object sender, EventArgs e)
{
//Here's where you do stuff.
string value = new_fullname.Value.ToString();
}
new_fullname is the Text I'm trying to read (defined in login.aspx):
<div class="col-md-12 form-group p_star" runat="server">
<input type="text" class="form-control" id="new_fullname" name="name" value=""
placeholder="Full Name" runat="server">
</div>
So, you have set the button type to submit which actually submits the server form which does not guarantee the triggering of the intended event. What you should do is, set the button type to button to get it working.
This is how you will get it working:
<button type="button" value="submit" class="btn_3" onclick="createAccount_Clicked" runat="server">
Create Account
</button>
Related
On postback, how can I check which html button cause postback in code behind
<button type="submit" name="index" class="btn" />
<button type="submit" name="index" class="btn" />
<button type="submit" name="index" class="btn" />
You will have to change your markup if you would like to know which button caused a postback, so that each button has a unique name ( right now they all have the same name).
In addition,you must provide a value for each button in order to check which button posted back.
Also, it's recommended to provide an id, but in your situation you could still get to know which button caused postback without providing an id.
Markup recommended for your scenario
<form id="form1" runat="server">
<div>
<button type="submit" name="index1" class="btn" value="Button1">Button 1</button>
<button type="submit" name="index2" class="btn" value="Button2">Button 2</button>
<button type="submit" name="index3" class="btn" value="Button3">Button 3</button>
</div>
</form>
C# code to check which button posted back
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
if(Request["index1"] !=null)
{
//then first button posted back
//Request["index1"] will return the value property of the button index1 if it posted back
} else if(Request["index2"] !=null)
{
//then first button posted back
//Request["index2"] will return the value property of the button index2 if it posted back
} else if(Request["index3"] !=null)
{
//then first button posted back
//Request["index3"] will return the value property of the button index3 if it posted back
}
}
}
So, I have a Paypal form that worked wonders. However, I now need to add a coupon field, where someone can enter a code, and get a reduction based on whatever the backend replies.
This all works wonderfully, but I've ran into an issue when adding the option to know before checking out whether your code is valid or not. Currently, my form (once simplified) looks like this :
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"
id="payPalForm" runat="server">
<asp:TextBox runat="server" ID="txtCode" />
<asp:Button Text="Validate" OnClick="ValidateDiscount" runat="server" />
<asp:Label ID="txtDesc" runat="server" />
<input type="submit" name="Submit" value="Pay up!" />
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1" />
...
</form>
With the backend having the function :
protected void ValidateDiscount(object sender, EventArgs e)
{
this.txtDesc.Text = "fetch from database using: " + txtCode.Text;
}
My issue is that wheneve I click on the Validate button, the form is submitted and I end up on the Paypal website. I used Jquery at first with preventDefault(), but that actually prevents my server-side function from firing. I've also tried putting a standard <button> or <input type='button'> tag instead, but I couldn't get it to fire my server-side function.
Is there any way to have the Validate button not submit the form, or should I just remove the action from the form and manually submit the form when clicking on the submit button?
You have set your form action to post to PayPal.
This is the action:
action="https://www.sandbox.paypal.com/cgi-bin/webscr"
Here is where you have it in you form tag:
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"
id="payPalForm" runat="server">
Remove this from your form tag and it should postback to your application.
I have two buttons in the same page ( Razor/ ASP.NET Web Pages), how do I check which button I clicked,
so far I have:
if (IsPost)
{
//code block here
}
<input type="submit" value="Update" class="submit" id="btnUpdate" />
<input type="submit" value="Clear" class="submit" id="btnClear" />
the problem is (my low IQ, I know) it executes with any button clicked, only btnUpdate should execute the code block
Note: IS NOT MVC
note2: asp.net shows an example checking which textbox is empty to determine the action when a different button is clicked, is not what I'm looking for
<input type="submit" value="Update" class="submit" id="btnUpdate" name="update" />
<input type="submit" value="Clear" class="submit" id="btnClear" name="clear"/>
attach name attribute to buttons and check which one submitted via
if(IsPost){
if (Request.Form["clear"] != null)
{
//when clear clicked
}
else if (Request.Form["update"] != null)
{
//when update clicked
}
}
Please try this:
if (IsPostBack)
{
if(! string.IsNullOrEmpty(Request.Form["Button2"]))
Response.Write("{Button2 was clicked");
}
remove the "submit" class from your clear button, and change its type to "button"
. This will prevent it from posting the form.
I have a modal that pops up when you click a button, inside that modal I have a save button. When that save button is pressed I would like to fire some C# code in a with an OnClick function....
Can anyone tell me why this isn't working for me?
ASP.NET
<div class="modal fade" id="deviceModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4>Select</h4>
</div>
<div class="modal-body">
<asp:DropDownList ID="drpDownID" runat="server" CssClass="fields" EnableViewState="true">
<asp:ListItem Text="<Select>" Value="0" />
</asp:DropDownList>
</div>
<div class="modal-footer">
<asp:Button ID="btnSave" runat="server" Text="Save" data-dismiss="modal" CssClass="btn btn-primary" OnClick="btnSave_Click" />
<asp:Button ID="btnClose" runat="server" Text="Close" data-dismiss="modal" CssClass="btn btn-danger" OnClick="btnClose_Click" />
</div>
</div>
</div>
</div>
C#
protected void btnSave_Click(object sender, EventArgs e)
{
string test = "";
test = drpDownID.SelectedItem.ToString();
}
It won't even hit my break point.... And this is embedded in a form tag with a runat server
It works fine if I take the save button outside of the modal, but that completely defeats the purpose of the modal. So it has to be something with modal don't like click events very much...
Have you tried adding () to the onclick? like OnClick="btnSave_Click()".
EDIT: You can probably ignore that. It doesn't seem to matter for asp tags. Maybe this question can help you: ASP.NET button inside bootstrap modal not triggering click event
If your click on the save doesn't trigger a postback inside the dialog and it does outside the dialog: Can you use Firebug in Firefox to see whether the Save button is still part of the form?
I know JQuery dialog brings the container to the bottom of the body and therefor takes it out of a possible form, resulting in form posts not triggering from dialogues. I have a nice fix for this JQuery dialog issue, but that's probably not part of this topic.
I am trying to create a "next" button and a "back" button in my form. I want the "next" button to validate and submit the form. And I want the "back" button to simply go back to the "cart" page. But the "back" button keeps validating and trying to submit the form.
Here is my code:
<div class="buttons">
<button class = "button" id = "back">Back</button>
<input type="submit" value="Next" class="button" />
</div>
The reason I need the back link to be a button is so that it will look the same as the "next" button. Any ideas how I can get this working correctly?
A <button> element always submits the form. The same goes for a <input type="submit" /> element.
But a <input type="button" /> will not submit the form. That's what you want.
<div class="buttons">
<input type="button" class="button" id="back" onclick="window.location = '#Url.Action("Index", "Cart")';">Back</a>
<input type="submit" value="Next" class="button" />
</div>
Edit I'm not sure if you can put that inside an <a> element though. I reworked my example to use click events rather than a link. If it is valid to put inside a <a> element (can anyone confirm?), you can do it like that as well.