Solved

using if else with Databinder.eval( ) is not letting the code compile

Posted on 2003-12-01
6
1,486 Views
Last Modified: 2008-03-10
I have an itemTemplate in a datagrid. I want display an image button or a simple hyper link based upon some field vlaue in "MATRIX_ITEM_TYPE". I simply put the if else condition but that does not seem to work. Any ideas would be highly appreciated. thanks...

<asp:templateColumn>
<itemTemplate>
<tr>
<td colspan=3 align=middle>
<%# if (DataBinder.Eval(Container.DataItem, "MATRIX_ITEM_TYPE") == 3){ %>
<asp:ImageButton ID="itemType" Runat="server" ImageUrl="images/items/custom_configuration.gif"></asp:ImageButton>
<%}else{%>
 <a href="">more info</a>
<%}%>      
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
0
Comment
Question by:rxraza
  • 3
  • 2
6 Comments
 
LVL 29

Assisted Solution

by:David H.H.Lee
David H.H.Lee earned 50 total points
ID: 9855422
rxraza,
Instead of
<%# if (DataBinder.Eval(Container.DataItem, "MATRIX_ITEM_TYPE") == 3){ %>

Change to
<% if (DataBinder.Eval(Container.DataItem, "MATRIX_ITEM_TYPE") == 3){ %>
-Eliminate "#". What you need is dynamically generate code based on selected criteria, not binding data into <ItemTemplate> in datagrid.

However, there are some others way to solve your problems as well. Please refer my post at this thread :
http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20806371.html

Regards
x_com
0
 
LVL 3

Accepted Solution

by:
vijay7248 earned 150 total points
ID: 9856272
Hi,
You can use a function and return the value u want ......

Try this workaround:
1. Declare a label inside ur item template like this:
      <ItemTemplate>
       <asp:label runat=server
Text='<%#getpath(DataBinder.Eval(Container.DataItem, "MATRIX_ITEM_TYPE").ToString)%>'>
       </asp:label>
     </ItemTemplate>

2. Then in ur server side script add the function getpath:
       function getpath(ItType As Integer) as string
            dim path as string
                  if ItType=3 then
                     path="<img border=0 src='images/items/custom_configuration.gif' alt='Picture Available'/>"
                  else
                     path="<a href='ur path here'>more info</a>"
                  end if
            return path
       end function


Using a function for this kind of needs will avoid much confusions in the code..

Hope this helps...  :)

-Vijay


0
 

Author Comment

by:rxraza
ID: 9860475
well guys:

Here is the situation. I have modified the code, now the code is as follows:
............
<ItemTemplate>
<%# getDetails(DataBinder.Eval(Container.DataItem, "MATRIX_ITEM_TYPE"))%>
</itemTemplate>
.......

Server Side function is as follows:

getDetails(matrixItemType){
   if (matrixItemType == 3){
      return "<asp:ImageButton ID=itemType Runat=server   ImageUrl=""images/items/custom_configuration.gif""></asp:ImageButton>"      
  }
  else{
     return "<a href="""">more details...</a>"
 }
}

The problem is it is returning the correct element based upon the value in the data grid column but on the page the <asp:ImageButton> is not being rendered.
Any idea about how to render this control on the page? thanks

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:vijay7248
ID: 9864413
Hi rxraza,
Happy to see u use my approach.

>>>>>>
The problem is it is returning the correct element based upon the value in the data grid column but on the page the <asp:ImageButton> is not being rendered.
Any idea about how to render this control on the page? thanks
>>>>>>

Why not try the ordinary HTML <img> tag  instead of using the <ASP:Image>
 button.Since u need the functionality of an image button u can put an <img> tag within <a href>.Something like this.......


getDetails(matrixItemType){
   if (matrixItemType == 3){
      return "<a href='currentpage.asp?gofunction=yes'><img border=0 src='images/items/custom_configuration.gif' alt='Picture Available'/></a>"
  }
  else{
     return "<a href="""">more details...</a>"
 }
}

Incase if u r accessing a function/sub (say Sub Test)   using the Image button.....
U can read the queryString in ur PageLoad function as::

           if Request.QueryString("gofunction")="yes" then
                     call Test
           end if

If u jus need to transfer to another page ...then give the filename in the a href..


Hope this works...     :)

-Vijay
0
 

Author Comment

by:rxraza
ID: 9868700
Thanks Vijay for a descriptive explanation. The solution that you mentioned is a workaround, which will always work.

Actually I wanted to know is it possible to render the <asp:ImageButton> once it is dynamically generated
OR
it could only be rendered by the 'dll' responsible for handling .aspx scirpts

thanks in Advance
0
 
LVL 3

Expert Comment

by:vijay7248
ID: 9874109
rxraza,
I'm sure there must be some way or the other to dynamically add a control through coding.
Since u r using Datagrid this could be slightly difficult.
(FYI: I'm new to .Net)

But the thing is, I've not tried that yet . sorry to say this :(
I dont have the time to test a code and give that to you as I've been put into a new project now.

I know you can do it..Jus try adding a control dynamically to the datagrid using some tutorials/helps from www...

or else you can do something similar using the AddColumn functionality of DAtagrids..

Hope this helps :)


-Vijay
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question