zoukankan      html  css  js  c++  java
  • 带下拉框的GridView的OnRowEditing

    ===========================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%"
                                                    onkeyup="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 onclick=&quot;JavaScript:return confirm('是否确定删除该数据?')&quot;&gt;删除&lt;/span&gt; " />
                                        <asp:CommandField HeaderText="操作" ItemStyle-Width="100" ShowDeleteButton="true" UpdateText="确认"
                                            CancelText="取消" DeleteText="&lt;span onclick=&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;
                }
    }

  • 相关阅读:
    不使用spring的情况下用java原生代码操作mongodb数据库的两种方式
    log4j配置输出到多个日志文件
    HIDL概述【转】
    Android HIDL学习(2) ---- HelloWorld【转】
    第2课第7节_Java面向对象编程_内部类_P【学习笔记】
    SDM439平台出现部分机型SD卡不能识别mmc1: error -110 whilst initialising SD card【学习笔记】
    第2课第6节_Java面向对象编程_包和权限_P【学习笔记】
    Android A/B System OTA分析(一)概览【转】
    查看DDR的频率【学习笔记】
    音频参数路径【学习笔记】
  • 原文地址:https://www.cnblogs.com/threestone/p/1812806.html
Copyright © 2011-2022 走看看