ASP.NET RequiredFieldValidator : How can i bind value to " InitialValue " property? - asp.net

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.

Related

Required Field Validator for Dropdown list in ASP.NET

I have the following dropdown in my page
<asp:DropDownList ID="cboEmployerType" runat="server" TabIndex="8" Width="60%" onclick="javascript:shouldsubmit=false;">
<asp:ListItem Value="Null">-Select-</asp:ListItem>
<asp:ListItem Value="E">Employer</asp:ListItem>
<asp:ListItem Value="O">OJT Provider</asp:ListItem>
</asp:DropDownList>
And a RequiredFieldValidator for it
<asp:RequiredFieldValidator ID="cboEmployerType_RequiredFieldValidator" runat="server" InitialValue="null" ErrorMessage="Employer Type Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboEmployerType" ValidationGroup="valEmployer" Display="None"></asp:RequiredFieldValidator>
But I do not get the Validation Message. What am I missing?
You have taken Display="None" in RequiredFieldValidator take it as
Display="Dynamic"
and take InitialValue="Null"
also assign the same validation group to drop down list
i.e. ValidationGroup="valEmployer"
It works. Just make the following 3 changes:
Either remove Display="None" or use Display="Dynamic" in RequiredFieldValidator
Set ValidationGroup="valEmployer" to dropdown as well as the button for which the validation should occur
Set InitialValue="Null" instead of InitialValue="null" in RequiredFieldValidator with capital "N"
The following is the code
<asp:DropDownList ID="cboEmployerType" ValidationGroup="valEmployer" runat="server" TabIndex="8" Width="60%" onclick="javascript:shouldsubmit=false;">
<asp:ListItem Value="Null">-Select-</asp:ListItem>
<asp:ListItem Value="E">Employer</asp:ListItem>
<asp:ListItem Value="O">OJT Provider</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="cboEmployerType_RequiredFieldValidator" runat="server" InitialValue="Null" ErrorMessage="Employer Type Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboEmployerType" ValidationGroup="valEmployer" Display="Dynamic"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" ValidationGroup="valEmployer" runat="server" Text="Button" />
Hope this helps.
try with InitialValue
InitialValue="Null" Display="Dynamic"
Remove ValidationGroup="valEmployer" property from the RequiredFieldValidator control
and set InitialValue="Null" instead of "null" in RequiredFieldValidator control.
Set Display=Dynamic.
Try this.
Add ValidationGroup="valEmployer" property in dropdownlist and in the button on click of which validation occurs.
OR
Just remove ValidationGroup="valEmployer" property from the RequiredFieldValidator control.
This is working for me-
<asp:DropDownList ID="cboEmployerType" ValidationGroup="valEmployer" runat="server" TabIndex="8" Width="60%" onclick="javascript:shouldsubmit=false;">
<asp:ListItem Value="Null">-Select-</asp:ListItem>
<asp:ListItem Value="E">Employer</asp:ListItem>
<asp:ListItem Value="O">OJT Provider</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="cboEmployerType_RequiredFieldValidator" ValidationGroup="valEmployer" runat="server" InitialValue="Null" ErrorMessage="Employer Type Required" ForeColor="Red" Font-Size="0.9em" ControlToValidate="cboEmployerType" Display="Dynamic"></asp:RequiredFieldValidator>
Keep ValidationGroup="valEmployer" on which event you want to fire the Validations.

validate a combination of different fields

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.

Validating dropdownlist inside gridview control

for testing i have tried customvalidation
function ClientValidate(sender, args) {
//return false for testing...
args.IsValid = false;
}
<asp:CustomValidator runat="server" ID="CustomValidator1" ControlToValidate="ddldetail"
Text="Please select" ValidateEmptyText="true"
ClientValidationFunction="ClientValidate"
Display="Dynamic">
</asp:CustomValidator>
edit: here is what i exaclty wants to happen:
how to validate a dropdownlist and i have done this zillion times but what i am doing wrong here? any second pair of eye might spot it? i am trying to validate the dropdownlist if the user have not select any help?
<asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" CausesValidation="true"/>
<asp:GridView ID="GVInputMapping" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
EnableModelValidation="True" onrowdatabound="GVInputMapping_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" ControlStyle-Width="250px" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddldetail">
<asp:ListItem Selected="True" Value="0">Select me</asp:ListItem>
<asp:ListItem Value="1">abc</asp:ListItem>
<asp:ListItem Value="2">GHt</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="requiredDDL" runat="server"
ControlToValidate="ddldetail" ErrorMessage="Please select" InitialValue="Select me" Display="Dynamic"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have the InitialValue of the validator set to Select me, but the Value of that item is actually 0:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Select Me" Value="0" />
<asp:ListItem Text="Foo" Value="1" />
<asp:ListItem Text="Bar" Value="2" />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="DropDownList1"
ErrorMessage="Please select"
InitialValue="0"
Display="Dynamic">
</asp:RequiredFieldValidator>
You'll also need to assign a unique validation group for each row, otherwise validation will kick off for every row. To assign a unique validation group, you can use the Id column:
ValidationGroup='<%# string.Format("Group_{0}", Eval("Id")) %>'
You would add this to the validator and the button in the row.

Isolate a required field validator?

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" />

asp.net Validation on DropDownList box

I have a dropdownlist (cboViewAlbums) which has displays album values. The first item is a
Please select an album.... I am trying to use validation which when lb_create_album linkButton is clicked throws an error if the cboViewAlbums list has the value 0 selected.
Below is the code for the this and my attempt:
<asp:DropDownList ID="cboViewAlbums" runat="server"
DataSourceID="SqlDataSource1" DataTextField="album_name"
DataValueField="album_id" Width="250px" AutoPostBack="True" AppendDataBoundItems="true">
<asp:ListItem Value="0">Please select an album...</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="lb_create_album" runat="server">Create Album</asp:LinkButton>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:fpaConnectionString %>"
SelectCommand="SELECT [album_id], [album_name] FROM [fpa_albums] ORDER BY [album_name]">
</asp:SqlDataSource>
<br />
<asp:HyperLink CssClass="example7" ID="hLinkUploadPhotos" NavigateUrl="multiple_upload.aspx" runat="server">Upload Multiple Photos</asp:HyperLink>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="cboViewAlbums" ErrorMessage="Please Select an Album"
InitialValue="Please select an album..."></asp:RequiredFieldValidator>
Any idea how I can get this working?
Thanks
You have to use Range validator with dropdown list & set mininmum value greater then 0 & maximum value to set any max value , aslo provide type value of min & max and that is integer.
Below is the sample code i have make for you you have to bind your datasource insted of static list items.
<asp:DropDownList runat="server" ID="ddl1" >
<asp:ListItem Value="0" Text="Select value" />
<asp:ListItem Value="1" Text="text1" />
<asp:ListItem Value="2" Text="text2" />
</asp:DropDownList>
<asp:RangeValidator ErrorMessage="Please select value" ControlToValidate="ddl1" runat="server"
MinimumValue="1" MaximumValue="100000000" Type=Integer />
<asp:Button Text="text" runat="server" />
If this helpful to you please mark as an answer
Thanks
Arun
First, you can't do validation with HyperLink, better use the LinkButton. HyperLink does not do post back, so that's your first error.
Second, on your RequiredFieldValidator, put the initialvalue=0, and that should fix your problem.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="cboViewAlbums" ErrorMessage="Please Select an Album"
InitialValue="0"></asp:RequiredFieldValidator>

Resources