asp.net - Set a DropDownList SelectedValue to a bound field with sometimes null value -
i have situation have gridview populated sql table editing allowed. 1 of fields, have user has select value drop down box in edit template gridview.
<asp:templatefield headertext="department" sortexpression="department"> <edititemtemplate> <asp:dropdownlist id="ddleditdepartment" runat="server" selectedvalue='<%# bind("department") %>'> <asp:listitem>department 1</asp:listitem> <asp:listitem>department 2</asp:listitem> <asp:listitem>department 3</asp:listitem> </asp:dropdownlist> </edititemtemplate> <itemtemplate> <asp:label id="lbldepartment" runat="server" text='<%# bind("department") %>'></asp:label> </itemtemplate> </asp:templatefield>
if select "edit" record in field has value stored in table it, no problem. however, if select "edit" , there no value in table yet, following error
'ddleditdepartment' has selectedvalue invalid because not exist in list of items. parameter name: value
how can account blank or null values in situation , make work?
you're binding dropdownlist field named "department" in gridview's datasource:
selectedvalue='<%# bind("department") %>'
it's kind of hack, modify query populates department field of gridview account nulls (by coalescing them out):
select coalesce(department, "no department entered") department yourtable
and add kind of "default" option in dropdown:
<asp:listitem>no department entered</asp:listitem>
Comments
Post a Comment