<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.
Related
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.
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.
I have got problem with FileUpload control. I have this one, two drop down list, text box and button. If I select in first dropDownList "Yes" second one become disable and set value on NO (In second ddl I have two option YES or NO and in first one as well) however if I select NO in first dropDownList I posible to choose both option in second dropDownList. First ddl change second one on postBack using selectedIndexChanged evet and when it happends I loose file name in UploadFile control which I set before.
Code sample:
<asp:FileUpload ID="fuUploadGeometry" runat="server" Width="100%" />
<asp:DropDownListID="ddlSymmetry"runat="server" AutoPostBack="true"
onselectedindexchanged="ddlSymmetry_SelectedIndexChanged">
<asp:ListItem Value="0">-- Select --</asp:ListItem>
<asp:ListItem Value="true">Yes</asp:ListItem>
<asp:ListItem Value="false">No</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlModule" runat="server" Enabled="True">
<asp:ListItem Text="-- Select --" Value="0"/>
<asp:ListItem Text="Yes" Value="1" />
<asp:ListItem Text="No" Value="2"/>
</asp:DropDownList>
<asp:TextBox ID="txtTopic" runat="server"></asp:TextBox>
What should I do to keep file name in UploadFile control during changes selected options in drop down lists?
Try this, I added the label so you can see that the postback of the onselectedindexchange only affects the dropdown and not the file upload control, hope this helps.
<asp:FileUpload ID="fuUploadGeometry" runat="server" Width="100%" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlSymmetry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSymmetry_SelectedIndexChanged">
<asp:ListItem Value="0">-- Select --</asp:ListItem>
<asp:ListItem Value="true">Yes</asp:ListItem>
<asp:ListItem Value="false">No</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlModule" runat="server" Enabled="True">
<asp:ListItem Text="-- Select --" Value="0" />
<asp:ListItem Text="Yes" Value="1" />
<asp:ListItem Text="No" Value="2" />
</asp:DropDownList>
<asp:TextBox ID="txtTopic" runat="server"></asp:TextBox>
<asp:Label runat="server" ID="msgFromList" />
</ContentTemplate>
</asp:UpdatePanel>
protected void ddlSymmetry_SelectedIndexChanged(Object sender, EventArgs e)
{
msgFromList.Text = ddlSymmetry.SelectedItem.Value.ToString();
}
First thing keep in ur mind that is FileUpload Control Will became empty if any post back event occur on ur web page. So best solution is to put ur file upload control after all controll that can cause a post back like drop down list.
I have a DropDownList binded with aSqlDataSource to display the values from the database.
I am unable to validate using a RequiredFieldValidator.
For the most part you treat it as if you are validating any other kind of control but use the InitialValue property of the required field validator.
<asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="your-dropdownlist" InitialValue="Please select" ErrorMessage="Please select something" />
Basically what it's saying is that validation will succeed if any other value than the 1 set in InitialValue is selected in the dropdownlist.
If databinding you will need to insert the "Please select" value afterwards as follows
this.ddl1.Items.Insert(0, "Please select");
Suppose your drop down list is:
<asp:DropDownList runat="server" id="ddl">
<asp:ListItem Value="0" text="Select a Value">
....
</asp:DropDownList>
There are two ways:
<asp:RequiredFieldValidator ID="re1" runat="Server" InitialValue="0" />
the 2nd way is to use a compare validator:
<asp:CompareValidator ID="re1" runat="Server" ValueToCompare="0" ControlToCompare="ddl" Operator="Equal" />
If you are using a data source, here's another way to do it without code behind.
Note the following key points:
The ListItem of Value="0" is on the source page, not added in code
The ListItem in the source will be overwritten if you don't include
AppendDataBoundItems="true" in the DropDownList
InitialValue="0" tells the validator that this is the value that
should fire that validator (as pointed out in other answers)
Example:
<asp:DropDownList ID="ddlType" runat="server" DataSourceID="sdsType"
DataValueField="ID" DataTextField="Name" AppendDataBoundItems="true">
<asp:ListItem Value="0" Text="--Please Select--" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvType" runat="server" ControlToValidate="ddlType"
InitialValue="0" ErrorMessage="Type required"></asp:RequiredFieldValidator>
<asp:SqlDataSource ID="sdsType" runat="server"
ConnectionString='<%$ ConnectionStrings:TESTConnectionString %>'
SelectCommand="SELECT ID, Name FROM Type"></asp:SqlDataSource>
InitialValue="0" : initial validation will fire when 0th index item is selected in ddl.
<asp:RequiredFieldValidator InitialValue="0" Display="Dynamic" CssClass="error" runat="server" ID="your_id" ValidationGroup="validationgroup" ControlToValidate="your_dropdownlist_id" />
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'