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. -,-