Writing Code to Format Data (ListView|ItemTemplate)

Discussion in 'ASP .Net' started by Jonathan Wood, May 16, 2009.

  1. 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
     
    Jonathan Wood, May 16, 2009
    #1
    1. Advertising

  2. On May 16, 10:38 pm, "Jonathan Wood" <> 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
     
    Ratnesh Maurya, May 17, 2009
    #2
    1. Advertising

  3. Jonathan Wood

    Stan Guest

    On 16 May, 18:38, "Jonathan Wood" <> 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
     
    Stan, May 17, 2009
    #3
  4. "Stan" <> wrote in message
    news:...

    > 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
     
    Jonathan Wood, May 17, 2009
    #4
  5. "Ratnesh Maurya" <> wrote in message
    news:...

    > 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
     
    Jonathan Wood, May 17, 2009
    #5
  6. On May 16, 7:38 pm, "Jonathan Wood" <> 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)%>
     
    Alexey Smirnov, May 18, 2009
    #6
  7. On May 17, 2:06 am, Stan <> wrote:
    > On 16 May, 18:38, "Jonathan Wood" <> 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.
     
    Alexey Smirnov, May 18, 2009
    #7
  8. > 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:...
    On May 17, 2:06 am, Stan <> wrote:
    > On 16 May, 18:38, "Jonathan Wood" <> 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
     
    Jonathan Wood, May 19, 2009
    #8
  9. On May 19, 6:32 pm, "Jonathan Wood" <> 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" <> wrote in message
    >
    > news:...
    > On May 17, 2:06 am, Stan <> wrote:
    >
    > > On 16 May, 18:38, "Jonathan Wood" <> 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/library/system.web.ui.webcontrols.listview.itemdatabound.aspx
     
    Alexey Smirnov, May 19, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bennett Haselton
    Replies:
    1
    Views:
    5,352
    Joe Fallon
    Sep 24, 2004
  2. =?Utf-8?B?RGFiYmxlcg==?=

    format datetime in GridView ItemTemplate?

    =?Utf-8?B?RGFiYmxlcg==?=, Apr 12, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    29,340
    =?Utf-8?B?RGFiYmxlcg==?=
    Apr 12, 2006
  3. David C

    ListView ItemDatabound data

    David C, Sep 24, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    1,301
    Alexey Smirnov
    Sep 27, 2009
  4. David C

    Getting unbound ListView data values

    David C, Nov 10, 2009, in forum: ASP .Net
    Replies:
    0
    Views:
    1,199
    David C
    Nov 10, 2009
  5. .
    Replies:
    0
    Views:
    738
Loading...

Share This Page