Asp.Net中几种常见的方法批量显示数据

来源:互联网 发布:pc版直播软件 编辑:程序博客网 时间:2024/06/06 21:40

在开发Asp.Net程序中,我们经常会有这样的需求,将数据库里的部分数据读取并显示在前段页面,下面将列举几种常见的方法:


1、使用GridView控件,可以通过可视化界面绑定数据源,并配置显示的列和样式,就搞定了!一行代码不用写!

其中,绑定数据源的方式有四种:

一是直接配置GridView控件本身;

二是使GridView控件绑定一个新的SqlDataSource服务器控件;

三是对GirdView控件的数据源属性进行复制,可以是一个DataSet对象;

四是对GirdView控件的数据源属性进行复制,可以是Linq的一次查询结果;


2、使用内嵌HTML表格客户端控件的Repeater服务器控件

前端代码:

 <asp:Repeater ID="rpTest" runat="server">
   <HeaderTemplate>
     <table>
       <tr>
         <th>ID</th>
         <th>Title</th>
         <th>Text</th>
       </tr>
   </HeaderTemplate>
   <ItemTemplate>
     <tr>
       <td><asp:Label runat="server" ID="lblID" Text='<%# Eval("DeptId") %>'></asp:Label></td>
       <td><asp:Label runat="server" ID="lblTitle" Text='<%# Eval("Name") %>'></asp:Label></td>
       <td><asp:Label runat="server" ID="lblText" Text='<%# Eval("MainNumber") %>'></asp:Label></td>
     </tr>
   </ItemTemplate>
   <AlternatingItemTemplate>
     <tr>
       <td><asp:Label runat="server" ID="lblID" Text='<%# Eval("DeptId") %>'></asp:Label></td>
       <td><asp:Label runat="server" ID="lblTitle" Text='<%# Eval("Name") %>'></asp:Label></td>
       <td><asp:Label runat="server" ID="lblText" Text='<%# Eval("MainNumber") %>'></asp:Label></td>
     </tr>
   </AlternatingItemTemplate>
   <FooterTemplate>
     </table>
   </FooterTemplate>
 </asp:Repeater>

后台代码:

  private void BindDataToRepeater()
        {
            string strCon = "Data Source=192.168.27.211;Initial Catalog=pdms;Persist Security Info=True;User ID=sa;Password=123456";
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select DeptId,Name,MainNumber from t_Project", con);


            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds, "t_Project");
            rpTest.DataSource = ds.Tables["t_Project"];
            rpTest.DataBind();
            con.Close();
        }


3、使用HTML Table服务器控件

前端代码:

        <Table ID="tb" runat="server" style="border:1.0" class="gridview_m">
        <tr>
        <td>Id</td>
        <td>Name</td>
        <td>MainNumber</td>
        </tr>
        </Table>

后台代码:

   public void TbBind()
        {
            string strCon = "Data Source=192.168.27.211;Initial Catalog=pdms;Persist Security Info=True;User ID=sa;Password=123456";
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select DeptId,Name,MainNumber from t_Project", con);


            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds, "t_Project");
            
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                HtmlTableRow r = new HtmlTableRow();
                HtmlTableCell c1 = new HtmlTableCell();
                c1.InnerHtml = ds.Tables[0].Rows[i]["DeptId"].ToString();
                HtmlTableCell c2 = new HtmlTableCell();
                c2.InnerHtml = ds.Tables[0].Rows[i]["Name"].ToString();
                HtmlTableCell c3 = new HtmlTableCell();
                c3.InnerHtml = ds.Tables[0].Rows[i]["MainNumber"].ToString();
                r.Controls.Add(c1);
                r.Controls.Add(c2);
                r.Controls.Add(c3);
                tb.Controls.Add(r);
            }
        }


4、仅使用HTML Table客户端表格

不需要前端代码;

后台代码如下:

  public void BindTable()
        {
            string strCon = "Data Source=192.168.27.211;Initial Catalog=pdms;Persist Security Info=True;User ID=sa;Password=123456";
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select DeptId,Name,MainNumber from t_Project", con);


            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds, "t_Project");


            DataTable dt = new DataTable();
            dt = ds.Tables[0];


            string str = "<table>";
            for (int i = 0; i < 10; i++)
            {
                str += "<tr>";
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    str += string.Format("<td >{0}</td>", dt.Rows[i][j].ToString());
                }
                str += "</tr>";
            }
            str += "</table>";
            Response.Write(str);
        }

原创粉丝点击