Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Writing Code to Format Data (ListView|ItemTemplate)

Reply
Thread Tools

Writing Code to Format Data (ListView|ItemTemplate)

 
 
Jonathan Wood
Guest
Posts: n/a
 
      05-16-2009
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

 
Reply With Quote
 
 
 
 
Ratnesh Maurya
Guest
Posts: n/a
 
      05-16-2009
On May 16, 10:38*pm, "Jonathan Wood" <jw...@softcircuits.com> wrote:
> 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
 
Reply With Quote
 
 
 
 
Stan
Guest
Posts: n/a
 
      05-17-2009
On 16 May, 18:38, "Jonathan Wood" <jw...@softcircuits.com> wrote:
> 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
 
Reply With Quote
 
Jonathan Wood
Guest
Posts: n/a
 
      05-17-2009
"Stan" <> wrote in message
news:0ea3b2e7-91b0-4a08-bfcd-...

> 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

 
Reply With Quote
 
Jonathan Wood
Guest
Posts: n/a
 
      05-17-2009
"Ratnesh Maurya" <> wrote in message
news:2b357c55-78a2-4d84-9bbb-...

> 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

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      05-18-2009
On May 16, 7:38*pm, "Jonathan Wood" <jw...@softcircuits.com> wrote:
> 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)%>
 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      05-18-2009
On May 17, 2:06*am, Stan <googles...@philhall.net> wrote:
> On 16 May, 18:38, "Jonathan Wood" <jw...@softcircuits.com> wrote:
>
>
>
> > 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


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.
 
Reply With Quote
 
Jonathan Wood
Guest
Posts: n/a
 
      05-19-2009
> 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

"Alexey Smirnov" <> wrote in message
news:02961159-5f95-4589-97dd-...
On May 17, 2:06 am, Stan <googles...@philhall.net> wrote:
> On 16 May, 18:38, "Jonathan Wood" <jw...@softcircuits.com> wrote:
>
>
>
> > 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


 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      05-19-2009
On May 19, 6:32*pm, "Jonathan Wood" <jw...@softcircuits.com> wrote:
> > 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
>
> "Alexey Smirnov" <alexey.smir...@gmail.com> wrote in message
>
> news:02961159-5f95-4589-97dd-...
> On May 17, 2:06 am, Stan <googles...@philhall.net> wrote:
>
> > On 16 May, 18:38, "Jonathan Wood" <jw...@softcircuits.com> wrote:

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


http://msdn.microsoft.com/en-us/libr...databound.aspx
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading raw data and writing out in EBCDIC format Ven C++ 6 07-14-2006 03:51 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Slow code writing data to table JP SIngh ASP General 1 10-01-2004 11:48 AM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
need some advice on writing a custom format event handler for data-bound Fields Larry ASP .Net Web Controls 3 04-21-2004 03:44 AM



Advertisments