Click here to Skip to main content
15,119,363 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a user control which has code for asp.net repeater. I am using this control in other page. In the user control code behind I am using LING to SQL to get the data from database. In the repeater, I need to display a link for a specific key value. So, I need added an If statement in the .ascx page. Here is the IF condition code I am using,

XML
<% if (%> <%#(string)DataBinder.Eval(Container.DataItem, "key")%>= "50")
<%{%>
<a id="A1" href="http://www.google.com" target="_blank">test link</a>
<%}else{%>
<a id="A2"></a>
<%}%>


But it is showing error in this statement.

I have goggled and also tried this:

XML
<% if ((%> <%#(string)DataBinder.Eval(Container.DataItem, "key")%> <%).equals("50")) {
<a id="testmore" href="http://www.google.com" target="_blank" runat="server" >test link</a>
} %>


and this way:
XML
<a id="testmore" href="http://www.google.com" target="_blank"  runat="server" 
visible="<%if%>(<%#(string)DataBinder.Eval(Container.DataItem, "key")%><%= "50") ? true : false%>">test link 



But, none of this format are working correctly.

Can any one give me the correct format to write this statement. Please let me know if you need any more information.

Thanks in Advance.

Scorpion
Posted
Updated 13-Dec-12 8:08am
v2
Comments
[no name] 13-Dec-12 14:31pm
   
Can you specify the error you are getting ?
scorpion_dev 13-Dec-12 15:05pm
   
For the first format I am getting an error:

Invalid expression term 'else' and highlights <%}else{%>

Second format: CS1525: Invalid expression term ')' and highlights the line:

<% if ((%> <%#(string)DataBinder.Eval(Container.DataItem, "key")%> <%).equals("50")) {

1 solution

Save your string "key" value in a publicly declared variable in your server side event, then access it in your aspx code.

C#
public string myKey = string.Empty;


then in your aspx code

C#
<![CDATA[<% if (myKey == "50")
   {
%>]]>
<a id="A1" href="http://www.google.com" target="_blank">test link</a>
    <%}
   else 
   {
%>
<a id="A2"></a>
    <%}%>
   
Comments
scorpion_dev 13-Dec-12 17:47pm
   
Hi Sheikh,

Thanks for the reply.

As I told earlier, I am using this inline expressions inside a repeater. I am able to get the value in the code behind. But, I am not sure how can we apply this value to a single item inside repeater.

Also, is there any way we can do this completely using inline expressions (not using code behind).

Thanks

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