Writing Code to Format Data (ListView|ItemTemplate)

  • Thread starter Thread starter Jonathan Wood
  • Start date Start date
J

Jonathan Wood

I have a Web form with a ListView control and, since my formatting is a
little involved, I would like to perform that formatting from code. To this
end, I changed my ItemTemplate from this:

<tr runat="server">
<td>
<%#Eval("Name")%>
</td>
</tr>

to this:

<tr runat="server">
<td>
<%#FormatName(Container)%>
</td>
</tr>

And I wrote a FormatName() method that returns a string. This worked okay
but it occurs to me that it would be quite a bit more efficient if, instead
of building and returning a string, if my method simply emitted the text
directly using Response.Write().

In this case, I need to call my method but not display the result. So I
tried changing my ItemTemplate to:

<tr runat="server">
<td>
<% FormatName(Container); %>
</td>
</tr>

But now I get the error that Container is not available in the current
context.

Is there any way to access the current ListViewDataItem this way?

Thanks.

Jonathan
 
I have a Web form with a ListView control and, since my formatting is a
little involved, I would like to perform that formatting from code. To this
end, I changed my ItemTemplate from this:

<tr runat="server">
   <td>
      <%#Eval("Name")%>
   </td>
</tr>

to this:

<tr runat="server">
   <td>
      <%#FormatName(Container)%>
   </td>
</tr>

And I wrote a FormatName() method that returns a string. This worked okay
but it occurs to me that it would be quite a bit more efficient if, instead
of building and returning a string, if my method simply emitted the text
directly using Response.Write().

In this case, I need to call my method but not display the result. So I
tried changing my ItemTemplate to:

<tr runat="server">
   <td>
      <% FormatName(Container); %>
   </td>
</tr>

But now I get the error that Container is not available in the current
context.

Is there any way to access the current ListViewDataItem this way?

Thanks.

Jonathan

Hi Jonathan,

How about using your "FormatName" method in databind event of
listview.

Cheers,
-Ratnesh
 
I have a Web form with a ListView control and, since my formatting is a
little involved, I would like to perform that formatting from code. To this
end, I changed my ItemTemplate from this:

<tr runat="server">
   <td>
      <%#Eval("Name")%>
   </td>
</tr>

to this:

<tr runat="server">
   <td>
      <%#FormatName(Container)%>
   </td>
</tr>

And I wrote a FormatName() method that returns a string. This worked okay
but it occurs to me that it would be quite a bit more efficient if, instead
of building and returning a string, if my method simply emitted the text
directly using Response.Write().

In this case, I need to call my method but not display the result. So I
tried changing my ItemTemplate to:

<tr runat="server">
   <td>
      <% FormatName(Container); %>
   </td>
</tr>

But now I get the error that Container is not available in the current
context.

Is there any way to access the current ListViewDataItem this way?

Thanks.

Jonathan

Hi Jonathan

As I understand it, the databinding process will evaluate expressions
declared in an item template but will not execute statements. Hence
your first revised code worked but not the second.

Response.write() is inappropriate here. That will just feed the output
of your code directly into the html stream in the wrong order. If you
are going to use web server controls from the standard class library
you have to let the system handle the rendering for you.

HTH
 
As I understand it, the databinding process will evaluate expressions
declared in an item template but will not execute statements. Hence
your first revised code worked but not the second.

Response.write() is inappropriate here. That will just feed the output
of your code directly into the html stream in the wrong order. If you
are going to use web server controls from the standard class library
you have to let the system handle the rendering for you.

If you place a function call in the HTML the way I showed, and that function
calls Response.Write(), the written data will appear at the location where
the function call was inserted. It's easy to try if you doubt this.

Jonathan
 
How about using your "FormatName" method in databind event of
listview.

Yes, that's another approach to look at. I'm not sure it's what I want but
I'll look into it.

Thanks!

Jonathan
 
I have a Web form with a ListView control and, since my formatting is a
little involved, I would like to perform that formatting from code. To this
end, I changed my ItemTemplate from this:

<tr runat="server">
   <td>
      <%#Eval("Name")%>
   </td>
</tr>

to this:

<tr runat="server">
   <td>
      <%#FormatName(Container)%>
   </td>
</tr>

And I wrote a FormatName() method that returns a string. This worked okay
but it occurs to me that it would be quite a bit more efficient if, instead
of building and returning a string, if my method simply emitted the text
directly using Response.Write().

In this case, I need to call my method but not display the result. So I
tried changing my ItemTemplate to:

<tr runat="server">
   <td>
      <% FormatName(Container); %>
   </td>
</tr>

But now I get the error that Container is not available in the current
context.

Is there any way to access the current ListViewDataItem this way?

Thanks.

Jonathan

No, you need to use <%#FormatName(Container)%>
 
I have a Web form with a ListView control and, since my formatting is a
little involved, I would like to perform that formatting from code. To this
end, I changed my ItemTemplate from this:

<tr runat="server">
   <td>
      <%#Eval("Name")%>
   </td>
</tr>

to this:

<tr runat="server">
   <td>
      <%#FormatName(Container)%>
   </td>
</tr>

And I wrote a FormatName() method that returns a string. This worked okay
but it occurs to me that it would be quite a bit more efficient if, instead
of building and returning a string, if my method simply emitted the text
directly using Response.Write().

In this case, I need to call my method but not display the result. So I
tried changing my ItemTemplate to:

<tr runat="server">
   <td>
      <% FormatName(Container); %>
   </td>
</tr>

But now I get the error that Container is not available in the current
context.

Is there any way to access the current ListViewDataItem this way?

Thanks.

Jonathan

No, you need to use <%#FormatName(Container)%>
 
Hi Jonathan

As I understand it, the databinding process will evaluate expressions
declared in an item template but will not execute statements. Hence
your first revised code worked but not the second.

Response.write() is inappropriate here. That will just feed the output
of your code directly into the html stream in the wrong order. If you
are going to use web server controls from the standard class library
you have to let the system handle the rendering for you.

HTH

One more thing - you can also use DataBound method to set values from
the code behind. This would help to get asp.net markup <%# %> away
from the html code.
 
Hi Jonathan

As I understand it, the databinding process will evaluate expressions
declared in an item template but will not execute statements. Hence
your first revised code worked but not the second.

Response.write() is inappropriate here. That will just feed the output
of your code directly into the html stream in the wrong order. If you
are going to use web server controls from the standard class library
you have to let the system handle the rendering for you.

HTH

One more thing - you can also use DataBound method to set values from
the code behind. This would help to get asp.net markup <%# %> away
from the html code.
 
One more thing - you can also use DataBound method to set values from
the code behind. This would help to get asp.net markup <%# %> away
from the html code.

Thanks, but did you meant DataBound *event*?

Jonathan
 
One more thing - you can also use DataBound method to set values from
the code behind. This would help to get asp.net markup <%# %> away
from the html code.

Thanks, but did you meant DataBound *event*?

Jonathan
 
Back
Top