aspx page
<asp:Label ID="Label1" runat="server" Text="Date of Birth: "></asp:Label>
<asp:Label ID="dob_msg_lbl" runat="server" Font-Size="Medium" ForeColor="Red" Text="invalid date of birth" visible="False"></asp:Label>
<br />
<asp:TextBox ID="day_tb" runat="server" MaxLength="2" Width="15px"></asp:TextBox> <asp:Label ID="Label4" runat="server" Text="/" Font-Bold="True"></asp:Label>
<asp:DropDownList ID="month_ddl" runat="server">
<asp:ListItem Value=""></asp:ListItem>
<asp:ListItem Value="Jan">January</asp:ListItem>
<asp:ListItem Value="Feb">Feb</asp:ListItem>
<asp:ListItem Value="Mar">Mar</asp:ListItem>
<asp:ListItem Value="Apr">Apr</asp:ListItem>
<asp:ListItem Value="May">May</asp:ListItem>
<asp:ListItem Value="Jun">Jun</asp:ListItem>
<asp:ListItem Value="Jul">Jul</asp:ListItem>
<asp:ListItem Value="Aug">Aug</asp:ListItem>
<asp:ListItem Value="Sep">Sep</asp:ListItem>
<asp:ListItem Value="Oct">Oct</asp:ListItem>
<asp:ListItem Value="Nov">Nov</asp:ListItem>
<asp:ListItem Value="Dec">Dec</asp:ListItem>
</asp:DropDownList> <asp:Label ID="Label5" runat="server" Text="/" Font-Bold="True"></asp:Label> <asp:TextBox ID="year_tb" runat="server" Width="30px" MaxLength="4"></asp:TextBox>
aspx.vb page
Dim dobStr As String = day_tb.Text + " " + month_ddl.SelectedValue + " " + year_tb.Text
Try
dob = Convert.ToDateTime(dobStr)
Catch ex As Exception
dob_msg_lbl.Visible = True
End Try
Currently I am mimicking the asp.net's validation functions by setting dob_msg_lbl.visible to true when Convert.ToDateTime results in an exception.
The problem with such a method is that the validation only occurs when the form is submitted.
I want to validate it on the fly, just like what happens when you use RequiredFieldValidator and RegularExpressionValidator.
Is it to possible to use something such as the CustomValidator to validate a combination of textbox and dropdownlist on the fly?
You can try to use CustomerValidator as explained here
http://www.codeproject.com/Articles/9522/CustomValidator-dependent-on-multiple-controls
Or
you can create a validate function with your logic, and call it on the Selection change event for dropdown and text change event of TextBox. So anytime, any of those thing changes that will trigger the event and will call your validate function.
Related
I have the following code
<div>
<asp:DropDownList ID="testDropDownList" runat="server" ValidationGroup="testValidationGroup">
<asp:ListItem Value="Choose">[ Select Item ... ]</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="testRequiredFieldValidator" runat="server" ValidationGroup="testValidationGroup"
ErrorMessage="*" InitialValue="Choose" ControlToValidate="testDropDownList"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="testButton" runat="server" OnClick="testButton_Click" Text="Button"
ValidationGroup="testValidationGroup" />
<br />
</div>
in which i validate Dropdownlist by RequiredFieldValidator
If changed the value of initialvalue property to read from static property in stactic classs .. but it always give me emtpy string in runtime unless this property have the value "Choose" ...
<asp:DropDownList ID="testDropDownList" runat="server" ValidationGroup="testValidationGroup">
<asp:ListItem Value="Choose">[ Select Item ... ]</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="testRequiredFieldValidator" runat="server" ValidationGroup="testValidationGroup"
ErrorMessage="*" InitialValue='<%# Util.ChooseValue %>' ControlToValidate="testDropDownList">
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="testButton" runat="server" OnClick="testButton_Click" Text="Button"
ValidationGroup="testValidationGroup" />
Could Any one help me to know what's the issue in my code ??
Please call
testRequiredFieldValidator.databind()
in page load event and let me know if this is still an issue.
Set InitialValue of RequiredFieldValidator on page_load event in aspx.cs page.
<asp:DropDownList ID="ddTitle" runat="server" DataTextField="TitleName" DataValueField="TitleId"ValidationGroup="t1">
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ID="ReqDropDnw" ControlToValidate="ddTitle" Display="Dynamic" ValidationGroup="t1" InitialValue="<-- Select Title-->" ErrorMessage="Please Select Title">
</asp:RequiredFieldValidator>
I have used this but it's not working, can I have a proper solution for it
My dropdown list is a dynamic.
Use like this way...
<asp:DropDownList ID="ddl" runat="server">
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
<asp:ListItem Text="abc" Value="1"></asp:ListItem>
<asp:ListItem Text="xyz" Value="2"></asp:ListItem>
</asp:DropDownList>
Now you can use required field validator for this dropdownlist.....
After binding data from DB you can use add one empty item to list like:
ddTitle.Items.Insert(0, "");
And now Required field validatior will work for this dropdown.
I have two search buttons on a page, one linked to a dropdown list and one linked to a dropdown list with a textbox for more search criteria. I have required field validators on all of the aforementioned controls. When I choose something from the first dropdown and click the appropriate search button, the field validator for the textbox fires, disabling the first search button. Is there a way to localize/isolate the validators to only associate with one of the two buttons? Code below:
<asp:Panel ID="Panel1" runat="server" HorizontalAlign="Center">
<asp:Label ID="Label1" runat="server" Text="Search by status:"></asp:Label>
<asp:DropDownList ID="DdlStatus" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Status" DataValueField="Status" AppendDataBoundItems="true">
<asp:ListItem Text="Choose a status" Value="0" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="BtnStatusSearch" runat="server" Text="Search" onclick="BtnStatusSearch_Click" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [Status] FROM [Status]"></asp:SqlDataSource>
<asp:Label ID="LblSearch" runat="server" Text="Other search:"></asp:Label>
<asp:DropDownList ID="DdlSearch" runat="server">
<asp:ListItem Selected="True" Value="0">Choose search criteria</asp:ListItem>
<asp:ListItem Value="1">Broker</asp:ListItem>
<asp:ListItem Value="2">Customer</asp:ListItem>
<asp:ListItem Value="3">Customer State</asp:ListItem>
<asp:ListItem Value="4">Broker State</asp:ListItem>
</asp:DropDownList><asp:RequiredFieldValidator ID="RfvDdlSearch" runat="server" Display="Dynamic"
ErrorMessage="Required field" ControlToValidate="DdlSearch" CssClass="ErrorMessage"></asp:RequiredFieldValidator>
<asp:TextBox ID="TbSearch" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTbSearch" runat="server" Display="Dynamic"
ErrorMessage="Required field" ControlToValidate="TbSearch" CssClass="ErrorMessage"></asp:RequiredFieldValidator>
<asp:Button ID="BtnSearch" runat="server" onclick="BtnSearch_Click" Text="Search" />
Yes, you can use the ValidationGroup property and set that validation group to your button control: ValidationGroup="button1"
<asp:RequiredFieldValidator ID="rfv" runat="server" ValidationGroup="button1"
ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:Button ID="btnLogin" runat="server" Text="Login" ValidationGroup="button1" OnClick="btnLogin_Click" />
I am having two controls in that i have set the required field vaildators for that controls.my issues is for the time being i have set that two controls disabled but this required validator is checking if the two controls in the disabled mode.i am checking that required field validator while clicking the save button.how to overcome this?
<asp:DropDownList ID="DropDownList1" Width="150" runat="server" TabIndex="17" Enabled="false"
AppendDataBoundItems="true">
<asp:ListItem Selected="True" Text="--Select Company--"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="DropDownList1"
ValidationGroup="UserDetailsGroup" InitialValue="--Select Company--" ErrorMessage="*"></asp:RequiredFieldValidator>
texboxcontrol:
<asp:TextBox ID="AcquiredDate" runat="server" Width="150" TabIndex="18" Enabled="false"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="AcquiredDate"
ValidationGroup="UserDetailsGroup" ErrorMessage="*"></asp:RequiredFieldValidator>
here the save button is the image button?
<asp:ImageButton ID="Save" runat="server" ImageUrl="~/Images/save-button.gif"
OnClick="Save_Click" TabIndex="41" ValidationGroup="UserDetailsGroup" />
How to overcome this pblm either in server side or client side?
Disable the Validators:
RequiredFieldValidator3.Enabled = DropDownList1.Enabled
RequiredFieldValidator2.Enabled = AcquiredDate.Enabled
This piece of code
<asp:DropDownList runat="server" ID="testdropdown" SelectedValue="2">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
yields this error:
The 'SelectedValue' property cannot be
set declaratively.
Yet, this is a legal and commonly used edit template for databound GridViews. The SelectedValue attribute certainly appears to be declaratively set here.
<EditItemTemplate>
<asp:DropDownList runat="server"
ID="GenreDropDownList"
DataSourceID="GenreDataSource"
DataValueField="GenreId"
DataTextField="Name"
SelectedValue='<%# Bind("Genre.GenreId") %>'>
</asp:DropDownList>
</EditItemTemplate>
The question is: what is the difference between the cases when you are allowed to set it declaratively and those in which you are not? The error message implies that it's never allowed.
in markup use SelectedValue='<%# "32" %>' syntax .(note the order of single and then the double quotes in the following example ):
<asp:DropDownList ID="ddlField" SelectedValue='<%# "32" %>'
runat="server" DataTextField="Name" DataValueField="ID" >
</asp:DropDownList>
or in code-behind just after DataBinding .(example):
ddlField.DataSource = Fields.SelectAll();
ddlField.DataBind();
ddlField.SelectedValue = "32";
It means you cannot set it through the designer.
The correct way is:
<asp:DropDownList runat="server" ID="testdropdown">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2" Selected></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
The reason the bound method works is because the value isn't selected in design mode but at runtime after the control is bound to a datasource
The DropDownList.SelectedValue method is meant to be applied at runtime hence the error about not being able to set it 'decoratively'