how do I "write" an ASP.Net onto the page in code?

B

bennett

How can I do a loop from 1 to 5, where on line 1 I print 1 space
followed by a button, on line 2 I print 2 spaces followed by a button,
on line 3 I print 3 spaces followed by a button, etc.?

I tried this code:

for (int i = 1; i <= 5 ; ++i)
{
for (int j = 1; j <= i; ++j)
{
Response.Write("&nbsp;");
}
Response.Write("<asp:Button id=\"Button" +
i.ToString() +
"\" runat=\"server\" Text=\"Button\"></asp:Button>" +
"<br>\n");
}

but that didn't work, since Response.Write just sent the characters
"<asp:Button" etc. to the browser; ASP.Net did not interpret them.

Or maybe someone could think of a better way to do what I'm trying to
do:

I have a database table of message posts; some messages are "replies"
to other messages, and each record in the table has a parent_post_id
that refers to the ID of the parent post, if there is one. What I'm
actually trying to do is display the messages in a hierarchical way,
i.e. the first top-level post, followed by all of its direct replies
that are indented by X spaces, and each reply is also followed by all
of *its* replies indented by 2X spaces, etc., the familiar way in which
a hierarchy of message posts is usually displayed. Each message post
is displayed as a custom control.

The ideal solution would be to do it recursively, having a custom
control for a "post" that displays the current post and then has a list
of "post" controls for all the replies, indented X spaces further over
than the parent. This would result in all the replies to the replies
being displayed as well, all the way down to the last replies.
Unfortunately ASP.Net does not allow user controls to contain
themselves recursively.

So my plan is to do it iteratively: get the first top-level post, then
get a list of all its replies, then go through and print the replies,
but after checking each reply, check if *it* has any replies, and go
and print those... etc. But to do that I need a way to "print" the
user control that represents a message post. Hence my original
question.
 
B

Brock Allen

The server side cotnrol syntax like "<asp:Button>" is a declarative syntax
to tell ASP.NET what contorls you'd like on the page. This syntax is parsed
at compile time by ASP.NET (prior to the page actually handling a request).
Doing Respone.Write is during a request, and so it's far too late for the
ASP.NET parser to get involved.

Since this snippet of code you've provided is very much in the old ASP style
(and very much not in the ASP.NET style) I'd suggest doing some research
and reading on how the model's different. The ASP.NET QuickStarts is a great
place to get started:

http://www.asp.net/Tutorials/quickstart.aspx
 
B

bennett

I already know that the snippet of code I posted is not in the ASP.Net
style, I've written most of my project already using Visual Studio .Net
and using samples from books to do almost everything I needed to do. I
know how to populate datagrids and datarepeaters, and I don't have any
bad habits left over from ASP because I never worked with ASP :)

The trouble is that nothing in the books or tutorials that I went
through, gave any indication of how to do something like what I
described above, where you want to display something recursively -- for
example, display a "message board post" user control, with all of its
reply posts nested below it, and all of the replies to the replies
nested below *them*, etc. The samples in the books are good for
learning how to work with rigid structures like datagrids, and even for
more flexible things like datarepeaters, but not for doing something
nested like the post hierarchy I described, where the best solution
would be a control that recursively contains itself except ASP.Net of
course does not allow those.

If using Response.Write() to write the controls is impossible, then
what would be the proper way to create a "recursive" structure like the
one described above? Like a datarepeater but with the rows in a
hierarchical relationship to each other instead of just a flat list.

Even just a function name or a control name would be enough to get me
started, if it's documented!
 
B

bennett

Thanks for the pointer. Unfortunately the site I'm building is not
just one where I want to add a discussion board, but I'm actually
writing a new *kind* of discussion board, so I pretty much have to
write it from the ground up, and I've got almost all of it done except
this one detail. I'd love to get ideas from the Community Server app,
but looking at the online demo at http://forums.asp.net/ it doesn't
look like they actually do the hierarchical indented display of form
posts the way I'm trying to set up mine. Looking at
http://forums.asp.net/914877/ShowPost.aspx
it's just a flat vertical display, basically a DataRepeater, and I know
how to do that.

I am not asking anyone to write any code for me but is there a
one-sentence description of *how* to do what I'm trying to do?

IF recursive user controls were possible, it could be done in one
sentence: "Make a custom control called 'RecursiveUserPost' that
displays the post's author, subject, and timestamp, and then contains a
DataRepeater that is indented over by a few spaces, and query for all
posts that have the current post as their 'parent' and use that to
populate the DataRepeater, and have the ItemTemplate for the
DataRepeater recursively contain an instance of RecursiveUserPost that
is bound to that data."

Is there any way to describe in one sentence how to do it given that
recursive user controls are not possible?
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top