Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all;

How i can use if statement to display data in listview. I am giving my code below.

XML
<asp:ListView ID="ListView1" runat="server" DataSourceID="Datasource">
    <AlternatingItemTemplate>
        <li style="background-color: #FFF8DC;">Question:
            <asp:Label ID="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' />
            <br />
            Option1:
            <asp:Label ID="Option1Label" runat="server" Text='<%# Eval("Option1") %>' />
            <br />
            Option2:
            <asp:Label ID="Option2Label" runat="server" Text='<%# Eval("Option2") %>' />
            <br />
            Option3:
            <asp:Label ID="Option3Label" runat="server" Text='<%# Eval("Option3") %>' />
            <br />
            Option4:
            <asp:Label ID="Option4Label" runat="server" Text='<%# Eval("Option4") %>' />
            <br />
            Correct: Option
            <asp:Label ID="CorrectLabel" runat="server" Text='<%# Eval("Correct") %>' />
            <br />


                        </li>


    </AlternatingItemTemplate>



Here, If Correct= 1 Then OptionLabel1.color = Red.
How i can write code for this, Please Help me to write code.

Thank you.
Posted
Updated 5-Jun-12 21:35pm
v3
Comments
Sandeep Mewara 6-Jun-12 4:13am    
What is Correct? What logic are you talking of. Please be clear.

try this:
XML
<asp:label color="<%# ((int)Container.DataItem). == 1 ? "red" : "Green" %>" id="CorrectLabel" runat="server" text="<%# Eval("Correct") %>" xmlns:asp="#unknown" />
 
Share this answer
 
Read this article about how to use inline if condition to swtich between cell background colors.

http://stackoverflow.com/questions/5313790/asp-net-c-sharp-listview-inline-conditions[^]


Hope this helps , If yes then plz accept and vote the answer. Any queries / questions on this are always welcome.

Thanks & Regards
RDBurmon.Sr.Software Engineer
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900