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