Outputting text when using code behind?

A

Andrei Pociu

I have a major doubt about outputting text in ASP .NET when using code
behind.

I know most of the output you gain from a code behind file (.aspx.cs) is
outputted to the Webform (.aspx) using labels, datagrids, datalists...
Also, I know you can output directly using Response.Write(). But this places
the output at the beginning of the file.

How can you output the text at a specific place in the HTML code? An example
would be when building a menu on a website and you retrieve the items from a
database using the code in a code-behind file.
I'm guessing that the solution solution has to be something like inserting a
custom tag into the aspx.cs file which will be replaced with the output
controlled from the aspx.cs file.

Thank you for your support.
 
G

GrantMagic

You can place a Literal Tag on the aspx page, and set the text value to be
anything you like.
Very much like a Label.

Alternately you can make a Control which retrieves the data in its own code
behind and this can be placed anywhere on your page.
 
A

Andrei Pociu

So every part of a website should be coded using different user controls?
For the menu I should use one user control, for the latest news box another
user control and so on?

Oh, and do you know some example website source code available which I can
download?

Thank you for your answers.
 
G

Guest

Andrei,

One more thought: you can use a "placeholder" control when you *know* where
you want something to go, then add to that control at runtime, e.g. (vb
syntax)

dim uc as new MyLabel
MyPlaceHolder.controls.add(uc)

Literals have a place, but I find the syntax of using placeholders easier to
swallow.

Re user controls, I use them extensively for a few main reasons: 1) Easy way
to reuse code within the site or across sites, 2) Unclutters the geography of
the page and 3) Easy to turn them off or swap them out at run-time (e.g. a
single "visible=false" can turn off a whole chunk of the ui). Other reasons
people use them is that they can be cached independently, and you can modify
them without redeploying the whole project.

hth,

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top