Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

problem with conditional output in templates

  Asked By: Trupti    Date: Feb 06    Category: Asp.net    Views: 1204
  

I have a question about handing data within a template definition for a
DataList. In my aspx I'm trying to output different values surrounding a
datafield depending on the value of another datafield. Here's a glimpse of
code of what I'm trying to accomplish:


<asp:DataList ...>
<ItemTemplate>
<# DataBinder.Eval(Container.DataItem, "bBooleanField") %><br/>
<% if ((bool) DataBinder.Eval(Container.DataItem, "bBooleanField"))
{ %>true<% } else { %>false<% } %>
</ItemTemplate>
</asp:DataList>

If the field "bBooleanField" for that row is true, I'd like "true" to be
printed on that line, otherwise "false" --- if I can get past this point
I'll be replacing those "true|false" strings with other DataBinder
expressions - but I need to get this far first. The error I'm getting when
doing this is "The type or namespace name 'Container' could not be found
(are you missing a using directive or an assembly reference?)". I've tried
a few variations of syntax & adjusting the <% and <%# tags but with no luck.

Anyone have any ideas how this can be done? As you've probably guessed from
the if statement - I'm using c# & doing most of the coding in code-behind
pages.

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Alan Palmer     Answered On: Feb 06

The problem  is that you're using this syntax  <% %> which doesn't work as
databinding, so you don't have access to Container.DataItem (the item
bounded). This syntax <%# .... %> is used for databinding purpose

So what you would have to do is


<asp:DataList ...>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "bBooleanField") %><br/>
<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem,
"bBooleanField")).ToString() %>
</ItemTemplate>
</asp:DataList>

Note this:

DataBinder.Eval(Container.DataItem, "bBooleanField")

this return an Object type, so you would have to use Convert.ToBoolean
to get a Boolean type  and then use the ToString() method of the Boolean
type to get True or False

 
Answer #2    Answered By: Guadalupe Rogers     Answered On: Feb 06

I had that problem, I figured you can do this ::

Firstly the problem  arises because when you have called
if ((bool) DataBinder.Eval(Container.DataItem, "bBooleanField"))
and it doesn't know what Container is because the line  is not databound ...
which is signified by the #.

My fix for it was this
You can call a function inputting the databound value
<%# MySupp((string)DataBinder.Eval(Container.DataItem, "suppname"))%>

then from that function (in code behind) you can return the html you need to
make your page work, mine was


public string MySupp(string strg)
{
if (supp != strg)
{
supp = strg;
str = "<tr><td valign=middle colspan=4><font class=supplier>";
str += strg;
str +=" </font></td></tr>";
return str;
}
else
{
supp = strg;
return "";
}
}

It's sneaky isn't it ... and cool ... and took ages to figure out (by me I might
add ... so use it at your peril)
There may be better ways to do it ... but when you're dealing with templates  in
html based files, I can't think of one.

 
Answer #3    Answered By: Gustavo Taylor     Answered On: Feb 06

You may write a Helper Function to do so.
FYI: http://aspalliance.com/colt/articles/article1.aspx

 
Answer #4    Answered By: Velma Adams     Answered On: Feb 06

Why not writing just this line  of code:
<%# DataBinder.Eval(Container.DataItem, "bBooleanField").ToString() %>

But doesn't do the job the first line of code as well?

 
Answer #5    Answered By: Whitney Cruz     Answered On: Feb 06

however my final destination wasn't to
output the words "true" and "false" (although that's what i stated in the
original posting) --- but I was looking for a way to execute/output more
code within the if/else blocks (perhaps outputting more HTML with a
combination of other datafields). I think a helper function is the best way
to go in this situation.

 
Answer #6    Answered By: Asir Hashmi     Answered On: Feb 06

www.learnasp.com/.../databindingtricks.aspx
is another example of helper functions.

 
Answer #7    Answered By: Saxon Anderson     Answered On: Feb 06

One thing to note with these things ::

Its easy to do this


<table>
<tr><td></td></tr>
<tr>
<# MyHelper(.......) >
</tr>
</table>

Myhelper(){
return "<td>hello</td>
}

which works at runtime but will play havoc with the VS designer. It will
probbaly fill in the missing  bits and add a TBODY and all sorts, and be a real
pain.
So make sure what's in the designer is valid html, even if that means outputting
more from the helper function.

<table>
<tr><td></td></tr>
<# MyHelper(.......) >
</table>


Myhelper(){
return "<tr><td>hello</td></tr>"
}

as if you didn't know already.

 
Didn't find what you were looking for? Find more on problem with conditional output in templates Or get search suggestion and latest updates.




Tagged: