[Asp.net] bind the Repeater to a NameValueCollection Data Source

Source: Internet
Author: User
Tags servervariables

When developing a function today, you needRequest. ServerVariablesProperty boundRepeaterControl display,Request. ServerVariablesReturnsNameValueCollectionObject, a set of key-value pairs.

After Google, you needRepaeter_ItemDataBoundEvent processing is similar to the following code:

 

Front-end page:

<Asp: repeater ID = "Repeater1" runat = "server" onitemdatabound = "repeaterincluitemdatabound"> <HeaderTemplate> <table class = "no-style full"> <thead> <tr> <th> information (key) </th> <th> information (value) </th> </tr> </thead> <tbody> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp: label ID = "lblName" runat = "server" Text = ""> </asp: Label> </td> <asp: label ID = "lblValue" runat = "server" Text = ""> </asp: label> </td> </tr> </ItemTemplate> <FooterTemplate> </tbody> </table> </FooterTemplate> </asp: Repeater>

 

Background page:

    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()];        }    }

 

First, this is too cumbersome.LabelControl, but also inRepeater_ItemDataBoundIn the event, write the code. The straightforward method is not very good,EvalThe binding syntax is not enough.Asp.netThere is no need to put all the things that can be done on the page.CsGo to the page.

 

Optimized page:

<Asp: repeater ID = "Repeater1" runat = "server"> <HeaderTemplate> <table class = "no-style full"> <thead> <tr> <th> information (key) </th> <th> information (value) </th> </tr> </thead> <tbody> </HeaderTemplate> <ItemTemplate> <tr> <td> <% # Container. dataItem %> </td> <% # Request. serverVariables [Container. dataItem. toString ()] %> </td> </tr> </ItemTemplate> <FooterTemplate> </tbody> </table> </FooterTemplate> </asp: Repeater>

 

Last words:

After the optimization, the code is much more neat and tidy, saving no time, and the mood is much better, so it is recorded. -,-

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.