GridView中DropDownList的应用

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

GridView中DropDownList的应用
在GridView中添加一个字段,并且需要在编辑的时候以DropDownList的形式展现出来,同时要求展示的时候要求显示的数据保存不变。

首先,点击Gridview右上角Gridview 任务(Gridview Tasks),选择编辑列(Edit Colums),将某字段(比如Place)转化为模板列(Convert this field into a TemplateField )。

然后,进入前台代码 xxx.aspx页面,对该字段的列进行编辑:
[html]view plaincopyprint?
1.<%------------------------------------ Place------------------------------------%>
2.<asp:TemplateField HeaderText="Place">
3.<EditItemTemplate>
4.<asp:DropDownList ID="dropdlst_p" runat="server">
5.<asp:ListItem Value="1">A窗口</asp:ListItem>
6.<asp:ListItem Value='2'>B窗口</asp:ListItem>
7.<asp:ListItem Value='3'>C窗口</asp:ListItem>
8.<asp:ListItem Value='4'>D窗口</asp:ListItem>
9.<asp:ListItem Value='5'>E窗口</asp:ListItem>
10.<asp:ListItem Value='6'>F窗口</asp:ListItem>
11.</asp:DropDownList><asp:Label ID="lblHidden" runa t="server" Text='<%# Bind("Place") %>' Visible="false"></asp:L abel>
12.</EditItemTemplate>
13.<ItemTemplate>
14.<asp:Label ID="lblP" runat="server" Text='<%# Bind(" Place") %>'></asp:Label>
15.</ItemTemplate>
16.<HeaderStyle Font-Bold="True" Font-
Size="9pt" Width="200px" />
17.<ItemStyle Font-
Size="9pt" Width="400px" Wrap="False" />
18.</asp:TemplateField>
对上面两个Label说明:第一个Label将在后面用到取值,作为中间变量的作用,不需要显示出来,所以隐藏了;而第二个Label的就是GridView显示的值,很明显都是从数据库读出来的数据,是一样的值。

接着,进入Gridview的编辑操作(GridView1_RowEditing)事件:
[csharp]view plaincopyprint?
1.protected void GridView1_RowEditing(object sender, Grid ViewEditEventArgs e)
2.{
3.GridView1.EditIndex = e.NewEditIndex;//进入编辑状态
4.BindGV();//对Gridview绑定数据
5.DropDownList dropdl=(DropDownList)GridView1.Rows[e. NewEditIndex].FindControl("dropdlst_p");
6.string selectedText = ((Label)GridView1.Rows[e.NewEditIn dex].FindControl("lblHidden")).Text;//取出DropDownList "dropdlst_p"原先显示的数据
7.for (int i = 0; i < dropdl.Items.Count; i++)//遍历dropdlst_p中的所有数据
8.{
9.if (dropdl.Items[i].T ext == selectedText)
10.{
11.
12.dropdl.Items[i].Selected = true;//选中dropdlst_p的原先显示的数据项
13.}
14.}
15.}
最后,对数据进行更新(GridView1_RowUpdating)
[csharp]view plaincopyprint?
1.protected void GridView1_RowUpdating(object sender, Gr idViewUpdateEventArgs e)
2.{
3.string script = "";
4.string bookingID = GridView1.DataKeys[e.RowIndex].Valu
e.ToString();
5.DropDownList ddl = (DropDownList)GridView1.Rows[e.Ro wIndex].FindControl("dropdlst_p");
6.if (DBHandler.UpdateCPList(bookingID, ddl.SelectedValue, userID))//此方法作用是判断是否更新成功,取出droplst_p的选择项进行判断
7.{
8.script = "alert('Update Successfully!');";
9.}
10.else
11.{
12.script = "alert('Update Failed!');";
13.}
14.ScriptManager.RegisterStartupScript(this, typeof(Page) , "startup", script, true);
15.gridRpt.EditIndex = -1;//取消编辑状态
16.BindGV();//重新绑定GridView数据源
17.}。

相关文档
最新文档