溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

asp.net 關于Repeater數據控件實用

發(fā)布時間:2020-06-25 20:13:29 來源:網絡 閱讀:922 作者:我不會抽煙 欄目:編程語言

首先說明一下我是asp.net菜鳥,寫文章純粹是為了把學過的東西記錄下來,方便以后忘記的時候可以回頭再來看看。

這里講的Repeater控件并不是詳解,而是一個最基礎的使用,下面根據我寫的小例子來講。

數據庫為自己寫的TestDatabase。

前臺代碼為

<asp:Repeater ID="Repeater1" runat="server" >
            <HeaderTemplate>
            <table id="Table1" >
                <tr >
                    <th>球員ID</th>
                    <th>球員姓名</th>
                    <th>所在球隊</th>
                    <th>球衣號碼</th>
                    <th>場上位置</th>
                    <th>出生日期</th>
                    <th>身高</th>
                    <th>體重</th>
                    <th>是否退役</th>
                    <th>投籃命中率</th>
                </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr >
                    <td><asp:label id="label1" runat="server" text='<%#Eval("PL_ID")%>'/> </td>
                    <td><asp:label id="label3" runat="server" text='<%#Eval("PL_NAME")%>'/> </td>
                    <td><asp:label id="label2" runat="server" text='<%#Eval("TEAM") %>'/> </td>
                    <td><asp:label id="label4" runat="server" text='<%#Eval("PL_NO")%>'/> </td>
                    <td><asp:label id="label5" runat="server" text='<%#Eval("POSITION")%>'/> </td>
                    <td><asp:label id="label6" runat="server" text='<%#IsNull2NA(Eval("BIRTHDAY").ToString())%>'/> </td>
                    <td><asp:textbox  id="label7" runat="server" text='<%#IsNull2NA(Eval("HEIGHT").ToString())%>'/> </td>
                    <td><asp:label id="label8" runat="server" text='<%#IsNull2NA(Eval("WEIGHT").ToString())%>'/> </td>
                    <td><asp:RadioButton ID="RadioButton1" runat="server" Checked='<%#IsTOrF(Eval("retired").ToString())%>' Enabled="false"/></td>
                    <td><asp:label id="label9" runat="server" text='<%#Eval("shoot_per")%>'/></td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
       </asp:Repeater>

針對上面的代碼我有兩點要說明,

1、Repeater的標簽元素:其中我們看到了<HeaderTemplate></HeaderTemplate>,<ItemTemplate></ItemTemplate>,

<FooterTemplate></FooterTemplate> ,這幾個標簽,如果不加<HeaderTemplate> <FooterTemplate> 這兩個標簽,用<ItemTemplate> 包在最外面的話,將會出現下面情況

asp.net 關于Repeater數據控件實用所以要按照我寫的用<HeaderTemplate></HeaderTemplate> 把<table> 和<th>的內容都包含進去

2、關于<%# Eval() %>引用后臺代碼:因為我調用這個的時候是為了給Text屬性賦值,但是由于沒有經驗,我在 <%# Eval() %>周圍加上了"" ,實際上應該是'',這個大家一定要注意。


后臺代碼為

    protected void Page_Load(object sender, EventArgs e)
    {
       string conn = WebConfigurationManager.ConnectionStrings["TestDatabaseConnectionString"].ConnectionString;
       SqlConnection myConn = new SqlConnection(conn);
       myConn.Open();
       string sql = "select * from NBA_PLAYER_TBL where PL_NO>'12'";
       SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
       DataSet ds = new DataSet();
       adp.Fill(ds);
       Repeater1.DataSource = ds;
       Repeater1.DataBind();
       myConn.Close();
     }
    public string IsNull2NA(string inStr)
    {
        if (inStr == "")
        {
            return "NA";
        }
        return inStr;
    }
    public Boolean IsTOrF(string inC)
    {
        if (inC== 'T'.ToString())
        {
            return true;
        }
        return false;
    }

后臺代碼相對來說是比較基礎的內容,只要記得要有Repeater的DataBind()操作就行了。

結果

asp.net 關于Repeater數據控件實用

做的比較丑,但是基本功能都已經實現。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI