c# - Required Field Validator in ASP.NET not catching Inital Value in DropDownList when set in Codebehind -
i started populating dropdownlist's items table in database. ever since removed hard-coded items , started using auto-populated items requiredfieldvalidator doesn't seem notice initalvalue still selected when press submit.
i'm placing initial value "select genre" after pull items table.
here dropdownlist , validator:
<strong>genre:</strong> <asp:dropdownlist id="ddlgenres" runat="server"></asp:dropdownlist> <asp:requiredfieldvalidator id="rfvgenres" runat="server" controltovalidate="ddlgenres" initialvalue="select genre" errormessage="you must pick genre." forecolor="red"></asp:requiredfieldvalidator>
here code code behind i'm using populate dropdownlist:
//code outside of method static string connstring = system.configuration.configurationmanager.connectionstrings["defaultconnection"].connectionstring; sqlconnection conn = new sqlconnection(connstring); private void populateddlgenres() { try { sqlcommand cmdgenre = new sqlcommand(); cmdgenre.commandtext = "select * genre"; cmdgenre.connection = conn; conn.open(); datatable dt = new datatable(); dt.load(cmdgenre.executereader()); conn.close(); ddlgenres.datasource = dt; ddlgenres.datatextfield = "genretext"; ddlgenres.datavaluefield = "genrevalue"; ddlgenres.databind(); } catch (exception ex) { // handle error } ddlgenres.items.insert(0, new listitem("select genre", "0")); }
you put textfield on initialvalue.
initialvalue="0" solve it
Comments
Post a Comment