日期:2014-05-17  浏览次数:20817 次

gridview中嵌套dropdownlist后,给dropdownlist绑定数据及赋值的问题
gridview里的dropdownlist是个模板列,每次打开页面时用RowDataBound给dropdownlist绑定数据
HTML code

<asp:TemplateField HeaderText="订货途径">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>



if (!IsPostBack)里绑定数据
C# code

            sqloldArrival.Connection = srcConnection;
            sqloldArrival.CommandText = "select DHWays 订货途径 from xxx Where SheetID='" + sheetid + "' ";
            sqloldArrival.CommandType = CommandType.Text;
            daoldArrival.SelectCommand = sqloldArrival;
            daoldArrival.Fill(dtoldArrival);
            GvArrivalDetail.DataSource = dtoldArrival;
            GvArrivalDetail.DataBind();
            for (int i = 0; i < GvArrivalDetail.Rows.Count; i++)
            {
                DropDownList ddl = (DropDownList)GvArrivalDetail.Rows[i].Cells[10].FindControl("DropDownList1");
                ddl.Enabled = false;//将ddl的状态设为禁用
            }



RowDataBound:
C# code

        if (((DropDownList)e.Row.FindControl("DropDownList1")) != null)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("画册", "0"));
            ddl.Items.Insert(1, new ListItem("样品", "1"));
            ddl.Items.Insert(2, new ListItem("其他", "2"));
         
        }



这样做,每次打开页面时,因为RowDataBound的关系,dropdownlist都会被重建,如果我将这个ddl从“画册”改为“样品”,点更新后因为RowDataBound的关系,dropdownlist还是会被重建,显示的还是画册而不是我改动的样品。
请问这个要怎么改呢?
另外,如果某一行是“样品”,在打开页面时如何让这一行的ddl显示成样品呢?

------解决方案--------------------
http://www.soaspx.com/dotnet/asp.net/Control/control_20091123_1732.html
这个应该对你有帮助
不要用RowDataBound来绑定数据,写到html里