Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 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