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

在开发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);
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值