[已解决]将SQL列中的数据显示到网页中的ASP.NET标签 [英] [Solved]Display data from SQL columns to ASP.NET labels in a web page

查看:82
本文介绍了[已解决]将SQL列中的数据显示到网页中的ASP.NET标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我被困在一段代码上。

基本上我需要将SQL表格中的数据显示给少数标签在ASP.NET页面中。

我在类文件(数据访问层)中创建了一个方法,该方法从SQL数据库中选择整个表并作为DataTable返回到aspx.cs页面,在下拉列表的selectedindexchanged事件上。

我试图将ASP.NET标签绑定到各个列,但令我惊讶的是找不到标签的任何DataSource属性.Hence,the我写的代码没有显示任何进展。

我想我错过了什么。专家,能否请您指教?非常感谢任何帮助或指示。



以下是我的示例代码:



.aspx页面:



Hello people,

Am stuck on a piece of code.
Basically I need to display data from SQL table to few labels in an ASP.NET page.
I created a method in a class file(Data Access layer), which selected the whole table from the SQL db and returned to the aspx.cs page as a DataTable,on the selectedindexchanged event of a dropdownlist.
I was trying to bind the ASP.NET labels to the individual columns, but to my surprise couldn''t find any DataSource property for the label.Hence, the code which I wrote doesn''t show any progress.
I guess I''m missing something. Experts, can you please advise?Any help or pointers would be greatly appreciated.

Below is my sample code:

.aspx page:

<pre lang="xml"><body>
    <form id="form1" runat="server">


  <div id="header">
  <h1>I am in the Header Section</h1>
  </div>
  <div id="foobar">
  <p>
  <asp:DropDownList ID="CodeDropDown" runat="server" AutoPostBack="true" OnSelectedIndexChanged="SelectedIndexChanged"></asp:DropDownList>
  </p>
  </div>
  <div id="section1">
  <asp:Label ID="label1" runat="server"></asp:Label>
      <br />
  </div>
  <div id="section2">
  <asp:Label ID="label2" runat="server"></asp:Label>
      <br />
  </div>





aspx.cs页面:





aspx.cs page:

    protected void SelectedIndexChanged(object sender, EventArgs e)
    {
        int x = Convert.ToInt32(CodeDropDown.SelectedItem.Text);
        DAL objx = new DAL();
        DataTable dt = objx.populateDivs(x);
        
        label1.Text = dt.Columns[1].ToString();
        label2.Text = dt.Columns[2].ToString();
        label1.DataBind();
        label2.DataBind();
}





数据访问层:





Data access layer:

public DataTable populateDivs(int id)
{
    SqlDataAdapter da = new SqlDataAdapter("select * from Development.dbo.DesignPage where Code='" + id + "'", xconn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    return dt;
}





-Regards

anurag



-Regards
anurag

推荐答案

大家好,



通过一些R& S找到解决方案D .. :)因此想到共享。

因为我们没有ASP.NET标签的DataSource属性,所以我们不能将Db直接绑定到标签。 br />
相反,我们可以使用SqlDataReader迭代所提供的查询,然后将各个列名或索引值等同于不同的标签。



以下是上述方案的示例代码。



Hi people,

Found out the solution by some R & D..:) and hence thought of sharing.
Since we don''t have a DataSource property for an ASP.NET label, we can''t bind the Db directly to the label.
Instead, we can use SqlDataReader to iterate throught the query provided and then equating the individual column names or index values to the different labels.

Below is the sample code for the above asked scenario.

    protected void SelectedIndexChanged(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(CodeDropDown.SelectedItem.Text);
        SqlCommand cmd = new SqlCommand("select * from development.dbo.DesignPage where code='" + id + "'", xconn);
        try
        {
            xconn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                label1.Text = dr["Section1"].ToString();
                label2.Text = dr["Section2"].ToString();
                label3.Text = dr["Section3"].ToString();
                label4.Text = dr["Section4"].ToString();
                label5.Text = dr["Section5"].ToString();

            }
        }
        finally
        {
            xconn.Close();
        }
   
    }
}





希望它有所帮助。



-regards

anurag



Hope it helps somebody.

-regards
anurag


这篇关于[已解决]将SQL列中的数据显示到网页中的ASP.NET标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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