If statement in a repeater control

  • Thread starter Thread starter Fernando Chilvarguer
  • Start date Start date
F

Fernando Chilvarguer

My problem:

I have a field in a database table that contains URL info.
The values can be:
1. A Url
2. Null

If the value is null I still need to display the record but not the url
link.

How do I accomplish this with the ASP.NET repeater control?

In regular ASP I would just do something like this:

<% IF CONDITION THEN%>
HTML HTML HMTL
<%ELSE%>
ALTERNATE HTML
<%END IF%>

Thanks,
Fernando
 
Hi Fernando,

As for the problem you mentioned, it is a general databinding scenario in
asp.net. First , we should see that the asp.net has completely different
programming model with the classic asp. In asp, we render all the html code
manually via script code. In asp.net, we using Databinding mechanism when
we need to display list of datas retrieved from db and contained in a
certain recordcontainer(dataset or datareader). And there're some buildin
controls for databinding display ,(datagrid/datalist/repeater). What we
need to do is specifying the databinding expression in the control's
template and assign the datasource at runtime. The asp.net databinding
expression is something like:

<%# data binding expression %>

#Data Binding Expression Syntax
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/ht
ml/cpcondatabindingexpressionsyntax.asp

and we can define our own helper functions in page class and call them in
the <%# %> binding block. So as for the problem you mentioned , we can
define a helper function in the code behind such as string
checkValue(object)
{
if(object is dbnull)

return otherthing
else
return url
}

and call it in repeater's template as below:

<ItemTemplate>
..
<%# checkValue(DataBinder.Eval(..)) %>
..

</ItemTemplate>

Hope helps. If you have anything else unclear, please feel free to post
here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
HI Steven,
Thank you so much, it worked great!!

But I ran into another problem due to my lack of .NET experince (so hard to
break away frorm traditional ASP).

Here's the problem: I created the "checkValue" per your sugestion. To
organize the code I try to put all static helper functions like this one in
a separate class, let's call it "myStaticClass".

On the code behind of my page I did add the "using" statement and if I try
to access the function from the code behind it works fine.
Now, If I try to access the function from the .aspx page, it does not find
it
Code:
<% # myStaticClass.checkValue(object) %>

To solve the problem I created a wrapper function on the code behind to call
the helper funcion from the class and it worked fine but I can't imagine it
to be the correct and elegant way of doing it.

Any suggestions are greatly appreciated!!!

Thank again,
Fernando
 
Hi Fernando,

Thanks for your followup. I'm glad that my suggestions are of assitance.
As for the new problem you mentioned, that is because when you call a
class(no the codebehind page class)'s method, the page can't find it by
default. We can directly use the codebehind class's methods( protected or
public ones) because the actual asp.net page's class is a derived class of
the codebehind class. We don't need to import namespace for it. If we want
to call other class's member function, we need explicitly import the
class's name space. For example:

<%@Page ....%>
<%@ Import Namespace="MyWebApp.DataBinding" %>
................
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp:TextBox id="txtOne" runat="server" Text='<%# DataHelper.GetText()
%>'>
</asp:TextBox>
.......


The DataHelper Class is under the MyWebApp.DataBinding namespace, so I
explicity import its namespace. Using the <%@Import ..%> directive

http://msdn.microsoft.com/library/en-us/cpgenref/html/cpconImport.asp?frame=
true

Also, we can call the class with its full namespace so that we don't need
to import it , such as
<asp:TextBox id="txtOne" runat="server"
Text='<%# MyWebApp.databinding.DataHelper.GetText() %>'>

</asp:TextBox>

If you have anything else unclear, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Once again, thank you!!!

And of course, I have another one for you. (and I promise as soon as I'm
proficient enough in .NET I'll help others too.)

This one is regarding name spaces.

On my development machine, I created my ASP.NET project under the "AAANEW"
directory.

Every file created inside the project uses the "AAANEW" namespace.

My production server directory is "AAA" (without the new) and I want the
namespace references everywhere in my project to be only "AAA".

How do I accomplish this? Is there any tool in VS that does that? Can I just
do a global search and replace?

Thanks,
Fernando
 
Hi Fernando,

Thanks for your followup. As for the changing namespace question you
mentioned, it's different depending on the project's type you use. C# or
VB.NET:

1.For C# project, vs.net specify the namespace in each .cs file , you can
find
namespace XXXX
{


}

arround the classes in each cs file. Thus, if we want to change the
namespace for a whole project, we need to do manual replace in all the cs
files

2. In VS.NET projects, all the namespace are centrally set in the project
setting(rather than explicitly in each .vb source file). You can open the
project's properties window and find the "Root namespace" setting, we can
change this and rebuild the whole project.

Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Back
Top