错误:dropdownlist有一个无效 SelectedValue,因为它不在项目列表中。

今天遇到这样一个问题:
在GridView里绑定一个dropdownlist。dropdownlist数据从字典表中获取。
前台代码:
                <asp:GridView ID="gv_List_Detail" runat="server" AutoGenerateColumns="False" Width="100%"
                    DataKeyNames="ID,ProductID,ColorID,ProductName,SortID"
                    ShowFooter="True" OnRowDataBound="gv_List_Detail_RowDataBound" >
                    <Columns>
                        <asp:TemplateField HeaderText="颜色">
                            <ItemTemplate>
                                    <asp:DropDownList ID="ddl_Color" runat="server" DataTextField="Name" DataValueField="ID"
                                    Width="60px" SelectedValue='<%#Bind("ColorID") %>'>
                            </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
后台绑定数据源:
        foreach (GridViewRow gr in gv_List_Detail.Rows)
        {
            ((DropDownList)gr.FindControl("ddl_Color")).DataSource = CustomDictionary.Get_DP_JXC_PDT_Color();
            ((DropDownList)gr.FindControl("ddl_Color")).DataBind();
            ((DropDownList)gr.FindControl("ddl_Color")).Items.Insert(0, new ListItem("请选择...", "0"));
        }
并赋值:            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
 
                dt.Rows[i]["ColorID"] = ((DropDownList)this.gv_List_Detail.Rows[i].FindControl("ddl_Color")).SelectedValue;}
执行就发生了错误:dropdownlist有一个无效 SelectedValue,因为它不在项目列表中。
仔细的看了代码,再看这个错误,估计是因为没有绑定dropdownlist的时候,就已经给它赋值引起的。
可是不知道怎么修改。
终于解决了,RowDataBound()
正确的代码如下。
前台不赋值:
                <asp:GridView ID="gv_List_Detail" runat="server" AutoGenerateColumns="False" Width="100%"
                    DataKeyNames="ID,ProductID,ColorID,ProductName,SortID"
                    ShowFooter="True" OnRowDataBound="gv_List_Detail_RowDataBound" >
                    <Columns>
                        <asp:TemplateField HeaderText="颜色">
                            <ItemTemplate>
                                    <asp:DropDownList ID="ddl_Color" runat="server" DataTextField="Name" DataValueField="ID"
                                    Width="60px" >
                            </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
后台代码如下:
    protected void gv_List_Detail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl_Color=(DropDownList)e.Row.FindControl("ddl_Color");
            ddl_Color.DataSource = CustomDictionary.Get_DP_JXC_PDT_Color();
            ddl_Color.DataBind();
            ddl_Color.Items.Insert(0, new ListItem("请选择...", "0"));
            int ColorID = 0;
            try
            {
                ColorID = int.Parse(this.gv_List_Detail.DataKeys[e.Row.RowIndex]["ColorID"].ToString());

                ddl_Color.SelectedValue = ColorID.ToString();
            }
            catch { }
        }
       
    }

小结:
GridView..::.RowDataBound 事件
在 GridView 控件中将数据行绑定到数据时发生。

posted on 2008-11-07 12:52  吴文艳  阅读(3590)  评论(1编辑  收藏  举报