带下拉框的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;
            }
}

分类:  Asp.net

本文转自Lei Zhang的博客博客园博客,原文链接:http://www.cnblogs.com/threestone/archive/2010/08/30/1812806.html,如需转载请自行联系原作者
目录
相关文章
|
数据库
利用DropDownList实现下拉
利用DropDownList实现下拉
129 0
利用DropDownList实现下拉
|
C#
C# DataGridview控件自动下拉到最后一行
有时候使用DataGridView难免会在最后插入一条数据,如果插入的数据超过滚动条显示的行数,那么默认情况下不会显示到最后一行。增加以下代码一直将滚动条拉倒最低。 this.dataGridView1.
2627 0
|
.NET 开发框架 Go
GridView控件自定义分页的实现
前人栽树,后人乘凉,话不多说,代码如下:     实现方式一: .aspx: [c-sharp] view plain copy <form id="form1" runat="server">       <table style="width: 605px">         .
1432 0