在aspx页面中显示DataTable信息 [英] Display DataTable information in aspx page

查看:271
本文介绍了在aspx页面中显示DataTable信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Asp.net的新手。我创建了一个登录页面,该页面检索DataTable中的用户信息。我将此信息存储在会话变量中。

I am new to Asp.net. I have created a login page which retrieves the user information in a DataTable. I am storing this information in a Session Variable.

这里是代码:

 using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
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.Xml.Linq;
using System.Data;
using System.Web.Configuration; 


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from UserTable where UserName =@username and Password=@password", con);
    cmd.Parameters.AddWithValue("@username", txtUserName.Text);
    cmd.Parameters.AddWithValue("@password", txtPWD.Text);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        Session["userdata"] = dt;
        Response.Redirect("Home.aspx");
    }
    else
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
    }
}
}

现在想显示数据表Home.aspx中的信息。我该怎么办?

Now want to display the DataTable information in Home.aspx. How can i do this?

推荐答案

Home.aspx

Home.aspx

<asp:Label ID="lblUserName" runat="server" />

后面的代码

protected void Page_Load(object sender, EventArgs e)
{

    DataTable dt=new DataTable();
    dt= (DataTable)Session["userdata"] ;
    lblUserName.Text=dt.Rows[0]["UserName"].ToString();//your cloumn name;

}

这篇关于在aspx页面中显示DataTable信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆