1、页面代码
<asp:TemplateField HeaderText="等级">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# FormatUserlevel(Eval("User_UserLevel"))%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
//这个label的作用是为了在后台控制 当你点击编辑按钮的时 DropDownList 的选中值, <asp:Label ID="Label7" runat="server" Text='<%# Eval("User_UserLevel")%>' Visible="false"></asp:Label>
<asp:DropDownList ID="ddl_userLevel" runat="server" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# FormatUserlevel(Eval("User_UserLevel"))%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
//这个label的作用是为了在后台控制 当你点击编辑按钮的时 DropDownList 的选中值, <asp:Label ID="Label7" runat="server" Text='<%# Eval("User_UserLevel")%>' Visible="false"></asp:Label>
<asp:DropDownList ID="ddl_userLevel" runat="server" >
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
将此列设为模版,在现实数据的时候是以label形式,当点击控件自带的编辑按钮的时候就是以DropDownList形势显示
2、后台代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
SystemMemberLevel bll = new SystemMemberLevel();
IList<SystemMemberLevelModel> list = bll.GetAllList();
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = ((DropDownList)e.Row.FindControl("ddl_userLevel"));
Label lb = ((Label)e.Row.FindControl("Label7"));
if (ddl != null)
{
ddl.DataSource = list;
ddl.DataTextField = "name";
ddl.DataValueField = "id";
ddl.SelectedValue = lb.Text;
ddl.DataBind();
}
}
}
{
SystemMemberLevel bll = new SystemMemberLevel();
IList<SystemMemberLevelModel> list = bll.GetAllList();
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = ((DropDownList)e.Row.FindControl("ddl_userLevel"));
Label lb = ((Label)e.Row.FindControl("Label7"));
if (ddl != null)
{
ddl.DataSource = list;
ddl.DataTextField = "name";
ddl.DataValueField = "id";
ddl.SelectedValue = lb.Text;
ddl.DataBind();
}
}
}