【问题标题】:Display DataTable information in aspx page在aspx页面中显示DataTable信息
【发布时间】:2013-09-06 06:53:41
【问题描述】:

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

代码如下:

 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 中显示 DataTable 信息。我该怎么做?

【问题讨论】:

    标签: c# asp.net datatable


    【解决方案1】:

    使用网格视图..

    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
    

    在运行时它只呈现为表格..

    然后

    GridView1.DataSource=Session[userdata];
    GridView1.DataBind();
    

    【讨论】:

      【解决方案2】:

      主页.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;
      
      }
      

      【讨论】:

      • 做到了!感谢您的回复:)。
      【解决方案3】:

      您可以在 Home.aspx 上使用以下方法之一检索您的数据表

      DataTable dtTable = Session["userdata"] as DataTable
      

      DataTable dtTable= (DataTable)Session["userdata"];
      

      提取数据-

      dtTable.rows[rowindex][columnindex] // Ex: dtTable.rows[0][0]
      

      或者如果您知道列名

      dtTable.rows[rowindex][columnname] // Ex: dtTable.rows[0]["yourColumnName"] 
      

      【讨论】:

      • 感谢您的回复:)。
      【解决方案4】:

      Home.aspx你可以做如下

      protected void Page_Load(object sender, EventArgs e)
      {
           DataTable dt = (DataTable)Session["userdata"]
           gridview.DataSource =dt;
           gridview.DataBind();
      }
      

      【讨论】:

      • 感谢您的回复:)。
      【解决方案5】:

      在您的 Home.aspx 中添加您想要显示的有关用户的信息的标签。假设您想显示用户名,然后添加:

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

      在后面的代码中,添加:

      protected void Page_Load(object sender, EventArgs e)
      {
          DataTable dt = (DataTable)Session["userdata"] ;
          lblUserName.Text = dt.Rows[0]["UserName"].ToString();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-30
        • 1970-01-01
        • 1970-01-01
        • 2012-03-16
        • 1970-01-01
        • 2022-06-16
        相关资源
        最近更新 更多