Solved

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

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

626 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