GridView内嵌DropDownList操作
生活随笔
收集整理的這篇文章主要介紹了
GridView内嵌DropDownList操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
效果:
?
步驟:
1.先把DropDownList所在的列轉換成TemplateField(模板列)
2.在GridView里添加三個事件OnRowEditing,OnRowCancelingEdit,OnRowUpdating
?? 再在事件上寫上相應的代碼
代碼片段:
ASPX文件:
<asp:GridView ID="gvRequestRoleList" runat="server" AutoGenerateColumns="False" CellPadding="4"
???????? ForeColor="#333333" GridLines="None" AllowPaging="True" OnPageIndexChanging="gvRequestRoleList_PageIndexChanging"
???????? OnRowEditing="gvRequestRoleList_RowEditing" OnRowCancelingEdit="gvRequestRoleList_RowCancelingEdit"
???????? OnRowUpdating="gvRequestRoleList_RowUpdating">
.......
<asp:TemplateField HeaderText="申請權限">
?????????????? <EditItemTemplate>
????????????????? <asp:DropDownList ID="ddlRequestRole" runat="server">
???????????????????? <asp:ListItem Value="Common">普通用戶</asp:ListItem>
???????????????????? <asp:ListItem Value="Intermediate">中級用戶</asp:ListItem>
???????????????????? <asp:ListItem Value="Senior">高級用戶</asp:ListItem>
???????????????????? <asp:ListItem Value="Admin">超級用戶</asp:ListItem>
????????????????? </asp:DropDownList>
?????????????? </EditItemTemplate>
?????????????? <ItemTemplate>
????????????????? <asp:Label ID="Label1" runat="server" Text='<%# Bind("RequestRoleCHName") %>'></asp:Label>
?????????????? </ItemTemplate>
??????????? </asp:TemplateField>
.......
</asp:GridView>
CS文件:
//用戶按"修改權限"(更新)時
?protected void gvRequestRoleList_RowEditing(object sender, GridViewEditEventArgs e)
????? {
???????? gvRequestRoleList.EditIndex = e.NewEditIndex;????????
??? ?? ?? //綁定數據
???????? RequestRoleListDataBind();
????? }
//用戶取消操作時
????? protected void gvRequestRoleList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
????? {
???????? gvRequestRoleList.EditIndex = -1;
??? ?? ?? //綁定數據
???????? RequestRoleListDataBind();
????? }
//更新操作
????? protected void gvRequestRoleList_RowUpdating(object sender, GridViewUpdateEventArgs e)
????? {
//獲得DropDownList 控件
???????? DropDownList ddlRequestRole = gvRequestRoleList.Rows[e.RowIndex].Cells[5].FindControl("ddlRequestRole") as DropDownList;
???????? string roleName = ddlRequestRole.SelectedItem.Value;
???????? Label lblUserName = gvRequestRoleList.Rows[e.RowIndex].Cells[2].FindControl("lblUserName") as Label;
???????? string userName = lblUserName.Text;
??? ?? ?? //更新用戶的角色
???????? ChangeRole(userName, roleName);??? ?? ??
???????
??? ?? ?? //審核用戶的申請
???????? //....(略)
???????? //刷新頁面
??? ?? ?? //....(略)
????? }
?
步驟:
1.先把DropDownList所在的列轉換成TemplateField(模板列)
2.在GridView里添加三個事件OnRowEditing,OnRowCancelingEdit,OnRowUpdating
?? 再在事件上寫上相應的代碼
代碼片段:
ASPX文件:
<asp:GridView ID="gvRequestRoleList" runat="server" AutoGenerateColumns="False" CellPadding="4"
???????? ForeColor="#333333" GridLines="None" AllowPaging="True" OnPageIndexChanging="gvRequestRoleList_PageIndexChanging"
???????? OnRowEditing="gvRequestRoleList_RowEditing" OnRowCancelingEdit="gvRequestRoleList_RowCancelingEdit"
???????? OnRowUpdating="gvRequestRoleList_RowUpdating">
.......
<asp:TemplateField HeaderText="申請權限">
?????????????? <EditItemTemplate>
????????????????? <asp:DropDownList ID="ddlRequestRole" runat="server">
???????????????????? <asp:ListItem Value="Common">普通用戶</asp:ListItem>
???????????????????? <asp:ListItem Value="Intermediate">中級用戶</asp:ListItem>
???????????????????? <asp:ListItem Value="Senior">高級用戶</asp:ListItem>
???????????????????? <asp:ListItem Value="Admin">超級用戶</asp:ListItem>
????????????????? </asp:DropDownList>
?????????????? </EditItemTemplate>
?????????????? <ItemTemplate>
????????????????? <asp:Label ID="Label1" runat="server" Text='<%# Bind("RequestRoleCHName") %>'></asp:Label>
?????????????? </ItemTemplate>
??????????? </asp:TemplateField>
.......
</asp:GridView>
CS文件:
//用戶按"修改權限"(更新)時
?protected void gvRequestRoleList_RowEditing(object sender, GridViewEditEventArgs e)
????? {
???????? gvRequestRoleList.EditIndex = e.NewEditIndex;????????
??? ?? ?? //綁定數據
???????? RequestRoleListDataBind();
????? }
//用戶取消操作時
????? protected void gvRequestRoleList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
????? {
???????? gvRequestRoleList.EditIndex = -1;
??? ?? ?? //綁定數據
???????? RequestRoleListDataBind();
????? }
//更新操作
????? protected void gvRequestRoleList_RowUpdating(object sender, GridViewUpdateEventArgs e)
????? {
//獲得DropDownList 控件
???????? DropDownList ddlRequestRole = gvRequestRoleList.Rows[e.RowIndex].Cells[5].FindControl("ddlRequestRole") as DropDownList;
???????? string roleName = ddlRequestRole.SelectedItem.Value;
???????? Label lblUserName = gvRequestRoleList.Rows[e.RowIndex].Cells[2].FindControl("lblUserName") as Label;
???????? string userName = lblUserName.Text;
??? ?? ?? //更新用戶的角色
???????? ChangeRole(userName, roleName);??? ?? ??
???????
??? ?? ?? //審核用戶的申請
???????? //....(略)
???????? //刷新頁面
??? ?? ?? //....(略)
????? }
轉載于:https://www.cnblogs.com/sinkzephyr/archive/2007/08/20/862594.html
總結
以上是生活随笔為你收集整理的GridView内嵌DropDownList操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 好听的歌曲---爱情转移
- 下一篇: SqlHelper.cs 使用时要注意的