Repeater, GridView judgment in binding timing show different line styles or text _ Practical tips

Source: Internet
Author: User
One: Repeater or DataList controls
1. Change plain text content if the sex field in the Student Information table in the database is represented by 0 and a man but we want to repeat the control after the sex shows the male or female instead of showing 0 or 1
Method One: Of course we can judge and convert in SQL statements
Select (Case sex when 0 then ' men ' else ') as sex from Studentinfo
Method Two: is to use the Repeat Control ItemDataBound () event
Front desk
Copy Code code as follows:

<asp:repearter id= "Repeater1" runat= "Server" onitemdatabound= "Repeater1_itemdatabound" >
<ItemTemplate>
<tr>
<td>
<input type= "checkbox" >
</td>
<TD style= "Text-align:center" >
<% #Eval ("StudentID")%>
</td>
<TD style= "Text-align:center" >
<asp:label id= "Lblsex" runat= "Server" text= "" >
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
protected void Repeater1_itemdatabound (object sender, RepeaterItemEventArgs e)
{
DataRowView DRV = (DataRowView) e.Item.DataItem;
if (drv["sex"]. ToString () = = "0")
{
((Label) E.item.findcontrol ("Lblsex")). Text = "male";
}
Else ((Label) E.item.findcontrol ("Lblsex")). Text = "female";
}

2. Display different styles (CSS) based on the line conditions to be generated
For example, to show the behavior of a man a style, the generation of women's line is a style that is (if the Repeater control)
((Label) E.item.findcontrol ("Lblsex")). Cssclass= "Style_male";
Of course, you can also set the prompt ((Label) E.item.findcontrol ("Lblsex"). Tooltip= "I'm a man."
If it's a DataList control, you can also set the style for that line directly.
Copy Code code as follows:

private void Assets_itendatabound (object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
DataRowView drv= (DataRowView) e.Item.DataItem;
String m= (String) drv["ID"];
int i=_usermenubll.assetscss (m);
Switch (i)
{
Case 0:e.item.cssclass= "Nomal";
Case 1:e.item.cssclass= "Red";
Case 2:e.item.cssclass= "Purple";
Case 3:e.item.cssclass= "Yellow";
Case 4:e.item.cssclass= "Blue";
Case 5:e.item.cssclass= "Green";
}
}

If it is a GridView control, use the GridView1_RowDataBound
Set E. Row.cssclass Of course can also use E. Row.findcontrol method

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.