在DataList中使用使用者控制項

來源:互聯網
上載者:User

DataList相對於datagrid對資料的顯示要靈活一些(還是在vs2003的叫法).應為DATALIST提供了幾個模板,而在模板中可以靈活的放置一些自訂的使用者控制項。具體實施如下:

先建立使用者控制項:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="authorNormal.ascx.cs" Inherits="authorNormal" %>
<table border="1" cellpadding="0" cellspacing="0" style="border-left-color: aqua; border-bottom-color: aqua; border-top-style: solid; border-top-color: aqua; border-right-style: solid; border-left-style: solid; border-right-color: aqua; border-bottom-style: solid" width="400">
    <tr>
        <td style="width: 100px">
            作者編號:</td>
        <td style="width: 100px">
            <asp:Label ID="lblId" runat="server"></asp:Label></td>
    </tr>
    <tr>
        <td style="width: 100px; height: 21px">
            作者姓名:</td>
        <td style="width: 100px; height: 21px">
            <asp:Label ID="lblName" runat="server"></asp:Label></td>
    </tr>
</table>

後台代碼:

 private string au_ID;
    public string Au_ID
    {
        set
        {
            au_ID = value;
        }
        get
        {
            return au_ID;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.bind();
        }
    }
    private void bind()
    {
        SqlDataReader sdr = new Author().show(this.Au_ID );
        while (sdr.Read())
        {
            this.lblId.Text  = sdr["au_id"].ToString();
            this.lblName .Text = sdr["au_lname"].ToString().Trim() + " " + sdr["au_fname"].ToString().Trim();
        }
    }
}

注意:要定義使用者控制項的一個屬性,該屬性可以是唯寫的,用於接收datalist綁定的資訊。

前台頁面的代碼:

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

<%@ Register Src="authorNormal.ascx" TagName="authorNormal" TagPrefix="uc1" %>

<!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>
   
    </div>
        <div style="text-align: center">
            <table>
                <tr>
                    <td style="width: 100px; height: 37px;">
                        <asp:DataList ID="DataList1" runat="server" RepeatColumns="2">
                            <ItemTemplate>
                                <uc1:authorNormal id="AuthorNormal1" runat="server"  Au_ID =<%#DataBinder.Eval(Container.DataItem,"au_id") %>>
                                </uc1:authorNormal>
                            </ItemTemplate>
                        </asp:DataList></td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

後台代碼:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.DataList1.DataSource = new Author().bind();
            this.DataList1.DataBind();
            this.DataList1.DataKeyField = "au_id";
        }
    }

在綁定的頁面內容時候,可以使用任何的資料(查詢後的,或記憶體,或從XML檔案載入都可以)。

特別要強調的是:如果是根據條件查詢後的往往要通過viewstate或session傳遞,如果傳遞的資料多,可以將資料先一個arraylist中,然後傳遞,然後在datalist頁面綁定的時候,可以藉助於一個虛擬表。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.