GridView 添加,修改,删除

本文介绍了一个使用ASP.NET中GridView控件实现数据展示、编辑、删除及分页功能的例子。通过前后端代码配合,实现了对数据库中数据的有效管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 GridView 添加,修改,删除事件前后台代码


GridView 控件代码   前台代码

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
            DataKeyNames="id" onpageindexchanging="GridView1_PageIndexChanging"
            onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
            onrowupdating="GridView1_RowUpdating" PageSize="5">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>


CS文件代码

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

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

    //正在删除的时候激发
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //获取要删除的记录的id并赋值给变量ID
        string ID = this.GridView1.DataKeys[e.RowIndex][0].ToString();
        logic.Class1 abc = new logic.Class1();
        string Sql = "DELETE FROM " + configure.Class1.Dataname + " where id=" + ID + "";
        abc.Exesql(Sql);
        baindata();
    }

    //换页后触发
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //更新页值
        this.GridView1.PageIndex = e.NewPageIndex;
        baindata();//重新绑定记录
    }

    //点击编辑后触发
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        baindata();
    }

    //当取消时触发
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        baindata();
    }

    //当更新时触发
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //获取要编辑的记录的id并赋值给变量ID
        string ID = this.GridView1.DataKeys[e.RowIndex][0].ToString();
        string text1 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
        string text2 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString();
        string text3 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString();
        string text4 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString();
        string text5 = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString();
        logic.Class1 abc = new logic.Class1();
        abc.Exesql("update book set title='" + text1 + "',content='" + text2 + "',addman='" + text3 + "',ip='" + text4 + "',addtime='" + text5 + "' where id=" + ID + "");
        this.GridView1.EditIndex = -1;
        baindata();
    }

    //绑定数据库
    public void baindata()
    {
        //实例化命名空间logic中的class1类
        logic.Class1 abc = new logic.Class1();
        //获取abc返回的DataSet
        DataSet da = abc.Opendata(configure.Class1.Sqlexe);
        //指定数据源
        this.GridView1.DataSource = da.Tables[0].DefaultView;//GridView1的数据源=DataSet返回数据
        //将数据绑定到GridView1
        this.GridView1.DataBind();
    }
}

1.3 研究主要内容 本研究的主要内容集中在基于.NET的个人健康管理系统的设计与实现上。该系统以MVC架构为基础,通过采用ASP.NET MVC框架,旨在为用户提供全面、高效、安全的健康管理服务。 首先,我们将对个人健康管理系统的需求进行深入的调研和分析。通过了解用户的具体需求和市场趋势,我们将明确系统的功能定位和特点,确保系统能够满足用户的实际需求。 其次,我们将基于MVC架构对系统进行全面的设计。这包括模型(Model)的设计,用于处理数据和业务逻辑;视图(View)的设计,用于展示数据和用户界面;以及控制器(Controller)的设计,用于处理用户请求和协调模型与视图之间的交互。通过合理的架构设计,我们将确保系统的稳定性和可扩展性。 在系统的实现过程中,我们将重点关注以下几个方面:一是实现用户友好的界面和操作流程,使用户能够方便地使用系统进行健康管理;二是实现健康信息的记录和存储功能,包括用户的基本信息、健康数据、运动记录等;三是实现健康数据的分析和展示功能,通过图表、报告等形式帮助用户了解自己的健康状况;四是实现医师咨询和互动圈子功能,为用户提供专业的健康建议和交流平台;五是确保系统的数据安全和隐私保护,采用合适的安全措施和技术手段。 此外,我们还将对系统进行详细的测试和优化,以确保系统的功能和性能达到最佳状态。通过测试,我们将发现并修复系统中的潜在问题,提升用户体验和系统的稳定性。 最后,我们将对研究结果进行总结和展望,分析系统的优势和不足,并提出进一步的改进和发展方向。同时,我们也希望能够为相关领域的研究和实践提供有益的参考和借鉴。 <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" ValidateRequest="false" CodeFile="Manage.aspx.cs" Inherits="foods_List" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <table width="99%" height="100%" align="center" style="border:2px solid #D6F2FE;"> <tr><td height="28" style="background-color:#D6F2FE;"> <b style="color:#4F98C6; font-size:12px;vertical-align:middle;">  管理膳食搭配信息</b> </td></tr> <tr> <td> <table style="height:100%; min-height:500px;"><tr><td valign="top"> <table style="width:100%;"> <tr> <td align="center"> <strong> 类别</strong><asp:DropDownList ID="ddlftype" runat="server" Width="150"> <asp:ListItem Value="">全部</asp:ListItem> <asp:ListItem>早餐</asp:ListItem> <asp:ListItem>午餐</asp:ListItem> <asp:ListItem>晚餐</asp:ListItem> </asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="查 找" class="btn" onclick="btnSearch_Click" /> </td> </tr> <tr> <td> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" Width="100%" AllowPaging="True" GridLines="None" onpageindexchanging="GridView1_PageIndexChanging"> <RowStyle Height="25px" HorizontalAlign="Center" /> <Columns> <asp:TemplateField ControlStyle-Width="30" HeaderText="选择" > <ItemTemplate> <asp:CheckBox ID="DeleteThis" runat="server" /> </ItemTemplate> <ItemStyle Width="30px" /> </asp:TemplateField> <asp:BoundField HeaderText="日期" DataField="fdate" /> <asp:BoundField HeaderText="类别" DataField="ftype" /> <asp:TemplateField HeaderText="所吃食物"> <ItemTemplate> <%# StringHelper.SubStringHtml( Eval("memo").ToString(),20) %> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="就餐时间" DataField="ftime" /> <asp:HyperLinkField DataNavigateUrlFormatString="View.aspx?id={0}" DataNavigateUrlFields="id" HeaderText="详情" Text="详情" > <ItemStyle Width="50px" /> </asp:HyperLinkField> <asp:HyperLinkField DataNavigateUrlFormatString="Modify.aspx?id={0}" DataNavigateUrlFields="id" HeaderText="修改" Text="修改" > <ItemStyle Width="50px" /> </asp:HyperLinkField> </Columns> <HeaderStyle Height="27px" /> <PagerStyle HorizontalAlign="Center" /> </asp:GridView> <table width="100%" height="28" border="0" cellpadding="0" cellspacing="0"> <tr> <td align="left"> <asp:Button ID="btnAll" runat="server" Text="全选" class="btn" OnClick="btnAll_Click"/> <asp:Button ID="btnUn" runat="server" Text="反选" class="btn" OnClick="btnUn_Click"/> <asp:Button ID="btnDelete" runat="server" Text="删除" class="btn" OnClick="btnDelete_Click"/> </td> </tr> </table> </td> </tr> </table> </td></tr></table> </td> </tr></table> </asp:Content> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
最新发布
06-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值