When binding repeater and gridview, determine whether different row styles or texts are displayed.

Source: Internet
Author: User

1. repeater or DataList Control
1. Change the plain text content. If the sex field in the student information table in the database uses 0 and 1 to represent men and women, but we want to bind the repeat control to show that the gender is male or female instead of 0 or 1.
Method 1: Of course, we can judge and convert in SQL statements
Select (case sex when 0 then 'male' else 'female' end) AS sex from studentInfo
Method 2: Use the repeat control ItemDataBound () event
Front-end
Copy codeThe Code is as follows:
<Asp: Repearter ID = "Repeater1" runat = "server" onitemdatabound = "repeaterincluitemdatabound">
<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 repeaterincluitemdatabound (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 row conditions to be generated)
For example, if you want to display the male row as a style, the female row generated is a style (if it is a repeater control)
(Label) e. Item. FindControl ("lblSex"). CssClass = "style_male ";
Of course, you can also set a prompt (Label) e. Item. FindControl ("lblSex"). ToolTip = "I Am a man"
If it is a DataList control, you can also set the style of that row directly.
Copy codeThe Code is 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"; break;
Case 1: e. Item. CssClass = "red"; break;
Case 2: e. Item. CssClass = "purple"; break;
Case 3: e. Item. CssClass = "yellow"; break;
Case 4: e. Item. CssClass = "blue"; break;
Case 5: e. Item. CssClass = "green"; break;
}
}

For the GridView control, use GridView1_RowDataBound.
Of course, the e. Row. FindControl method can also be used to set e. Row. CssClass.

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.