GridView控件编辑状态下的DropDownList控件选中原值

如果DropDownList控件的数据源可以直接调用某个方法就能绑定的话,可以在HTML中对DropDownList进行原值定位
 HTML:
< asp:TemplateField  HeaderText ="专业" >
  
< EditItemTemplate >
    
< asp:DropDownList  id ="ddlMajorBind"  runat ="server"  DataTextField ="MajorName"  DataValueField ="MajorID"  DataSource ="<%#bindMajor() %>"  SelectedValue ='<%#  Bind("MajorID") % > '> </ asp:DropDownList >  
  
</ EditItemTemplate >
  
< ItemTemplate >
    
< asp:Label  id ="Label3"  runat ="server"  Text ='<%#  Bind("MajorName") % > '> </ asp:Label >    
  
</ ItemTemplate >
  
< ItemStyle  HorizontalAlign ="Center"   />
</ asp:TemplateField >
C#:
public  DataSet bindMajor()
    {
        
return   new  MajorBLL().SelectTheMajorByDeptID(Session[ " DeptID " ].ToString());
    }
如果DropDownList控件的数据源不能直接调用某个方法绑定,必须在RowDataBind事件中进行绑定的话,原值定位也需要在此事件中完成
  if  (e.Row.RowType  ==  DataControlRowType.DataRow)
        {
            
// GridView控件的编辑状态下下拉列表选中原值

            
if  (e.Row.FindControl( " ddlMajorBind " !=   null   &&  e.Row.FindControl( " ddlDirectionBind " !=   null )
            {
                DropDownList ddlMajor 
=  (DropDownList)e.Row.FindControl( " ddlMajorBind " );
                DropDownList ddlDirection 
=  (DropDownList)e.Row.FindControl( " ddlDirectionBind " );
                ddlDirection.DataSource 
=  bindDirection(ddlMajor.SelectedValue);
                ddlDirection.DataTextField 
=   " DirectionName " ;
                ddlDirection.DataValueField 
=   " DirectionId " ;
                ddlDirection.DataBind();
                ddlDirection.Items.Insert(
0 new  ListItem( " " "" ));
                ddlDirection.SelectedValue 
=  DataBinder.Eval(e.Row.DataItem,  " DirectionID " ).ToString();//原值定位
            }
        } 
    }

你可能感兴趣的:(GridView)