[Asp.net]Repeater 綁定 NameValueCollection 類型的資料來源

來源:互聯網
上載者:User

今天在做一個功能的時候,需要把 Request.ServerVariables 屬性綁定給 Repeater 控制項顯示,Request.ServerVariables 返回的是一個 NameValueCollection 對象,一個索引值對的集合。

Google了一下,居然無一例外需要在 Repaeter_ItemDataBound 事件進行相應的處理,類似於下面的代碼:

 

前台頁面:

                <asp:Repeater ID="Repeater1" runat="server"                     onitemdatabound="Repeater1_ItemDataBound">                    <HeaderTemplate>                        <table class="no-style full">                            <thead>                                <tr>                                    <th>資訊(鍵)</th>                                    <th>資訊(值)</th>                                </tr>                            </thead>                            <tbody>                    </HeaderTemplate>                    <ItemTemplate>                                <tr>                                    <td>                                        <asp:Label ID="lblName" runat="server" Text=""></asp:Label>                                    </td>                                    <td>                                        <asp:Label ID="lblValue" runat="server" Text=""></asp:Label>                                    </td>                                </tr>                    </ItemTemplate>                    <FooterTemplate>                            </tbody>                        </table>                    </FooterTemplate>                </asp:Repeater>

 

後台頁面:

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)    {        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)        {            Label lblName = (Label)e.Item.FindControl("lblName");            Label lblValue = (Label)e.Item.FindControl("lblValue");            lblName.Text = e.Item.DataItem.ToString();            lblValue.Text = Request.ServerVariables[e.Item.DataItem.ToString()];        }    }

 

第一感覺,這樣做太過繁瑣,拉兩個 Label 控制項,還要在 Repeater_ItemDataBound 事件裡面,編寫代碼。直接了當的方法不是很好, Eval 綁定文法不是就能搞定,在 Asp.net 頁面就能搞定的事情,完全沒有必要放到 cs 頁面裡去吧。

 

最佳化後的頁面:

                <asp:Repeater ID="Repeater1" runat="server">                    <HeaderTemplate>                        <table class="no-style full">                            <thead>                                <tr>                                    <th>資訊(鍵)</th>                                    <th>資訊(值)</th>                                </tr>                            </thead>                            <tbody>                    </HeaderTemplate>                    <ItemTemplate>                                <tr>                                    <td>                                        <%# Container.DataItem %>                                    </td>                                    <td>                                        <%# Request.ServerVariables[Container.DataItem.ToString()] %>                                    </td>                                </tr>                    </ItemTemplate>                    <FooterTemplate>                            </tbody>                        </table>                    </FooterTemplate>                </asp:Repeater>

 

最後的話:

最佳化後的頁面, 代碼整潔了不少,也省下了不時間,心情也舒暢了不少,故而記錄之。-,- 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.