GridView的使用和嵌套DropDownList

在GridView中 经常要嵌套DropDownList dropDownList也需要设置默认选择值

                    <asp:TemplateField>
                        
<HeaderTemplate>实验班级</HeaderTemplate>
                        
<ItemTemplate>
                            
<%#DataBinder.Eval(Container.DataItem,"className"%>
                        
</ItemTemplate>
                        
<EditItemTemplate>
                            
<asp:DropDownList ID="DpClass" runat="server" DataSource="<%#DB_class.ReturnAllClassInfo() %>" DataTextField="className" DataValueField="classID"></asp:DropDownList>
                        
</EditItemTemplate>
                    
</asp:TemplateField>
其中DB_Class.ReturnAllClassInfo返回一个DataSet 用于绑定
        if ((e.Row.RowState & DataControlRowState.Edit) != 0)
        
{
            
//设置编辑状态时文本框的大小
            TextBox tb = e.Row.Cells[4].Controls[0as TextBox;
            tb.Width 
= new Unit(100);
            tb 
= e.Row.Cells[5].Controls[0as TextBox;
            tb.Width 
= new Unit(100);
            
//设置DropDownList选中项..
            DataRowView dr = e.Row.DataItem as DataRowView;//获取当前的数据行.
            DropDownList dpClass = e.Row.FindControl("DpClass"as DropDownList;
            dpClass.SelectedIndex 
= dpClass.Items.IndexOf(dpClass.Items.FindByText(dr["className"].ToString()));
            DropDownList dpLaboratory 
= e.Row.FindControl("DpLaboratory"as DropDownList;
            dpLaboratory.SelectedIndex 
= dpLaboratory.Items.IndexOf(dpLaboratory.Items.FindByText(dr["LaboratoryName"].ToString()));
        }
此代码用在RowDataBound中
DataRowView dr=e.Row.DataItem as DataRowView 获取当前的数据行
DropDownList.Item.IndexOf(DropDownList.Items.FindByText("string"))设置默认选项
posted @ 2008-02-26 13:52  孤城浪子  阅读(595)  评论(0编辑  收藏  举报
博客园,让我更上一层楼