Reputation: 71
I searched a lot but nothing realy helped me. I am beginner and please bear any ignorance
I have a dropdown list in my aspx page. The source of the dropdown is from a sqlsource that I have it in my code behind file. I wanted to add a static item to the top of the dropdown and I was able to add it with below line
reportparameter.Items.Insert(0, "-------SELECT----------");
how do I set a required field validator on this dropdown with the first value from the code behind. I tried different things and I get an conversion error whenever i submit the page.
Upvotes: 1
Views: 6560
Reputation: 460158
I assume that reportparameter
is the DropDownList
.
You can use the AppendDataBoundItems
property to tell ASP.NET that the DataSource should be appended to the "static" item(s).
You can use the RequiredFieldValidator
's InitalValue
property to tell it that this counts as not selcted. In your casse you need to set it to 0.
<asp:DropDownList id="reportparameter"
AppendDataBoundItems="True"
runat="server">
<asp:ListItem Selected="True" Value="0">-------SELECT----------</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
InitialValue="0"
ControlToValidate="reportparameter"
ErrorMessage="Required field!"
runat="server"/>
Upvotes: 5