09
2008
01

DataGrid,DataList,Repeater控件问题

DataGrid,DataList,Repeater 这个三个控件在ADO.net中都起着显示数据的作用。但其效果却有很大的差异。
这里只介绍下DataGrid,DataList,Repeater三个按扭的用法
 
DataGrid用法
前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataGrid.aspx.cs" Inherits="DataGrid" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataGrid ID="DataGrid1" runat="server" BackColor="White" BorderColor="#E7E7FF"
            BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" AutoGenerateColumns="false">
            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" Mode="NumericPages" />
            <AlternatingItemStyle BackColor="#F7F7F7" />
            <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <Columns>
               <asp:BoundColumn DataField="name" HeaderText="用户名">
               <HeaderStyle Width="30%" />
               </asp:BoundColumn>
               <asp:BoundColumn DataField="passwd" HeaderText="密码">
               <HeaderStyle Width="50%" />
               </asp:BoundColumn>
            </Columns>
        </asp:DataGrid></div>
    </form>
</body>
</html>

后台代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

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

            String cstr = "server=(local);Initial Catalog=News;uid=sa;pwd=123456";
            SqlConnection oConn = new SqlConnection(cstr);
            oConn.Open();
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = new SqlCommand();
            sda.SelectCommand.Connection = oConn;
            sda.SelectCommand.CommandText = "one";
            sda.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataSet ds = new DataSet();
            sda.Fill(ds);
            DataGrid1.DataSource = ds.Tables[0].DefaultView;
            DataGrid1.DataBind();
            oConn.Close();
        }
    }
}

------------------------------------------------

DataList的用法

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Datalist.aspx.cs" Inherits="Datalist" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server">
          <SelectedItemStyle Font-Size="X-Large" />
          <HeaderTemplate><div>用户列表</div></HeaderTemplate>
          <SeparatorTemplate>
             <hr>
          </SeparatorTemplate>
          <ItemTemplate>
             用户名称:<%# DataBinder.Eval(Container.DataItem,"name") %><br />
             用户密码:<%# DataBinder.Eval(Container.DataItem,"passwd") %>
          </ItemTemplate>
          <AlternatingItemTemplate>
             用户名称:<%# DataBinder.Eval(Container.DataItem,"name") %><br />
             用户密码:<%# DataBinder.Eval(Container.DataItem,"passwd") %>
          </AlternatingItemTemplate>
          <FooterTemplate>成功</FooterTemplate>
          <AlternatingItemStyle BackColor="red"  ForeColor="white"/>
          <ItemStyle BackColor="yellowGreen" Font-Size="12px" ForeColor="white" />
        </asp:DataList></div>
        <div>
      
        </div>
    </form>
</body>
</html>

 

后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Datalist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String connstr = "server=(local);Initial Catalog=News;uid=sa;pwd=123456";
            SqlConnection oConn = new SqlConnection(connstr);
            oConn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter();
            //sda.SelectCommand.Connection
            sda.SelectCommand = new SqlCommand();
            sda.SelectCommand.Connection = oConn;
            sda.SelectCommand.CommandText = "one";
            sda.SelectCommand.CommandType = CommandType.StoredProcedure;
            sda.Fill(ds);
            DataList1.DataSource = ds.Tables[0].DefaultView;
            DataList1.DataBind();
            oConn.Close();
        }

    }
}

---------------------------------------------------------------------

Repeater 的用法

前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeater.aspx.cs" Inherits="Repeater" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Repeater 控件学习</title>
    <style>
    .alter{
    color:red;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="color: #ff66cc">
        <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
              <div id="header">会员</div>
            </HeaderTemplate>
            <SeparatorTemplate>
              <hr>
            </SeparatorTemplate>
            <ItemTemplate>
               <div id="lei">
               <%# ((DataRowView)Container.DataItem)["name"] %><br>
               <%# ((DataRowView)Container.DataItem)["passwd"] %>
               </div>
            </ItemTemplate>
            <AlternatingItemTemplate>
               <div class="alter">
               <%# ((DataRowView)Container.DataItem)["name"] %><br>
               <%# ((DataRowView)Container.DataItem)["passwd"] %>
               </div>
            </AlternatingItemTemplate>
            <FooterTemplate>
            <div class="alter">共<%# ((DataView)Repeater1.DataSource).Count %>个文件被找到!</div>
            </FooterTemplate>
        </asp:Repeater>
        <br />
</div>
    </form>
</body>
</html>

 

后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;

public partial class Repeater : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String connstr = "server=(local);Initial Catalog=News;uid=sa;pwd=123456";
            SqlConnection oConn = new SqlConnection(connstr);
            oConn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter();
            //sda.SelectCommand.Connection
            sda.SelectCommand = new SqlCommand();
            sda.SelectCommand.Connection = oConn;
            sda.SelectCommand.CommandText = "one";
            sda.SelectCommand.CommandType = CommandType.StoredProcedure;
            sda.Fill(ds);
            DataList1.DataSource = ds.Tables[0].DefaultView;
            DataList1.DataBind();
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
            oConn.Close();
        }
       
     
    }
}

« 上一篇下一篇 »

评论列表:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。