I Have a dropdownlist in an aspx page, I am populating the dropdownlist using a datatable. How can i apply a requiredfield validator to this dropdownlist.? Plz help me
You have to set the InitialValue for the selected item's Value that is selected first:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="DropDownList1"
InitialValue="-- Please select --"
ErrorMessage="Please select something" />
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server">
<asp:ListItem>-- Please select --</asp:ListItem>
</asp:DropDownList>
Related
Morning All,
I have a dropdown list that is populated by an SQLSataSource. I wish to make this dropdown list a required field and need to add add a default item like -Please Select- at the top of this list to prompt the user to make a selection.
Here is my code...
<asp:DropDownList ID="ddOwner" runat="server" DataSourceID="OwnersList"
DataTextField="UserFullName" DataValueField="UserFullName" Height="24px"
Width="125px">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvOwner"
ControlToValidate="ddOwner"
ErrorMessage="Please assign an Owner"
Text="*"
runat="server" Display="None"/>
Can i add some code into the 'Initial value' item to set the -Please select- default item?
Regards Betty
Add an unbound item:
<asp:DropDownList ID="ddOwner" runat="server" DataSourceID="OwnersList"
DataTextField="UserFullName" DataValueField="UserFullName" Height="24px"
AppendDataBoundItems="true"
Width="125px">
<asp:ListItem Text="-Please Select-" Value="" />
</asp:DropDownList>
You can try with after your DataBind Method
...
ddOwner.DataBind();
ddOwner.Items.Add("Please select- default item?");
I have Dropdownlist whose value field and text field are bind at runtime.
it has --select-- as first item with a value of 0
and the rest of the values are bind at runtime.
I have given validaton group for both the control and the validator as "g1"
and Intialvalue=0
But still the page is posting back even if I select --select-- option.
<asp:DropDownList AutoPostBack="true" CssClass="dropdown" ValidationGroup="g1"
ID="ddlReportType" runat="server"
OnSelectedIndexChanged="ddlReportType_SelectedIndexChanged"></asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="ddlReportType" ID="RequiredFieldValidator1"
ValidationGroup="g1" CssClass="errormesg" ErrorMessage="Please select a type"
InitialValue="0" runat="server" Display="Dynamic">
</asp:RequiredFieldValidator>
And code Behind to Bind the Dropdown
ddlReportType.Items.Clear();
ddlReportType.DataSource = dt.Tables[0];
ddlReportType.DataTextField = "ReportType";
ddlReportType.DataValueField = "ReportTypeID";
ddlReportType.DataBind();
ddlReportType.Items.Insert(0, new ListItem("--Select--", "0"));
//ddlReportType.Items[0].Value = "0";
ddlReportType.SelectedIndex = 0;
<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID" Display="Dynamic"
ValidationGroup="g1" runat="server" ControlToValidate="ControlID"
Text="*" ErrorMessage="ErrorMessage"></asp:RequiredFieldValidator>
Here use asp:CompareValidator, and compare the value to "select" option.
Use Operator="NotEqual" ValueToCompare="0" to prevent the user from submitting the "select".
<asp:CompareValidator ControlToValidate="ddlReportType" ID="CompareValidator1"
ValidationGroup="g1" CssClass="errormesg" ErrorMessage="Please select a type"
runat="server" Display="Dynamic"
Operator="NotEqual" ValueToCompare="0" Type="Integer" />
When you do above, if you select the "select " option from dropdown it will show the ErrorMessage.
I was struggling with this for a few days until I chanced on the issue when I had to build a new Dropdown. I had several DropDownList controls and attempted to get validation working with no luck. One was databound and the other was filled from the aspx page. I needed to drop the databound one and add a second manual list. In my case Validators failed if you built a dropdown like this and looked at any value (0 or -1) for either a required or compare validator:
<asp:DropDownList ID="DDL_Reason" CssClass="inputDropDown" runat="server">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>Expired</asp:ListItem>
<asp:ListItem>Lost/Stolen</asp:ListItem>
<asp:ListItem>Location Change</asp:ListItem>
</asp:DropDownList>
However adding the InitialValue like this worked instantly for a compare Validator.
<asp:ListItem Text="-- Select --" Value="-1"></asp:ListItem>
Add InitialValue="0" in Required field validator tag
<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID"
Display="Dynamic" ValidationGroup="g1" runat="server"
ControlToValidate="ControlID"
InitialValue="0" ErrorMessage="ErrorMessage">
</asp:RequiredFieldValidator>
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>
I have a dropdownlist in gridview and bind grid view from a function in code behind for the dropdownlist.
The problem is the dropdownlist is in edittemplate and the selected value is the id in the same celle when the is dropdownlist when the row is not editing.
How I can display in editing the null value????
Add an item to your dropdownlist that represent the null value.
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("CurrencyType") %>' AppendDataBoundItems="true">
<asp:ListItem Value="">Not selected</asp:ListItem>
</asp:DropDownList>
You can put Your business logic and check whether the selected value is not null and then you can use your data items that would symbolize null when selected.
<asp:DropDownList ID="ddlitems" runat="server" SelectedValue='<%# Bind("mydata") %>' AppendDataBoundItems="true">
<asp:ListItem Value="">select</asp:ListItem>
</asp:DropDownList>
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" />