Saturday, March 24, 2012

HTML Dependant on conditions

Hi,

Im just starting out with asp.net and struggling as im used to using PHP.

I understand how to display text in a label depending on conditions ive written in vb but how do i do the same for a block of html code?

Normally for me, in PHP i would do somthing like the following:

if (condition) { display this load of HTML } else { display some other HTML } - And that would be placed in the page where i wanted the HTML to be inserted.

How can i achieve the same as this in ASP.NET?

Thanks for your time

The best approach really depends on the nature of the condition, but you would essentially do the same in ASP.NET. However, the main difference is that you would place an asp:Literal control on the page, and conditionally set its Text value in your code:

if(condition)
{
myLitControl.Text = "some text or html";
}else{
myLitControl.Text = "some other text or html";
}

In other cases, this approach may be more suitable:How to conditionally show an image, or other control in ASP.NET

[EDIT]

The control goes where you want the text to appear (in design view). Your code to control its text value goes into a <script runat="server"> block at the top of the page, or better still, in the code behind.


Yea thanks, that works just fine. i put my code in the aspx.vb file and it works a treat.

Thanks again


VB? Really? Interesting choice. I would have thought you would be more comfortable with C#, since it is not hugely different to php. On the other hand, I chose C# because itwasn't like VBScript...


Hello,

<aw:repeater id="rptResolution" runat="server" LabelPosition="top" ColSpan="4" DataMember="History" DataSourceName="WorkItem" Visible='<%# DataStore("WorkItem").Tables(0).DefaultView(0)("workitem_resolution") <> " " %>' >

isn't it?

but not to

"stUBBEdit" RunAt="server" Template="subUBBEdit" ItemDataField="workitem_comment" Label="sidLblComment" Height="100px" Width="100%" ColSpan="3">

Am I right?

I used a sub Function for the second item

Sub Page_Load()If ddlStatus.SelectedValue ="400"Or ddlStatus.SelectedValue ="600"Then stUBBEdit.Visible =True stResolutionEdit.Visible =True Else stUBBEdit.Visible =True stResolutionEdit.Visible =False End If End Sub

Thanks

No comments:

Post a Comment