Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-04
5
Medium Priority
?
787 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 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