网页显示数据,需要前后台数据的配合。
这里主要列举了几种方式来从后台获取数据,前端进行展示。
前端代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" ID="txtUserName"></asp:Label><br />
<asp:Label runat="server" ID="txtUserPhone"></asp:Label><br />
<asp:Label runat="server" ID="txtUserEmail"></asp:Label>
</div>
</form>
</body>
</html>
后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
int intIdx = 1;
//SqlParameter方法
string strSql1 = "select userName from UserInfo where idx=@idx";
string userName = SqlHelper.ExecuteScalar(CommandType.Text, strSql1,
new SqlParameter("@idx", intIdx)).ToString();
this.txtUserName.Text = userName;
//string.Format方法
string strSql2 = string.Format("select userPhone from UserInfo where idx={0}", intIdx);
string userPhone = SqlHelper.ExecuteScalar(strSql2).ToString();
this.txtUserPhone.Text = userPhone;
//拼接参数方法
string strSql3 = "select userEmail from UserInfo where idx ="+ intIdx + "";
string userEmail = SqlHelper.ExecuteScalar(strSql3).ToString();
this.txtUserEmail.Text = userEmail;
}
}
}具体网页展示如下:
本文介绍了一种通过前后台配合实现网页数据展示的方法。利用ASP.NET技术,在后台通过SQL语句获取用户信息,并将其传递到前端页面进行展示。文章提供了具体的前端和后台代码示例。
365

被折叠的 条评论
为什么被折叠?



