GridView数据修改、删除,修改控件TextBox和DropDownList,删除ShowDeleteButton=true提示删除确认[asp.net基础]

GridView列表中直接在当前行可修改、删除是很方便的,类似与winform的直观。本示例就是借助GridView自带修改删除功能,实现TextBox和DropDownList控件的结合,DropDownList的数据来自数据库,并选中可选项。ShowDeleteButton=true实现删除时,使用DeleteText实现删除前的提示框确认。

图示:

GridView数据修改、删除,修改控件TextBox和DropDownList,删除ShowDeleteButton=true提示删除确认[asp.net基础]

编辑前

GridView数据修改、删除,修改控件TextBox和DropDownList,删除ShowDeleteButton=true提示删除确认[asp.net基础]

编辑后的编辑图示

GridView数据修改、删除,修改控件TextBox和DropDownList,删除ShowDeleteButton=true提示删除确认[asp.net基础]

删除确认对话框


gridviewUp.aspx代码:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridviewUp.aspx.cs" Inherits="gridviewUp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .bubufxPagerCss table
        {
            text-align: center;
            margin: auto;
        }
        .bubufxPagerCss table td
        {
            border: 0px;
            padding: 5px;
        }
        .bubufxPagerCss td
        {
            border-left: #ffffff 3px solid;
            border-right: #ffffff 3px solid;
            border-bottom: #ffffff 3px solid;
        }
        .bubufxPagerCss a
        {
            color: #231815;
            text-decoration: none;
            padding: 3px 6px 3px 6px;
            margin: 0 0 0 4px;
            text-align: center;
            border: 1px solid #ac1f24;
        }
        .bubufxPagerCss span
        {
            color: #fefefe;
            background-color: #ac1f24;
            padding: 3px 6px 3px 6px;
            margin: 0 0 0 4px;
            text-align: center;
            font-weight: bold;
            border: 1px solid #ac1f24;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True"
            EmptyDataText="没有数据记录!!" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit"
            OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting"
            OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
            <Columns>
                <asp:BoundField HeaderText="编号" DataField="ID" ReadOnly="true" />
                <asp:BoundField HeaderText="姓名" FooterText="姓名" DataField="name" SortExpression="name"
                    ReadOnly="true" />
                <asp:TemplateField HeaderText="职位">
                    <ItemTemplate>
                        <asp:HiddenField ID="hfID" runat="server" Value=’<%# Eval("ID") %>’ />
                        <%# Eval("zhiweiName")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:HiddenField ID="hfID" runat="server" Value=’<%# Eval("ID") %>’ />
                        <asp:HiddenField ID="hfZhiwei" runat="server" Value=’<%# Eval("zhiweiID") %>’ />
                        <asp:DropDownList ID="ddlZhiwei" runat="server" Width="90px" />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="身份证号">
                    <ItemTemplate>
                        <%# Eval("card")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="tbcard" Text=’<%# Eval("card") %>’ runat="server" Width="150px" />
                    </EditItemTemplate>
                    <ItemStyle Width="100px" />
                </asp:TemplateField>
                <asp:BoundField HeaderText="时间" FooterText="时间" DataField="createtime" SortExpression="createtime"
                    ReadOnly="true" />
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" DeleteText="<div onclick=&quot;JavaScript:return confirm(’bubufx.com提醒您,确认要删除吗?’)&quot;>删除<div/>" />
            </Columns>
            <PagerSettings FirstPageText="首页" LastPageText="末页" NextPageText="下一页" PageButtonCount="5"
                PreviousPageText="上一页" Mode="NumericFirstLast" />
            <PagerStyle BorderColor="#66FF66" Font-Names="宋体" Font-Size="12px" HorizontalAlign="Center"
                CssClass="bubufxPagerCss" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>
gridviewUp.aspx.cs代码:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

public partial class gridviewUp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dataBind();
        }
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private void dataBind()
    {
        DB _db = new DB();
        string _sql = "select a.*,b.zhiweiName from gridviewTestTable a,zhiwei b where  a.zhiweiID=b.ID";

        DataTable _DT = _db.GetDt(_sql);

        GridView1.DataSource = _DT;
        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        dataBind();
    }

    /// <summary>
    /// “编辑”按钮按下时,将当前行设置为可编辑状态
    /// </summary>
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        dataBind();
    }

    /// <summary>
    /// “更新”按钮按下时,保存当前行的更改
    /// </summary>
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = ((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hfID")).Value;
        string zhiwei = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlZhiwei")).SelectedValue;
        string card = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("tbcard")).Text;

        string sql = "update gridviewTestTable set card=’" + card + "’,zhiweiID=’" + zhiwei + "’ where id=" + id;

        DB _db = new DB();

        int flag = _db.RunSql(sql);

        if (flag == 1)
        {
            GridView1.EditIndex = -1;
            dataBind();
        }
        else
        {
            Response.Write("修改异常!");
            Response.End();
        }
    }

    /// <summary>
    /// “取消”按钮按下时,取消当前行的编辑状态
    /// </summary>
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        dataBind();
    }

    /// <summary>
    /// “删除”按钮按下后,删除当前行
    /// </summary>
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = ((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hfID")).Value;

        string sql = "delete from  gridviewTestTable where ID=" + id;

        DB _db = new DB();

        int flag = _db.RunSql(sql);

        if (flag == 1)
        {
            GridView1.EditIndex = -1;
            dataBind();
        }
        else
        {
            Response.Write("删除异常!");
            Response.End();
        }

    }

    /// <summary>
    /// 行绑定时的处理,其中包括DropDownList的可选项绑定,和选中当前可选项
    /// </summary>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit) //判断当前行是否是“编辑”行
        {

            DropDownList ddlZhiwei = (DropDownList)e.Row.FindControl("ddlZhiwei");

            //绑定DropDownList数据
            DB _db = new DB();
            string _sql = "select * from zhiwei order by ID";

            DataTable _DT = _db.GetDt(_sql);

            ddlZhiwei.DataSource = _DT;
            ddlZhiwei.DataTextField = "zhiweiName";
            ddlZhiwei.DataValueField = "ID";
            ddlZhiwei.DataBind();
            //

            //选中DropDownList
            ddlZhiwei.SelectedValue = ((HiddenField)e.Row.FindControl("hfZhiwei")).Value;
            //

        }
    }
}
直接复制以上代码还不能完全运行,还需要数据库操作类DB,可点击到这里查看



数据库是两个表,数据列表和职位列表。

gridviewTestTable表:

GridView数据修改、删除,修改控件TextBox和DropDownList,删除ShowDeleteButton=true提示删除确认[asp.net基础]

zhiwei表:

GridView数据修改、删除,修改控件TextBox和DropDownList,删除ShowDeleteButton=true提示删除确认[asp.net基础]

GridView数据修改、删除,修改控件TextBox和DropDownList,删除ShowDeleteButton=true提示删除确认[asp.net基础]

上一篇:服装五金配件三维扫描方案_三维扫描仪


下一篇:GridView数据绑定自带分页及分页css样式,CheckBox多选,radio单选,自增列,[bubufx分享asp.net基础]