Clear button should not required field HTML5 - c#

im creating the form include below code :
<asp:TextBox ID="txtPhone" required="required" runat="server" placeholder="Phone No. (eg. 999)"
CssClass="glowStyle"></asp:TextBox>
<br />
<asp:Button ID="btnAddDriver" Text="Add" runat="server" CssClass="button" />
<br />
<asp:Button ID="btnCnclAddDriver" Text="Clear" runat="server"
CssClass="cancelbutton" onclick="btnCnclAddDriver_Click" />
</form>
when i click the "Clear" button, the required (html5) still show up.
how to disable it for Clear button only.

Add formnovalidate attribute to your cancel/clear button.
Here you can find some more information about this attribute.

Do you actually require a server control to cancel ? A simple <input type="reset"> or a small javascript code can do the job in most situations.
If not, simple add CauseValidation="false" to your button :
<asp:Button ID="btnCnclAddDriver" CauseValidation="false" Text="Clear" runat="server"
CssClass="cancelbutton" onclick="btnCnclAddDriver_Click" />
(remove suggestion as it applies to asp.net validation)

You can set the autopostback option for btnCnclAddDriver button to False. And execute your server side code (btnCnclAddDriver_Click) by Ajax.

You can clear your TextBox(s) with javascript (jQuery) :
$('txtPhone').val('');
Or Javascript :
function name()
{
document.getElementById('txtPhone').value = "";
}
Hope this help.

Related

How to track asp:button click with Google Tag Manager

Let's say I've got an asp:button on a website which I'm trying to track to count how many times it has been clicked by different users.
<asp:Button ID="SubBtn" runat="server" OnClick="Button1_Click" runat="server" Text="Sub" />
In my code behind I've got a redirect link to a different page.
I'm aware that I need to include something like
<a href="#" name="button1" onclick="dataLayer.push({'event': 'button1-click'});" >Button 1</a>
Do I need to convert my asp:button to a html button to make this work?
You need to use attribute "OnClientClick":
<asp:Button ID="SubBtn" runat="server" OnClick="Button1_Click" runat="server" Text="Sub" OnClientClick="dataLayer.push({'event': 'button1-click'});" />
You can read more about this attribute on MSDN: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick(v=vs.110).aspx

some time button click event is being fired twice

It seems that sometimes (but not always) my button click event is being fired twice. The fact that it seems to happen sometimes but not always is really puzzling me. i already try button visible false or disable functionality. please help. Here is my button :
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" style="background-color:#e3e6ea; padding:5px; color:#000000; font-weight:bold;" ValidationGroup="Validate" />
Use the OnClientClick and UseSubmitBehavior properties of the button control you can prevent the double click , change your asp.net code as follows :
<asp:Button ID="btnSubmit" runat="server"
Text="Submit"
OnClick="btnSubmit_Click"
OnClientClick="this.disabled=true;"
UseSubmitBehavior="false"
style="background-color:#e3e6ea;
padding:5px;
color:#000000; font-weight:bold;" ValidationGroup="Validate" />
You need to add OnClientClick="this.disabled=true;" and UseSubmitBehavior="false" to your button control
i think you have used "btnSubmit_Click" event twice in your code, please check your code ,i hope your problem will solve.

prevent The Client side validation go to server side

I have a form and I have a validation in JavaScript. How to prevent the submit button go the the server side if the form is not valid?
<button id="LoginButton" onclick="Login.initReuiredValidation();"
onserverclick="LoginButton_Click" runat="server" type="submit"
class="submit btn btn-primary pull-right">
<asp:Literal runat="server" meta:resourcekey="LoginButton" />
<i class="icon-angle-right"></i>
</button>
in jquery you can do as
$(yourform).submit(function(event){
event.preventDefault();
//do somehting
})
This isn't classic asp, it's asp.net webforms and I've edited the tags accordingly
Adding input validation in webforms is quite easy, you can assign a validation control to each form control, eg
Contact Name<br />
<asp:TextBox ID="Contact_name" runat="server" Width="246 pt"></asp:TextBox>
<asp:RequiredFieldValidator ID="ContactNameValidator" runat="server"
ErrorMessage="Please enter your name" ControlToValidate="Contact_name"></asp:RequiredFieldValidator><br />
<br />
Contact Telephone<br />
<asp:TextBox ID="Contact_phone" runat="server" Width="246pt"></asp:TextBox>
<asp:RequiredFieldValidator ID="ContactPhoneValidator" runat="server" ControlToValidate="Contact_phone"
ErrorMessage="Please enter your telephone number"></asp:RequiredFieldValidator>
If you view the output, you will see that the validation is actually done with (client side) JavaScript. Asp.net generates that for you, you don't have to write it yourself.
Further information here
https://msdn.microsoft.com/library/a0z2h4sw%28v=vs.100%29.aspx
In the code behind add...
LoginButton.Attributes.Add("onclick", "return false;");

ASP: Html Form action calls automatically on textbox OnTextChanged

I have defined form action in my asp page and with in form, I have defined the Textbox OnTextChanged. but when I type any thing on Textbox and click on tap to move next.. automatically instead of calling OnTextChanged ..my form action calls..
my code is below
<form id="WebToLead" action="https://url" method="POST" runat="server">
<asp:TextBox id="a" name="a" maxlength="10" runat="server" OnTextChanged="a_TextChanged" AutoPostBack="true" />
// some more textbox
<asp:Button ID="Button2" Text="Click" width="50px" runat="server" OnClick="Button2_Click" />
</form>
Means it action should be triggered on button2_click but it does when i type anything on aand tap to other fields
Ensure you have viewstate enabled for this to work, otherwise Asp.Net has no way of knowing whether the control's value has changed.
AutoPostBack is enabled thats why it is posting the form back to server. Remove it or disable it. It should work after that.

Using button as link that executes javascript prevents OnClick from working

I am using a button as a link because when the button is pressed it opens a popup of In Line HTML using Javascript (http://www.enthropia.com/labs/ibox/):
<asp:Button ID="searchButton" runat="server" Text="Search Stories" OnClick="searchButton_Click" />
However, this prevents the OnClick C# from executing when I press the button. How do I fix this?
I suggest you this code
<a href="#inner_content" rel="ibox&width=800&height=400" title="Search Non Scrum Stories">
</a>
<asp:Button ID="searchButton" runat="server" Text="Search Stories" OnClick="searchButton_Click" />
David you must separate html link and button (it's not xaml)
Alternatively, you can use LinkButton - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.aspx
Example (from the linked documentation):
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt"
OnClick="LinkButton_Click"
runat="server"/>
separate the tags and put your button in as a submit button or you can do something like:
<form action="ur_popup_page" onsumit="javascriptsunction()">
<asp:Button type="submit" id="searchbutton"/>
</form>

Categories