Writing Code to Format Data (ListView|ItemTemplate)

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
 
R

Ratnesh Maurya

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
 
S

Stan

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
 
J

Jonathan Wood

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
 
J

Jonathan Wood

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
 
G

Guest

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)%>
 
G

Guest

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.
 
J

Jonathan Wood

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top