Solved

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

Posted on 2003-12-01
6
1,481 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query Join 9 61
add custom headers to my mail 2 48
Server Timeout with Loop 6 38
Change visitor's REMOTE_ADDR to server's REMOTE_ADDR 2 19
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now