《ASP.NET》資料的綁定—Repeater圖文詳解

黄舟
發布: 2017-03-08 11:57:47
原創
1935 人瀏覽過

     前面學習了HTML靜態的網頁編程,了解了其中的一些語法,但是自己感覺對Web編程掌握的還不夠過癮,於是跟著計劃,開始了ASP.NET之旅。在寫這篇ASP.NET部落格之前,我想先比較一下ASP.NET與HTML的差異與連結。

     一、聯絡與差異:HTML是在客戶端編程,通常產生的是靜態網頁;ASP.NET是在伺服器端編程,通常能產生動態網頁。 ASP.NET中的控制項是HTML中的控制項重新設計、封裝起來的,也就是說ASP.NET中的控制項是以HTML中的控制項為基礎。 ASP.NET控制項具有回送功能,夠用ViewState維持控制項的狀態, HTML控制項則不能,點選頁面的操作,其狀態就會遺失。

     在ASP.NET的學習過程中,其控件的學習和使用佔了很大的一部分,今天,我說一下控件Repeater控制項的使用,用它來綁定後台數據,然後在客戶端(瀏覽器)上顯示出來!

     二、Repeater控制項

##     1、使用:使用範本循環顯示資料。

     2、包含的範本:

         項目模板(裡面的數據正常顯示)

 交錯顯示範本(裡面綁定的資料交錯著顯示)

#

頁尾範本(編輯頁腳)   

頁首範本(編輯頁首)

間隔範本 (在顯示的資料中插入間隔,像橫線、特殊符號等)

#     三、範例

     我使用vs2012的ASP.NET Web表單應用程式所寫的範例。

     1、內容介紹

     將資料庫中Person表中的資訊選出來,然後用Repeater控制項在客戶端顯示。下圖是我Sqlser資料庫中person表中的信息。

     

######    1:將資料庫中的資訊選出來並在後台綁定: 新建Web窗體應用程式,新增窗體,在窗體的Page_Load事件中加入以下程式碼。 ######

      

protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = DB.createConnection();
            SqlDataAdapter sda = new SqlDataAdapter();
            string sql="select * from person ";
            sda.SelectCommand = new SqlCommand(sql, con);
            DataSet ds=new DataSet();
            sda.Fill(ds, "per");
            this.Repeater1.DataSource=ds.Tables["per"];
            Repeater1.DataBind();
        }
登入後複製



     2:用控制Repeater的範本  <Item2; /ItemTemplate>   將資訊顯示,程式碼如下

#
<asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                    <p align="center">
                        <%# DataBinder.Eval(Container.DataItem,"pID") %>
                        <%# DataBinder.Eval(Container.DataItem,"personName") %>
                        <%# DataBinder.Eval(Container.DataItem,"personSex") %>
                    </p>
                </ItemTemplate>
            </asp:Repeater>
登入後複製

     3:顯示效果如下

     3:顯示效果如下

     3:顯示效果如下


#     4:範本使用(讓資料交叉顯示)


##############################) ######
 <asp:Repeater ID="Repeater1" runat="server">
                <AlternatingItemTemplate>
                    <p align="center">
                        <font color="blue"> <%# DataBinder.Eval(Container.DataItem,"pID") %>
                        <%# DataBinder.Eval(Container.DataItem,"personName") %> 
                        <%# DataBinder.Eval(Container.DataItem,"personSex") %></font>
                    </p>
                </AlternatingItemTemplate>
            </asp:Repeater>
登入後複製
###############    顯示效果如下,結構只顯示2、4、6、9列,這就是所謂的交叉顯示。 ###########################    最後,我將五個模板一塊使用,前台程式碼如下########## ########
<asp:Repeater ID="Repeater1" runat="server">
                <HeaderTemplate>
                    <h3 align="center">页眉模板</h3>
                </HeaderTemplate>

                <ItemTemplate>
                    <p align="center">
                        <font color="blue"> <%# DataBinder.Eval(Container.DataItem,"pID") %>
                        <%# DataBinder.Eval(Container.DataItem,"personName") %> 
                        <%# DataBinder.Eval(Container.DataItem,"personSex") %></font>
                    </p>
                </ItemTemplate>
                <AlternatingItemTemplate>
                    <p align="center">
                        <font color="blue"> <%# DataBinder.Eval(Container.DataItem,"pID") %>
                        <%# DataBinder.Eval(Container.DataItem,"personName") %> 
                        <%# DataBinder.Eval(Container.DataItem,"personSex") %></font>
                    </p>
                </AlternatingItemTemplate>

                <SeparatorTemplate>
                    <hr color="red" size="1" />
                </SeparatorTemplate>

                <FooterTemplate>
                    <h3 align="center">页脚模板</h3>
                </FooterTemplate>

            </asp:Repeater>
登入後複製
######    顯示效果圖如下##############################      這就是利用控制項將後台資料庫中的資訊用瀏覽器顯示出來的方法,其實不光Repeater控件,像DataList,GridView,CheckBoxList、DropDownList等等都能將資料庫中的資訊加以綁定然後再在瀏覽器中顯示出來,後面我會一一介紹,敬請期待! ! ###################

以上是《ASP.NET》資料的綁定—Repeater圖文詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!