If Statement in HTML

R

Rick

I am new to ASP.NET I have worked many years in Coldfusion. I am having a
problem finding out how to put an IF Statement into the HTML. How do I do
this in ASP.NET?

Thanks for any help you can give!!!

For example in CF if I want to put an IF Statement I would do something link
this.



<cfif khst.recordcount GT 0>
<tr>
<td class="text">Please Select a City </td>
<td width="181" height="15" align="left"
class="text"><cfselect name="city" query="khcity" value="city"
display="city" queryPosition="below" selected="#city#"
onChange="this.form.submit()">
<option value="PS" selected="selected">Please
Select a City</option>
</cfselect></td>
<cfif city NEQ "Please Select a City">
<td colspan="2" align="left" class="text">Number
of Matches <cfoutput>#khcityc.recordcount#</cfoutput></td>
<cfelse>
</cfif>
</tr>
</cfif>
 
S

Steve C. Orr [MVP, MCSD]

The new approach is to just use a DataGrid or GridView control.
There are also Table controls and Repeater controls & such if you need more
granularity.
 
C

clintonG

Same grammar we did in CFML, ASP and PHP but different syntax unique to the
scripting language(s) and while nested conditional logic is still supported
in ASP.NET ala <% ... %> most of us dropped that like a bad habit when we
discovered code-behind, web controls, and what are called user controls,
e.g. extensible #include files. Note there are also a number of expression
code blocks that remain in wide use as well as some new expressions for data
binding <%# ... %>.

I'd recommend mastering the page life cycle and the use of web and user
controls. The sooner you learn you are no longer scripting the sooner you
will be able to think using OOP designs and patterns.

Finally, HTML elements are usually referred to as HTML controls in ASP.NET
and are parsed the same way HTML elements are only much later in the page
life cycle. HTML controls can function as web controls when the attribute
pair runat="server" is declared which is why one must assume they are
usually referred to as HTML controls rather than HTML elements.

Being a convert I hope you had sufficient insight to choose C#.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
MAP 43°2'17"N 88°2'37"W : 43°2'17"N 88°2'37"W
 
R

Rick

I am not quite following you. Let's say I have a DataCombo control on a
page. Now depending on the choices the client chooses I may want to display
that DataCombo or not. Are you saying this is not done with an IF statement
any longer? Would I use something like the visible = True command or
something like that?
 
L

Laurent Bugnion

Hi,
I am not quite following you. Let's say I have a DataCombo control on a
page. Now depending on the choices the client chooses I may want to display
that DataCombo or not. Are you saying this is not done with an IF statement
any longer? Would I use something like the visible = True command or
something like that?

What he means is that you still can use conditional statements in the
ASPX page if you want (with the <% %> syntax, à la "old" ASP), but it's
not state of the art anymore.

You have different ways to choose to display a control or not.

- If you render the HTML code directly using a HtmlTextWriter (for
example in Custom Controls), then you can choose to render the HTML code
or not.
- If you use a Page of a User control, you can choose to add the said
control to the Controls collection or not.
- Finally, you can choose to always add the control to the Controls
collection, and use Visible to display it or not.

I am sure there are other ways too.

HTH,
Laurent
 
R

Riki

Laurent Bugnion said:
Hi,


What he means is that you still can use conditional statements in the ASPX
page if you want (with the <% %> syntax, à la "old" ASP), but it's not
state of the art anymore.

You have different ways to choose to display a control or not.

- If you render the HTML code directly using a HtmlTextWriter (for example
in Custom Controls), then you can choose to render the HTML code or not.
- If you use a Page of a User control, you can choose to add the said
control to the Controls collection or not.
- Finally, you can choose to always add the control to the Controls
collection, and use Visible to display it or not.


For clarity, from all these different ways, the last one will be used in 99%
of the cases.
 
S

Steven Cheng[MSFT]

Hello Rick,

I agree with Laurent, in ASP.NET you have many choices to finish the work,
however, we recommend that you make use of the ASP.NET server controls and
use control model and postback events to manipulate control states.

For your scenario, you can use the DropDownList control to display list
items and use the dropdownlist control's "SelectedIndexChanged" event to
change other control's states/properties:

#DropDownList Class
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownl
ist.aspx


for example, here is a test page which contains two dropdownlist, when the
first dropdownlist's selected item has changed, the second dropdownlist
will be hidden or showed according to the first list's selected value:(I've
also attached the complete page's aspx and cs file in this message, you can
get it if you're using Outlook express to visit the newsgroup):

==============aspx page===================

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" /><br />
<br />



ASP.NET server control means:<br />
<hr />
&nbsp;<asp:DropDownList ID="lstServer1" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="lstServer1_SelectedIndexChanged">
<asp:ListItem>show</asp:ListItem>
<asp:ListItem>hide</asp:ListItem>

</asp:DropDownList>
<asp:DropDownList ID="lstServer2" runat="server">
<asp:ListItem>aaa</asp:ListItem>
<asp:ListItem>bbb</asp:ListItem>
<asp:ListItem>ccc</asp:ListItem>
</asp:DropDownList>
<br />
</div>
</form>
</body>
</html>

======code behind (c#) ====================
public partial class databind_ListControlPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}



protected void lstServer1_SelectedIndexChanged(object sender, EventArgs
e)
{
if (lstServer1.SelectedIndex == 0)
{
lstServer2.Visible = true;
}
else
{
lstServer2.Visible = false;
}
}
}

==========================


Please feel free to let me know if there is anything unclear or if there is
anyother information you wonder.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

clintonG

It will come together much easier and perhaps faster if you take what I
suggested more seriously than simply picking up what sounds like an
immediate tip or two. That is all fine and well but will still leave you
coding in a shroud of mystery of how ASP.NET actually works until you find
and study all you can about the page life cycle, the control life cycle and
how the page contents are compiled. Especially if you intend to use Master
Pages.


<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
MAP 43°2'17"N 88°2'37"W : 43°2'17"N 88°2'37"W
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,113
Latest member
Vinay KumarNevatia
Top