VirtualGirl
VirtualGirl

Reputation: 1

if else statement within formview

i have a FormView, it renders fields from a SQLServer database but also much static text specific to the database value I get. I dont know if this can be done in the code behind and put all this in a function . I put it in my formview

<ItemTemplate>

<% If Eval("Feature1") = "Yes" Then %>

 <%# Eval("Username") %> <p>A lot of text</p>

 <% elseIf Eval("Feature1") = "No" Then %>

 <%# Eval("Username") %>
<p>A lot of different text</p>

 <% End If %>

</ItemTemplate>

If I do the code above I get error message:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. Can this be done within the formview?

Many thanks for your help.

Upvotes: 0

Views: 2010

Answers (2)

Mike Dormitorio
Mike Dormitorio

Reputation: 51

You can use DataBinder.Eval as follows:

<% if (DataBinder.Eval(formview.DataItem, "First") == "Yes") { %>
    <p>A lot of text</p>
<% } else { %>
    <p>A lot of different text</p>
<% } %>

If you have any postback, you must store the DataItem somewhere and use that as a parameter of the DataBinder.Eval instead because it becomes null.

Upvotes: 2

Clarence Klopfstein
Clarence Klopfstein

Reputation: 4852

Your if logic should be in the code behind, in the databound event of your control.

Each row will fire this event, and you can then edit how the row should look based on the data.

protected void FormView1_DataBound(object sender, EventArgs e)
{

}

Upvotes: 0

Related Questions