Solved

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

Posted on 2004-08-04
5
780 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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