我写了一个gridview嵌套DropDownList 这个DropDownList,但发现有些记录不在1,2,3选项里,造成出现"DropDownList1”有一个无效 SelectedValue,因为它不在项目列表中的错误",所以我想能不能在codeBehind里动态判断一下是否有效,无效再让SelectedValue="请选择",请高人指点,谢谢!代码如下:<asp:GridView ID="GridView1" ...>
                        <asp:TemplateField HeaderText="所属类别"> 
                    <ItemTemplate> 
                        <asp:DropDownList ID="DropDownList1" runat="server"  DataField="songtype" SelectedValue =<%# Bind("songtype")%> 
                            AutoPostBack="true" >
                         
                        <asp:ListItem Value="1">流行翻唱</asp:ListItem>
                        <asp:ListItem Value="2" >独奏古典</asp:ListItem>
                            <asp:ListItem Value="3">原创弹唱</asp:ListItem>
                        </asp:DropDownList>
                       
                    </ItemTemplate> 
                </asp:TemplateField>
...  .... </asp:GridView>
 

解决方案 »

  1.   

    在后台代码中加上 DropDownList1.Items.Insert(0, new ListItem("-请选择-", "0"));
      

  2.   

    谢谢两位,加这个不行啊 DropDownList1.Items.Insert(0, new ListItem("-请选择-", "0"));如果非要这样加的话,要加多两行才行,主要是4,5原来数据有,但现在不用了!
    DropDownList1.Items.Insert(0, new ListItem("-请选择-", "0"));DropDownList1.Items.Insert(0, new ListItem("-请选择-", "4"));DropDownList1.Items.Insert(0, new ListItem("-请选择-", "5"));
      

  3.   

     <asp:DropDownList ID="DropDownList1" runat="server"  DataField="songtype" SelectedValue =<%# aa(Bind("songtype"))%> //cs 页面写方法
    public string aa(object obj)
    {
       //logic....
       return ...
    }