C#后台拼接参数,前台显示数据

本文介绍了一种通过前后台配合实现网页数据展示的方法。利用ASP.NET技术,在后台通过SQL语句获取用户信息,并将其传递到前端页面进行展示。文章提供了具体的前端和后台代码示例。

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

网页显示数据,需要前后台数据的配合。

这里主要列举了几种方式来从后台获取数据,前端进行展示。

前端代码如下:

<%@ 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;
        }
    }
}

具体网页展示如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值