ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法

简介: ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法

ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法


前言

ASP.NET WEB是一门非常简单的课程内容,我们大概用三章的内容来包含所有的知识点,三章分为

1、ASP.NET WEB项目创建与文件上传操作

2、ASP.NET WEB项目中Cookie与Session的用法

3、ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法

分为三章,基本上将具体的用法讲解完毕,配套的【Repeater】的基础项目视频包含【数据库CRUD操作】让你快速上手,解决你考试的后顾之忧。

环境

系统环境:【win11】

开发工具:【Visual Studio 2017】

数据库:【SQLServer 2019】


测试数据(单表)

直接单表测试也方便,毕竟我们只看功能。

测试数据脚本:

DROP TABLE [dbo].[users]
GO
CREATE TABLE [dbo].[users] (
[id] int NOT NULL IDENTITY(1,1) ,
[userName] varchar(20) NOT NULL ,
[sex] bit NOT NULL ,
[age] int NOT NULL ,
[introduce] varchar(200) NOT NULL 
)
GO
DBCC CHECKIDENT(N'[dbo].[users]', RESEED, 3)
GO
-- ----------------------------
-- Records of users
-- ----------------------------
SET IDENTITY_INSERT [dbo].[users] ON
GO
INSERT INTO [dbo].[users] ([id], [userName], [sex], [age], [introduce]) VALUES (N'1', N'王语嫣', N'0', N'16', N'琅嬛福地,神仙姐姐。');
GO
INSERT INTO [dbo].[users] ([id], [userName], [sex], [age], [introduce]) VALUES (N'2', N'小龙女', N'0', N'18', N'终南山下,活死人墓。冰山美人,绝世江湖。');
GO
INSERT INTO [dbo].[users] ([id], [userName], [sex], [age], [introduce]) VALUES (N'3', N'赵灵儿', N'0', N'15', N'灵蛇岛等待逍遥哥哥的小姑娘。');
GO
SET IDENTITY_INSERT [dbo].[users] OFF
GO
-- ----------------------------
-- Indexes structure for table users
-- ----------------------------
-- ----------------------------
-- Primary Key structure for table [dbo].[users]
-- ----------------------------
ALTER TABLE [dbo].[users] ADD PRIMARY KEY ([id])
GO

DBHelper

1、需要换包名,也就是【namespace Demo_1】

2、需要换成自己的数据库地址以及用户名与pwd

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace Demo_1
{
    public class DBHelper
    {
        private static string url = "Data Source=.;Initial Catalog=mytest;Integrated Security=True";
        //账密方式
        //private static string url = "server=.;database=girl1804;uid=sa;pwd=root";
        /// <summary>
        /// 获取查询信息
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable Query(string sql)
        {
            SqlConnection conn = new SqlConnection(url);
            SqlDataAdapter sdap = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            sdap.Fill(ds);
            return ds.Tables[0];
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql)
        {
            SqlConnection conn = new SqlConnection(url);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            int rows = cmd.ExecuteNonQuery();
            conn.Close();
            return rows;
        }
        /// <summary>
        /// 存储过程·一般用不到,大型项目严禁使用
        /// </summary>
        /// <param name="proName"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static bool ExcuteProcedure(string proName, SqlParameter[] paras)
        {
            SqlConnection conn = new SqlConnection(url);
            conn.Open();
            SqlCommand cmd = new SqlCommand(proName, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i < paras.Length; i++)
            {
                cmd.Parameters.Add(paras[i]);
            }
            int rows = cmd.ExecuteNonQuery();
            conn.Close();
            return rows > 0;
        }
    }
}

GridView用法

创建GridView测试窗体

前台

<asp:GridView runat="server" ID="gridView" AutoGenerateColumns="false">
     <Columns>
        <asp:BoundField DataField="id" HeaderText="编号"/>
        <asp:BoundField DataField="userName" HeaderText="昵称"/>
        <asp:BoundField DataField="sex" HeaderText="性别"/>
        <asp:BoundField DataField="age" HeaderText="年龄"/>
        <asp:BoundField DataField="introduce" HeaderText="简介"/>
    </Columns>
</asp:GridView>

后台

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) {
        string sql = "select * from users";
        this.gridView.DataSource = DBHelper.Query(sql);
        this.gridView.DataBind();
    }
}

输出效果:

Repeater用法

创建【Repeater】测试窗体

前台

<link href="Content/bootstrap.css" rel="stylesheet" />
<table class="table table-bordered table-hover">
    <tr>
        <th>编号</th>
        <th>昵称</th>
        <th>性别</th>
        <th>年龄</th>
        <th>简介</th>
    </tr>
    <asp:Repeater runat="server" ID="repeater">
        <ItemTemplate>
            <tr>
                <td><%# Eval("id") %></td>
                <td><%# Eval("userName") %></td>
                <td><%# Eval("sex") %></td>
                <td><%# Eval("age") %></td>
                <td><%# Eval("introduce") %></td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

后台

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string sql = "select * from users";
        this.repeater.DataSource = DBHelper.Query(sql);
        this.repeater.DataBind();
    }
}

输出效果:

总结

ASP.NET Web的知识点不是很多,我们后面会有ASP.NET MVC的课程会进行更多ASP.NET WEB端的实际讲解。其实最期待的还是.NET Core微服务,现在已经是.NET6.0了,做项目真的挺方便的呢。但是对于程序员要求相对要高一些。

我留了一篇练习的文章,包含整个的增删改查,希望能帮助到大家,链接再下方:

ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能

相关文章
|
1月前
|
JavaScript Java 微服务
现代化 Java Web 在线商城项目技术方案与实战开发流程及核心功能实现详解
本项目基于Spring Boot 3与Vue 3构建现代化在线商城系统,采用微服务架构,整合Spring Cloud、Redis、MySQL等技术,涵盖用户认证、商品管理、购物车功能,并支持Docker容器化部署与Kubernetes编排。提供完整CI/CD流程,助力高效开发与扩展。
283 63
|
2月前
|
安全 JavaScript Java
java Web 项目完整案例实操指南包含从搭建到部署的详细步骤及热门长尾关键词解析的实操指南
本项目为一个完整的JavaWeb应用案例,采用Spring Boot 3、Vue 3、MySQL、Redis等最新技术栈,涵盖前后端分离架构设计、RESTful API开发、JWT安全认证、Docker容器化部署等内容,适合掌握企业级Web项目全流程开发与部署。
133 0
|
4月前
|
人工智能 安全 程序员
用 Colab 和 ngrok 免费部署你的 Web UI 项目,随时随地访问!
用 Colab 和 ngrok 免费部署你的 Web UI 项目,随时随地访问!
|
7月前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
381 7
|
8月前
|
开发框架 前端开发 .NET
一个适用于 .NET 的开源整洁架构项目模板
一个适用于 .NET 的开源整洁架构项目模板
144 26
|
7月前
|
安全 Linux 开发工具
零基础构建开源项目OpenIM桌面应用和pc web- Electron篇
OpenIM 为开发者提供开源即时通讯 SDK,作为 Twilio、Sendbird 等云服务的替代方案。借助 OpenIM,开发者可以构建安全可靠的即时通讯应用,如 WeChat、Zoom、Slack 等。 本仓库基于开源版 OpenIM SDK 开发,提供了一款基于 Electron 的即时通讯应用。您可以使用此应用程序作为 OpenIM SDK 的参考实现。本项目同时引用了 @openim/electron-client-sdk 和 @openim/wasm-client-sdk,分别为 Electron 版本和 Web 版本的 SDK,可以同时构建 PC Web 程序和桌面应用(Wi
455 2
|
8月前
|
开发框架 安全 .NET
【Azure Developer】.NET Aspire 项目本地调试遇 Grpc.Core.RpcException 异常( Error starting gRPC call ... )
Error starting gRPC call. HttpRequestException: The SSL connection could not be established, see inner exception. AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot
168 12
|
7月前
|
传感器 人工智能 机器人
D1net阅闻|OpenAI机器人项目招新 或自研传感器
D1net阅闻|OpenAI机器人项目招新 或自研传感器
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
193 7

热门文章

最新文章