if statement

S

SimonZ

I have dataRepeater.

How can I do:

<ItemTemplate>
<asp:TableCell >
<%If viewState("type") = "3" then%>
<%# writeLine(DataBinder.Eval(Container.DataItem,
"name")%>
<%else%>
<%# writeLine1(DataBinder.Eval(Container.DataItem,
"name1")%>
<%end if%>
</asp:TableCell>
</ItemTemplate>

The problem is that when viewState("type") =3 then I have different
dataSource.

If statement doesn't work like I write.

Any idea?

Thanks,S
 
K

Karl Seguin

It is possible to do inline formatting, but as the formatting gets more
complex, it's better to do it in codebehind hooking into the ItemDataBound
event.

You can learn more on both methods at:
http://openmymind.net/index.aspx?documentId=8#4

basically though, to do it your way, I would do:

<asp:literal runat="server" visible='<%# (ViewState("type") = "3")'
id="x"><%# DataBinder.Eval(Container.DataItem, "name") %></asp:literal>
<asp:literal runat="server" visible='<%# (ViewState("type") <> "3")'
id="x"><%# DataBinder.Eval(Container.DataItem, "name1") %></asp:literal>

As you can see, each literal will be visible based on whether the condition
is true or false.

Again, I would urge you to look into doing this in the ItemDataBound event.

Karl
 
S

SimonZ

Thank you Carl.

Do you know when to use databinding control event?

Or you always use item_dataBound event?

regards,
Simon
 
S

SimonZ

Hi Karl,

I choose ItemDataBound event

In your example you declare dataItem as new owner:
Owner owner = (Owner)e.Item.DataItem;

In my example, how can I read the value of the first column, for example:

string column1;

column1=e.Item.DataItem(1);

or something similar?

regards,S
 
K

Karl Seguin

Well, you can use DataBinder.Eval() in your code behind like you would in
your aspx, such as DataBinder.Eval(e.Item.DataItem, "Blah")

or, you can cast DataItem to the specific type. If you are binding from a
dataset, datatabele or dataview, you would do:

DataRowView dv = ctype(e.Item.DataItem, DataRowView)

dv("Column1")
dv(0)

Karl
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top