views:

9

answers:

1

Im working on the following markup, inside a databound control in ASP.NET Webforms.

 <asp:TemplateField HeaderText="DomainID">
                <ItemTemplate>
                    <%  for (int i = 0; i < 10; i++)
                        {%>
                    <%#Eval("DomainID"); %>
                    <%  ++i;
                        } %>
                </ItemTemplate>
            </asp:TemplateField>

Is it possible to actually write code blocks inside the <%#Eval("DomainID"); %> section such as

<%# var x = Eval("DomainID"); if ((int)x)>0){//print this} %>

If im making any sense?

+1  A: 

When it starts getting this complicated I always recommend changing your structure to use a server side control and do the binding via the OnDataBinding event for that control.

Example:

<asp:TemplateField HeaderText="DomainID"> 
    <ItemTemplate> 
        <asp:Literal ID="ltDomainID" runat="server" 
            OnDataBinding="ltDomainID_DataBinding" />
    </ItemTemplate>

Then in your codebind implement the OnDataBinding:

protected void ltDomainID_DataBinding(object sender, System.EventArgs e)
{
    Literal lt = (Literal)(sender);
    for (int i = 0; i < 10; i++) 
    {
        var x = (int)(Eval("DomainID"));
        if (x > 0)
        {
            lt.Text += x.ToString();
        }
        ++i; 
    }
}

I might have your logic a little messed up but this should give you the basic idea. Implementing the logic server side keeps you aspx much cleaner and it ties the logic directly to the output control (which in this case is the Literal).

Kelsey
I think you more or less have my logic pinned. I didnt want to resort to writing in my code behind for something so simple, but it seems as tho i may have to.
maxp
@maxp I normally implement all my databinding in this manner with webforms because it keeps all the `code` out of the aspx and it makes it really easy to move and adjust the aspx on the fly. I also add a `#region` tag around all my databinding code so that it is all together and the next guy can find it easily without having to hunt for it.
Kelsey