62,046
社区成员
发帖
与我相关
我的任务
分享
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="UserID" OnRowDataBound="gv_RowDataBound"
OnRowEditing="gv_RowEditing" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowUpdating="gv_RowUpdating" >
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="UserID" HeaderText="登陆名" ReadOnly="true" />
<asp:TemplateField HeaderText="密码">
<EditItemTemplate>
<asp:TextBox ID="tbPwd" runat="server" Text='<%# Bind("UserPwd") %>' Width="300px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbPwd" runat="server" Text='<%# Bind("UserPwd") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="名字">
<EditItemTemplate>
<asp:TextBox ID="tbName" runat="server" Text='<%# Bind("UserName") %>' Width="300px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbName" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="学院">
<EditItemTemplate>
<asp:DropDownList ID="ddlDeptCur" runat="server"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbDeptName" runat="server" Text='<%# Bind("DeptID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RoleID" HeaderText="角色" ReadOnly="true" />
</Columns>
</asp:GridView>
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlDeptCur = (DropDownList)e.Row.FindControl("ddlDeptCur");
ddlDeptCur.DataValueField = "DeptID";
ddlDeptCur.DataTextField = "DeptName";
ddlDeptCur.DataSource = deptList.ToList();
ddlDeptCur.DataBind();
ddlDeptCur.SelectedValue = DataBinder.Eval(e.Row.DataItem, "DeptID").ToString();
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
BindData();
DropDownList ddlDeptCur = (DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddlDeptCur");
//res_Dept deptAll = new res_Dept();
//deptAll.DeptID = "00";
//deptAll.DeptName = "全部";
//List<res_Dept> delist = deptList.ToList();
//delist.Insert(0, deptAll);
ddlDeptCur.DataValueField = "DeptID";
ddlDeptCur.DataTextField = "DeptName";
ddlDeptCur.DataSource = deptList.ToList();
ddlDeptCur.DataBind();
ddlDeptCur.SelectedValue = gv.DataKeys[e.NewEditIndex].Values[1].ToString();
}
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="UserID" OnRowDataBound="gv_RowDataBound"
OnRowEditing="gv_RowEditing" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowUpdating="gv_RowUpdating" >
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="学院">
<EditItemTemplate>
<asp:DropDownList ID="ddlDeptCur" runat="server"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbDeptName" runat="server" Text='<%# Bind("DeptID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RoleID" HeaderText="角色" ReadOnly="true" />
</Columns>
</asp:GridView>
这是前台, <EditItemTemplate>
<asp:DropDownList ID="ddlDeptCur" runat="server"></asp:DropDownList>
</EditItemTemplate>
在这里面,上面的可能不清楚,加了个颜色,结果显示不出来,哈哈。