Solved

C#: "Splitting" C# Code and Writing HTML Between

Posted on 2004-08-04
5
774 Views
Last Modified: 2008-02-01
Is it possible to "split" C# code and writing HTML code between the parts, so I don't have to use Response.Write()? Like in this classic ASP sample:

<% If Banana = Fruit Then %>
HTML code here.
<% End If %>

Instead of

<%
If Banana = Fruit Then

  Response.Write("HTML code here.")

End If
%>

Please give me a/some sample/samples of the code that can be used.
0
Comment
Question by:Joakim_
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 11715197
Hello,

Here is what you are looking for:

<%
Dim fruit As string = "Banana"
if fruit = "Banana" then
%>
Banana
<%
else
%>
Other
<%end if%>

But remember if you try to make ASP.Net as ASP, then you do not really get all of its power and you will see the results as we see in ASP pages that ends with endless <%%> and if end if structure. Instead go with the code behind technique that is far better than we usually used to do in ASP earlier.

Best, Nauman
0
 
LVL 9

Expert Comment

by:msdixon
ID: 11716258
think OO, not scripting. there's no reason you need to embed inline vb.net code. use the code behind to cleanly separate the html & .net code.

a good way to do what you're asking is to conditionally set the text property of a label:

.aspx page:

...
<asp:label runat="server" id="lblFruit" />
...


.aspx.vb page:
' in your page_load method:

if fruit.ToLower().Trim() = "banana" then
   lblFruit.Text = "this is a banana"
else
   lblFruit.Text = "not a banana"
end if

i know... old habits die hard, but you'll make it a lot easier on yourself if you try to learn .net the right way, not converting existing asp code to asp.net.
0
 
LVL 2

Author Comment

by:Joakim_
ID: 11719445
I wasn't looking for what that If, Else code is in ASP.NET. This is an example of what I want:

<script runat="server">
      void Page_Load(Object Sender, EventArgs e) {
            switch (Fruit) {
                  case Banana :

SOME WAY TO TYPE HTML CODE HERE (WITHOUT USING Response.Write())

                  break;
            }
      }
</script>
0
 
LVL 9

Accepted Solution

by:
msdixon earned 100 total points
ID: 11722502
use a control... a panel is a good one. you can dynamically create and add other controls (a literal for instance) to a panel. or if you're trying to dynamically add javascript, theres the attributes property
0
 
LVL 2

Author Comment

by:Joakim_
ID: 11726535
Ahh, thanks. I do now understand that I can't use the solution I asked for answer to.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now