• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Simple ASP.NET question

StageLeft

No Lifer
In old school asp if you had a number of links that were going to be displayed - say a list of 1 to 50, although sometimes 3 links and other times 35, or whatever, you could easily loop through something and do a response.write of the appropriate html.

Now, in asp.net what would be the typical way of approaching this? I can make an appropriate method which can make as many link objects as I want and it can set their positioning properties, text, etc. and whatever else, but is there a typical way of going about this? It feels a bit cumbersome to make my own method which would create a link and then set its position as 20 pixels (for instance) below the next one each time and set all of the other required properties, which may be different each time as I crank through a loop.

ALSO: if I want to display some other object at the end of this variable-length link list how would I do that? As opposed to tracking some pixel position variable I'd think the best way to do it would be to simply put that table in the right spot at design time, but this would only work if there is some sort of a placeholder link object that I could put before it - and this link object could take all of the different links or whatever I wanted.

I know the above is not very clear, sorry 🙂
 
The data binding server controls of ASP.NET will give you what you need. You can override their default binding behaviors, rendering behaviors, ad nauseum. Displaying anything at the "end of the list" would be no more than putting an additional control beyond the data control. It will render the data list as appropriate then the other object at the end. You could even throw this into a user control and have it rendered as a single unit.

Hopefully that gives you something to go on 🙂 I can't really give you any examples without knowing more specifics. Have a look at the DataList class as a starting point though. Also check the QuickStarts for data binding controls.
 
Thanks! That's definitely what I'm looking for. To be more specific let's say I have at the top of a page a list of links of unknown length. Next I want to have some other object like a table. I want the table to always come right after the list. So, are you saying that the way I'd do this would be to, at design time, put my first link object at the top of the page, followed by my table...then at run time I can have as many link objects as I want created and I can bind them all to that design-time link, and they will automatically come after it?

BTW I'm no longer at work, so I can't play with it here 😉
 
Yes, that's correct. Here's a contrived example:

<asp😀ataList id=&quot;links&quot; runat=&quot;server&quot;/>
<table>
<tr>
<td>This will always be after the drop down list renders</td>
</tr>
</table>

Of course I don't recommend tables; rather, divs/spans and CSS, but that's not the question here.

The data binding controls have a DataSource property that will allow you to reference an object that implements IEnumerable (DataView, DataTable, ArrayList, etc.). All you need to do is call DataBind() and your server control will be rendered as appropriate along with the table that follows.

[edit]It parsed the emoticons, so I turned it off[/edit]
 
Thanks again 🙂 I was just looking at this and he was talking about the repeater control. It seems similar to what you described with the datalist control, and I presume that the code would be similar to implement functionality in either one of them (at a basic level).

I see that in both cases I can set the datasource of the control to something and then, based on the templates I create, it will spit out the appropriate number of whatever I want. Then, whatever object happens to be placed after it in design view will be printed out next.

A couple of questions though if you could😀 (I decided to play with it at home afterall!):

1) If I'm not grabbing the data from a database and I don't have a datareader to use as the datasource how can I manually add items to the datalist, repeater, etc.? If I have a datasource I just set that, then databind, and we're happy. However, if I wanted to create links myself in code, maybe just looping through simple code how do I "bind" these to the datalist? I tried datalist.controls.add and then for the add object putting in a valid hyperlink...and it does add the link; it just doesn't display anything at runtime. BtW I tried that without specifying a template at all, assuming that it would be a simple hyperlink spitout.

2) All of the above stuff aside, let's say I want to create a hyperlink or whatever object at runtime, and there is no definition for it set at design. So, I create a hyperlink object and set its text and its navigateurl properties. How do I render the damn thing if there is nothing set for it at design time? I'm guessing the .rendercontrol method, but that seems to want a writer object of some sort...

As you can see I'm quite new with asp.net and it's taking me a while to get my mind out of the ingrained asp with vbscript thinking 😱 Thanks, MS guru!
 
Originally posted by: Skoorb
Thanks again 🙂 I was just looking at this and he was talking about the repeater control. It seems similar to what you described with the datalist control, and I presume that the code would be similar to implement functionality in either one of them (at a basic level).

I see that in both cases I can set the datasource of the control to something and then, based on the templates I create, it will spit out the appropriate number of whatever I want. Then, whatever object happens to be placed after it in design view will be printed out next.

A couple of questions though if you could😀 (I decided to play with it at home afterall!):

1) If I'm not grabbing the data from a database and I don't have a datareader to use as the datasource how can I manually add items to the datalist, repeater, etc.? If I have a datasource I just set that, then databind, and we're happy. However, if I wanted to create links myself in code, maybe just looping through simple code how do I "bind" these to the datalist? I tried datalist.controls.add and then for the add object putting in a valid hyperlink...and it does add the link; it just doesn't display anything at runtime. BtW I tried that without specifying a template at all, assuming that it would be a simple hyperlink spitout.

You can databind to any object that implements the IEnumerable, ICollection, or IListSource interface, you could use an ArrayList(). e.g.:

ArrayList links = new ArrayList();
HtmlAnchor anchor = new HtmlAnchor();
anchor.Name = "Test";
anchor.HRef = "http://somewhere.com";
// etc.
links.Add(anchor);

You can then bind it as follows:

yourRepeaterObject.DataSource = links;
yourRepeaterObject.DataBind();

Did you create your own class for the hyperlink, or are you using the HtmlAnchor class? What you do within the repeater will depend on what class you're using.

2) All of the above stuff aside, let's say I want to create a hyperlink or whatever object at runtime, and there is no definition for it set at design. So, I create a hyperlink object and set its text and its navigateurl properties. How do I render the damn thing if there is nothing set for it at design time? I'm guessing the .rendercontrol method, but that seems to want a writer object of some sort...

The RenderControl method does come into play, but ASP.NET Pages render by calling all of the RenderControl methods of the Control instances in its Controls collection. So, you need to get the instance of your control into the Controls collection of the Page instance before it's rendered. The first state in an ASP.NET Page lifecycle is Init, so you'll need to either override the protected OnInit method or register with the Init event. e.g.:

<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Init(object sender, EventArgs e)
{
TextBox t = new TextBox();
t.ID = "TestTextbox";
TestForm.Controls.Add(t);
}
</script>
<html>
<body>
<form id="TestForm" runat="server">
</form>
</body>
</html>

Note that I normally use codebehinds, but the above makes it clearer.

As you can see I'm quite new with asp.net and it's taking me a while to get my mind out of the ingrained asp with vbscript thinking 😱 Thanks, MS guru!

Kind word appreciated, and no problem 🙂 Let me know if that helps.

[edit]Formatting. It auto-parsed the URLs[/edit]
 
Back
Top