博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
带下拉框的GridView的OnRowEditing
阅读量:7237 次
发布时间:2019-06-29

本文共 4512 字,大约阅读时间需要 15 分钟。

===========================aspx

<asp:GridView runat="server" ID="grvMobile" AutoGenerateColumns="False" Width="100%"

                                OnRowCancelingEdit="grvMobile_RowCancelingEdit" OnRowDeleting="grvMobile_RowDeleting"
                                OnRowEditing="grvMobile_RowEditing" OnRowUpdating="grvMobile_RowUpdating" OnRowDataBound="grvMobile_RowDataBound">
                                <Columns>
                                    <asp:TemplateField HeaderText="姓名">
                                        <ItemStyle Width="100" />
                                        <ItemTemplate>
                                            <asp:Label ID="userName" runat="server" Text='<%#Eval("姓名") %>'></asp:Label>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:TextBox ID="txt_Name" runat="server" Text='<%#Eval("姓名") %>' Width="90%" />
                                        </EditItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="单位">
                                        <ItemTemplate>
                                            <%#Eval("单位")%></ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:HiddenField ID="Hidd_Comp" runat="server" Value='<%#Eval("单位")%>' />
                                            <asp:DropDownList ID="ddl_Comp" runat="server" Width="80%">
                                            </asp:DropDownList>
                                        </EditItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="号码">
                                        <ItemStyle Width="100" />
                                        <ItemTemplate>
                                            <asp:Label ID="phoneNum" runat="server" Text='<%#Eval("手机号")%>'></asp:Label>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:HiddenField ID="Hidd_OldPhoneNum" runat="server" Value='<%#Eval("手机号") %>' />
                                            <asp:TextBox ID="txt_PhoneNum" runat="server" Text='<%#Eval("手机号") %>' Width="90%"
                                                οnkeyup="this.value=this.value.replace(/\D/g,'')" />
                                        </EditItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="状态">
                                        <ItemStyle Width="60" />
                                        <ItemTemplate>
                                            <%#Eval("状态")%></ItemTemplate>
                                        <EditItemTemplate>
                                            <%#Eval("状态")%></EditItemTemplate>
                                    </asp:TemplateField>
                                    <asp:CommandField HeaderText="操作" ItemStyle-Width="100" ShowEditButton="true" UpdateText="确认"
                                        CancelText="取消" DeleteText="&lt;span οnclick=&quot;JavaScript:return confirm('是否确定删除该数据?')&quot;&gt;删除&lt;/span&gt; " />
                                    <asp:CommandField HeaderText="操作" ItemStyle-Width="100" ShowDeleteButton="true" UpdateText="确认"
                                        CancelText="取消" DeleteText="&lt;span οnclick=&quot;JavaScript:return confirm('是否确定删除该数据?')&quot;&gt;删除&lt;/span&gt; " />
                                </Columns>
                            </asp:GridView>

 

 

 

============================cs

  protected void grvMobile_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

  {
            this.grvMobile.EditIndex = -1;
            this.grvMobile.DataSource = ViewState["dt"] as DataTable;
            this.grvMobile.DataBind();
   }

   protected void grvMobile_RowDeleting(object sender, GridViewDeleteEventArgs e)

   {
            this.grvMobile.EditIndex=-1;
            MobileBLL bll = new MobileBLL();
            DataStruct ds = new DataStruct();
            string phoneNum = (this.grvMobile.Rows[e.RowIndex].FindControl("phoneNum") as Label).Text;
            ds = bll.DeleteMobile(phoneNum);
            if (ds.IsSuc)
            {
                string userName = (this.grvMobile.Rows[e.RowIndex].FindControl("userName") as Label).Text;
                PagerMobile.CurrentPageIndex = 1;
                BindMobile();
                showMessageBox("用户" + userName + "已成功删除");
            }
            else
            {
                showMessageBox("用户" + (this.grvMobile.Rows[e.RowIndex].FindControl("userName") as Label).Text + "删除失败");
            }
            ds = null;
            bll = null;
   }

 protected void grvMobile_RowEditing(object sender, GridViewEditEventArgs e)

  {
            this.grvMobile.EditIndex = e.NewEditIndex;
            this.grvMobile.DataSource = ViewState["dt"] as DataTable;
            this.grvMobile.DataBind();
  }

 

 protected void grvMobile_RowUpdating(object sender, GridViewUpdateEventArgs e)

 {
            MobileBLL bll = new MobileBLL();
            DataStruct ds = new DataStruct();
            string userName = (this.grvMobile.Rows[e.RowIndex].FindControl("txt_Name") as TextBox).Text;
            string uintName = (this.grvMobile.Rows[e.RowIndex].FindControl("ddl_Comp") as DropDownList).SelectedValue;
            string phoneNum = (this.grvMobile.Rows[e.RowIndex].FindControl("txt_PhoneNum") as TextBox).Text;
            string oldPhoneNum = (this.grvMobile.Rows[e.RowIndex].FindControl("Hidd_OldPhoneNum") as HiddenField).Value.Trim();
            ds = bll.ModifyMobile(userName, uintName, phoneNum, oldPhoneNum);
            if (ds.IsSuc)
            {
                e.Cancel = true;
                this.grvMobile.EditIndex = -1;
                this.grvMobile.DataSource = ViewState["dt"] as DataTable;
                this.grvMobile.DataBind();
                showMessageBox(ds.Message);
            }
            else
            {
                showMessageBox(ds.Message);
            }
            BindMobile();
 }

 

 protected void grvMobile_RowDataBound(object sender, GridViewRowEventArgs e)

  {
            CommonBLL bll = new CommonBLL();
            DataStruct ds = bll.QueryAllMobileUnit();
            DropDownList list = e.Row.FindControl("ddl_Comp") as DropDownList;

 

            if (ds.IsSuc && list != null)

            {
                list.DataSource = ds.Data;
                list.DataTextField = "单位";
                list.DataValueField = "单位";
                list.DataBind();

                list.SelectedValue = (e.Row.FindControl("Hidd_Comp") as HiddenField).Value;

            }
}

分类: 
本文转自Lei Zhang的博客博客园博客,原文链接:http://www.cnblogs.com/threestone/archive/2010/08/30/1812806.html,如需转载请自行联系原作者
你可能感兴趣的文章
面向对象三大基本特性,五大基本原则
查看>>
更改窗口图标并将其显示在任务栏
查看>>
包含的语句
查看>>
正则表达式-匹配标点符号
查看>>
osworkflow descriptor 解析 重要概念
查看>>
Edmonds_Karp 算法 (转)
查看>>
第一节 接口概述 [转贴]
查看>>
C# Attribute 用法备忘
查看>>
数据结构学习笔记(5.线性表之双向循环链表)
查看>>
智能家居趋势
查看>>
[Leetcode] Pow(x, n)
查看>>
关于Microsoft Speech SDK 中TTS的研究 [转]
查看>>
两个与后台有关的回调处理
查看>>
idhttp.post方式 调用datasnap rest 远程方法
查看>>
Gulp快速入门
查看>>
TClientDataSet的 fastscript封装
查看>>
有用的国外开源项目网址
查看>>
DataGridView 绑定DataTable方式编辑保存的bug?
查看>>
ComboBox 使用数据绑定时 Sorted 属性的bug
查看>>
BZOJ 3172 单词(ac自动机)
查看>>