ASP.NET and Customer HTML

  • Thread starter Thread starter Arsen Vladimirskiy
  • Start date Start date
A

Arsen Vladimirskiy

Hello,

What is the proper way to implement the following:

1) We have a list of customers
2) Customers can have some optional fields. For example, second phone
number, comments, and others.
3) We need to display the customers in a list, but in a nicely formatted
way. In other words, if the customer has a second phone, we want to show it
nicely with the extension (if it has one), if there is no second phone, we
do not want to have to says: Phone 2 #: empty. Same goes for all the other
optional fields.

If we are using a Repeater, what is the right way to achive this? Should we
put the if-statements inside the <%# %> blocks in the .aspx file?

For example, should we do this:

<%#
if (DataBinder.Eval(Container.DataItem,"phone2") != "")
{
Response.Write("Phone 2#: " +
DataBinder.Eval(Container.DataItem,"phone2"));
if (DataBinder.Eval(Container.DataItem,"phone2ext") != "")

Response.Write("&nbsp;x-DataBinder.Eval(Container.DataItem,"phone2ext"));
}
%>

This seems too messy. Is this the right approach to handling custom html
inside repeaters?

Thanks,
Arsen
 
The way I've done this, is to have a Label in the item template, and set its
Visible property to True or False depending on whether the field was null.
The text property would be bound either way - but if the label wasn't
visible, then it wouldn't matter.

This allows you to see at design time exacltly the layout, but at render
time, those customer's with the optional field filled in will have it
displayed, and those without it will not.

You can extend this to a Panel, with several web control inside it. You
could bind the controls, but set the Visible property on the Panel. So
either the whole thing is visible, or nothing is.

If you are going to use Response.Write's to achieve this, you might as well
be using ASP...
 
Hi Marina,

Did you change the Visible property to False or True in the code-behind?

Did you write a function to handle the OnItemCreated event of the Repeater?

Thanks,
Arsen
 
No, I did something like:

<asp:Label runat="server" Visible= '<%#
IIF(IsDBNull(Container.DataItem("MyCol")),False,True) %>' > <%#
Container.DataItem("MyCol") %> </asp:Label>

That's VB in the script, but easily changeable to C#.
 
What is the equivalent of the IFF() command in C#?
Did you write the IFF() yourself?
 
Use the ? expression evaluator.

<asp:Label runat="server" Visible= '<%#
IsDBNull(Container.DataItem("MyCol")? false : true) %>'>

Hope this helps :)

Jody Ananda
MCAD.NET,MCSD.NET
"All programs are poems, it just not all programmers are
poets."
 
Back
Top