I have TextBox with RequiredFieldValidator on my page.
I also have link that calls some simple javascript.
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox1"
Display="Dynamic" />
<asp:LinkButton ID="Link1" runat="server" OnClientClick="DoSomething(); return false;" CausesValidation="false"Text="Do" />
function DoSomething() {
textbox1.val("blah"); }
When user type something into textbox and then delete that text and focus next control - then validator fires.
Then user can use link that adds text using javascript. TextBox1 text is no longer empty but RequiredFieldValidator still shows error message. How to prevent that?
you can use javascript ValidatorEnable function
ValidatorEnable(document.getElementById('<%= ValidatorID.ClientID %>'), true);
I would recommend a CustomValidator here since you do NOT want the normal RequiredFieldValidator behavior. The CustomValidator client method will only run on postback.
The whole point of the validators is that they are to be used for both server side and client side validation.
If you only want to validate on the server side, don't use a validator and simply check that the values are valid and report an error.
Related
I have few textboxes and asp.net validation controls on a page. The validation fires when I click on "Submit" button. Is there anyway to fire the validation as soon as I leave the textbox? I would like to do the same validation for other 20+ fields in the form. However, only validating specific textbox when leaving it.
<asp:TextBox ID="txtclass" runat="server"></asp:TextBox>
<asp:RangeValidator ID="rvclass"
runat="server" ControlToValidate="txtclass"
ErrorMessage="Enter your class (6 - 12)" MaximumValue="12"
MinimumValue="6" Type="Integer">
</asp:RangeValidator>
<asp:Button ID="btnsubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />
Create a js function to perform the validating, then for each textbox you want to validate set the onblur and call that function. (I'm just free writing below so excuse the syntax error but you should get the gist)
function validateClass(val)
{
if(val.value > 5 && val.value < 13)
{
//alert(good!)
}
else
{
//alert(show error?)
}
}
call it in your textbox
<asp:TextBox ID="txtclass" runat="server" onblur="validateClass(this);"></asp:TextBox>
I am trying to get a javascript function to work properly with my asp.net controls. I have two radio buttons and three textboxes.
When the first radio button is clicked I would like to disable the third textbox and when the second radio button is clicked I would like to disable the third text box. See the attachment for a screenshot.
I have tested that the function works by adding "OnClick" to the first radio button and setting AN ALERT WHICH IS WORKING.
However, I am not sure how to pass the controls in to the function to have access to them. Anyone know how to do this?
Screenshot
ASP.NET
<asp:RadioButton ID="RadioButton1" runat="server" Checked="True" GroupName="DateTimeQuery" OnClick="TimeRangeClickEvent()"/>
<asp:TextBox ID="startdatetext" Enabled="true" runat="server" MaxLength="10"></asp:TextBox>
<asp:TextBox ID="enddatetext" Enabled="true" runat="server" MaxLength="10"></asp:TextBox>
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="DateTimeQuery" OnClick="TimeRangeClickEvent() />
<asp:TextBox ID="withinthepast" Enabled="true" runat="server" Text="1"></asp:TextBox>
Javascript
<script>
function TimeRangeClickEvent(How do I pass asp.net controls here)
{
alert('Working');
}
</script>
You should use ClientID control properties to get IDs of HTML elements and then work with them from Javascript. More info.
In Javascript:
var radio1 = document.getElementById('<%=RadioButton1.ClientID%>');
When you have all your HTML elements you just need to handle necessary change/clicked/selected events in javascript to make it work.
I have an ASP.NET 4.5 web form with inputs for a physical and mailing address. After entering a physical address, a user can tick a checkbox to fill in the mailing address if the information is the same.
This occurs via an OnClick function tied to the checkbox, which basically does this (I left out additional code):
document.getElementById("<%= txtMailAddressStreetNumber.ClientID%>").value = document.getElementById("<%= txtPermAddressStreetNumber.ClientID%>").value;
Each of the address textboxes has a RequiredFieldValidator. When I submit the form, it passes client-side validation and posts back to the server. There it fails (Page.IsValid is false). The textboxes that were populated by the OnClick function fail server-side validation unless I manually populate them.
I've tried calling Page_ClientValidate('FormGroup'); (FormGroup is the name of the validation group) after assigning values, and doing this doesn't trigger any validation error messages for the textboxes. However, when I submit the form (postback), the values inserted via Javascript are all cleared and I have to manually enter them.
Code in the .aspx page:
<asp:Label CssClass="mainlabel" ID="lblPermAddressZipCode" runat="server" Text="Zip Code" AssociatedControlID="txtPermAddressZipCode" />
<asp:RequiredFieldValidator ControlToValidate="txtPermAddressZipCode" ID="RequiredFieldValidator10" runat="server" ErrorMessage="*" Display="Dynamic" ValidationGroup="FormGroup" CssClass="formerror" />
<asp:TextBox ID="txtPermAddressZipCode" Width="100" onclick="openDialog(1)" ReadOnly="true" CssClass="readOnlyTextBox" runat="server" />
<asp:CheckBox ID="cbPermIsMailAddress" onclick="UsePermanentAsMailingAddress()" runat="server" /><small>(same as above)</small>
<asp:Label CssClass="mainlabel" ID="lblMailAddressZipCode" runat="server" Text="Zip code" AssociatedControlID="txtMailAddressZipCode" />
<asp:RequiredFieldValidator ControlToValidate="txtMailAddressZipCode" ID="RequiredFieldValidator15" runat="server" ErrorMessage="*" Display="Dynamic" ValidationGroup="FormGroup" CssClass="formerror" />
<asp:TextBox ID="txtMailAddressZipCode" ReadOnly="true" onclick="openDialog(2)" CssClass="readOnlyTextBox" Width="100" runat="server" />
Javascript:
function UsePermanentAsMailingAddress() {
//only copy if the address is complete
if (document.getElementById("<%= permCityStateZipId.ClientID %>").value != '') {
//exit if the box was unchecked
if (!$("#<%= cbPermIsMailAddress.ClientID%>").is(':checked')) {
return;
}
document.getElementById("<%= txtMailAddressZipCode.ClientID%>").value = document.getElementById("<%= txtPermAddressZipCode.ClientID%>").value;
//the ID value is the answer
document.getElementById("<%= mailCityStateZipId.ClientID%>").value = document.getElementById("<%= permCityStateZipId.ClientID %>").value;
//Page_ClientValidate('FormGroup');
}
else {
$("#<%= cbPermIsMailAddress.ClientID%>").removeAttr('checked');
alert("Please enter a complete permanent address first.");
}
}
Since the ReadOnly property is set in the .aspx, any changes made to it client-side will be ignored when it's posted back. If you absolutely need to have the textboxes read-only, then remove the property from your .aspx and set it with JavaScript on page load:
$(document).ready(function () {
$('#<%= txtMailAddressZipCode.ClientID%>').prop('readonly', true);
});
This way the user won't be able to directly change the textbox (I assume you have some other way of filling the textbox), but any changes will be posted back rather than the original (empty) values.
I have an asp button in default.aspx:
<asp:Button ID="Button1" runat="server" Text="Button" />
And there is a procedure in default.aspx.vb:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Panel1.Controls.Add(New LiteralControl(Date.Now))
End Sub
And teacher asked me, how to make exactly same operation (when click on button, shows up a date) but without postback and without javascript code. I could make a java code there and use OnClientClick="return false" but it seems not right
Refer The Client Side of ASP.Net Pages
You can't do. The default behaviour of Button(input type=submit) is to submit/post the form to the url specified in the form's action attribute.
If you want to prevent default behaviour you need to write a javascript return false
<asp:Button ID="btn" OnClientClick="return false" runat="server" Text="Button" />
By Default asp.net post the form to the same page itself. We say this as PostBack. See the form tags action value from rendered html in browser , it will be the same page name
<input type =submit name ="btn" id="btn"
onclick="javascript:__doPostBack('btn','')"value ="Button">
The following built in javascript code does this
<script>
function __doPostBack(eventTarget, eventArgument) {
document.Form1.__EVENTTARGET.value = eventTarget;
document.Form1.__EVENTARGUMENT.value = eventArgument;
document.Form1.submit();
}
</script>
Your page will have the below fields added automatically, inorder to detect which event needs to be fired in server side. you can access simply in a request parameters like Request.Params["__EVENTTARGET"]
<input type =hidden name ="__EVENTTARGET" value ="">
<input type =hidden name ="__EVENTARGUMENT" value ="">
Hi
While working on asp, i want if i click on my asp text field, a radio button should be checked, but i dont find any onClick event available for asp text field, and if i use onchange, it is from the server end, i want this situation without page refresh, just like we do in html fields using javascript, please help!!!
Try this one:
HTML:
<asp:TextBox ID="txtSelect" runat="server" Width="200px" Text="Click here"> </asp:TextBox>
<br />
<asp:RadioButton ID="radCheck" runat="server" />
JQUERY:
$("#txtSelect").click(function(){
$("#radCheck").attr("checked",true);
});
OnFocus is probably the event I would use.
You can use Ajax if you don't wanna refresh the page