Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
2.50/5 (4 votes)
See more:
Hi All,

XML
<%if (Convert.ToInt32(Eval("article_comment_counter")) > 0)
                                {%><a href="articles_comments.aspx?article_id=<%# Eval("article_id")%>"><%# Eval("article_comment_counter")%>comments</a> | <%}
                                else
                                {%><i>0 comments</i>

If I used this code then it shown error like this:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Please help me.

Thanks in Advance.

Can I do this in code behind?
If so, how?
I want to do in source page, if any one do then please help me.
Posted
Updated 11-Jun-11 1:15am
v4
Comments
Dalek Dave 11-Jun-11 7:15am    
Edited for grammar and readability.

Try to add a "#" between "<%" and "if ",if this code in a value of text .

Actually you can move these codes to .cs as a function, then you will find bug when you run the program.
 
Share this answer
 
v3
Comments
rahul dev123 10-Jun-11 7:51am    
I already use # but it shown error
Dalek Dave 11-Jun-11 7:16am    
I corrected a couple of spelling errors there, but also a good answer.
For Code Behind:

Way 1 : Remove the server code from source and keep the anchor tag in a panel or div with runat="server" attribute. So you can set the visible property from code behind after checking appropriate condition.

Way 2 : Rather than using anchor tag. Substitute it with hyperlink control. It will render the same anchor tag with the server side control over it. So you can hide show it from code behind using its visible property after checking appropriate condition.

And if it is necessary to do with Source only than replace it with following
<%#if (Convert.ToInt32(Eval("article_comment_counter")) > 0)
                                {%><a href="articles_comments.aspx?article_id=<%# Eval("article_id")%>"><%# Eval("article_comment_counter")%>comments</a> | <%}
                                else
                                {%><i>0 comments</i>


Hope it helps :)
 
Share this answer
 
Comments
rahul dev123 10-Jun-11 7:52am    
I already use this code but it shown error
Dalek Dave 11-Jun-11 7:17am    
Good call
Hi I hope this code help you little bit
XML
<span id="Span1" style='<%#(Convert.ToInt32(Eval("UserRole"))==1)? "display:none;": "display:inline;" %>'>
                               <asp:LinkButton CssClass="gray-btn" ID="LinkDelete" runat="server" CommandName="select"
                                   OnClientClick="Confrim(); if(confrim){return true;}else{return false;}"><img src="<%= Constants.RealUrl %>images/delete-icon.gif" alt="<%= delete %>" /> <%= delete%></asp:LinkButton>
                           </span>
 
Share this answer
 
Here is the link that might help you.
 
Share this answer
 
Comments
rahul dev123 10-Jun-11 3:51am    
There is no if else condition. my error is only for using if else
Dalek Dave 11-Jun-11 7:17am    
Not what op wants I think, but not a bad link on the subject.

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