Click to See Complete Forum and Search --> : How to place dynamic asp links


jaklotz
August 22nd, 2005, 12:15 PM
Hi,

When I load a page, I loop through an array to decide what links I want in my menu, but I get an error because apparently you are not allowed to do - I guess you can't create a webcontrol on the fly - it's got to be compiled, right? Here is my code:

<TBODY><%Dim x as integer%>
<%For x = 0 To Ubound(MenuLinks) Then%>
<tr>
<td>
<asp:linkbutton id=<%=MenuLinks(x)%> Runat="server"><b><font color=lightgrey face=Arial size=2><%=MenuLinks(x)%></b></FONT></asp:linkbutton>
</td>
</tr>
<%Next%>
</TBODY>

Is there any way around this? ..or will I have to use an href and javascript and manually submit the form back to itself like you would in traditional asp pages? Also, do I have to Dim x, or declare it as a public variable?
Thanks for the help!

J

cmiskow
August 22nd, 2005, 02:13 PM
in .NET you can do this with a Repeater control, with databindings to set the various properties. However, you cannot bind the ID. Here is an example:

in the code-behind (page load probably):

Repeater1.DataSource = MenuItems
Repeater1.DataBind()


in the HTML:

<tbody>
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<tr><td>
<asp:LinkButton Runat="server"><%# Container.DataItem %></asp:LinkButton>
</ItemTemplate>
</td></tr>
</asp:Repeater>
</tbody>

jaklotz
August 22nd, 2005, 02:18 PM
Thanks for the tip ..but I have another problem. That snippet of code I gave is in an include file, which I include on the actual page (Main.aspx) like so:

<!-- #Include File="HeaderInclude.inc" -->

..so I have no code behind file for this HeaderInclude ...how can I make a codebehind file for this so that I can include in in Main.aspx.vb (and what is the code I would use to include my HeaderInclude file's codebehind inside my Main.aspx codebehind file? Am I making sense???

Thanks!!!!

cmiskow
August 22nd, 2005, 02:31 PM
I understand your problem, but in ASP.NET using include files is probably not a good idea and it is never necessary. I would recommend building your menu into a user control and simply use the control every place you were using the include file. The user control will have its own code-behind page.

jaklotz
August 22nd, 2005, 02:37 PM
Thanks again - I'm sorry, I'm so used to doing asp, this is driving me nuts. Would you mind giving me an example of this user control file - something simple, and how you would include that on the page you need to include it on?
Thanks!!!

cmiskow
August 22nd, 2005, 04:49 PM
Before I go into detail... are you using Visual Studio .NET?

jaklotz
August 22nd, 2005, 04:54 PM
Yes.

cmiskow
August 22nd, 2005, 06:07 PM
OK, then all you have to do is this:

1) Right click on the web project in the solution explorer.
2) Click on Add->Add new item...
3) Select "Web User Control", and give it a name (menu.ascx or something... note the ASCX instead of ASPX), and click Ok.
4) Design and code the control just like an ASPX web page. It has a design view, an HTML view, and a code-behind page.
5) When done designing the control, go to the page where you want to place one. Click the control file in solution explorer and drag in onto the page. It will appear in design view as a grey rectangle, but when you run the page it will show up however you designed it.

jaklotz
August 22nd, 2005, 11:32 PM
So for the repeater control, you said that I couldn't give it an ID since there could be any number of menu links created at run time. If I can't give them an ID, how can I dynamically create the functions in the codebehind file that execute when each one is clicked?
Thanks!

jaklotz
August 23rd, 2005, 01:09 AM
....is the repeater ItemCommand function the only thing I can use, and if so, can you tell me this: why is it if I click on my menu link, it first posts back to the page, firing the page_load sub, then it runs the itemcommand sub for my repeater. Shouldn't this the repeater's itemcommand function run first since the menu link was clicked and then the postback occurs??

cmiskow
August 23rd, 2005, 09:10 AM
Yes, the ItemCommand is the event to use. Through the event arguments you can figure out which button was clicked.

The Page Load event always fires before command events such as button clicks. If you have something inside the Page Load that you don't want to run on postbacks, put it inside a If Not Page.IsPostback Then ... End If.

jaklotz
August 23rd, 2005, 04:00 PM
It seems like lots of overhead. Here's the thing, depending on the page, the menu links are going to be different and do different things, so what happens when one of those is clicked needs to be programmed on the page the user control is on, not in the ItemCommand sub in the control iteslf. So what I have it doing is a user clicks a menu link, so it does the post back, does the page load event, then it goes to the ItemCommand to figure out which link was clicked (setting a Session variable (to indicate the action to take) to pass back to the page when redirected), and then this redirects the user back to the same page, to which it fires the same page load event again this time, which checks this Session variable to determine what it should do and executes a redirect or whatever it should do when that link button is clicked. This all probably sounds a little rediculous and inefficient at that - is there an alternative to how I've coded this that I'm just not seeing. In traditional asp it was easy, because you'd just submit the form to an action page, and it would process however depending on what action is submitted. Let me know if you'd like me to provide some code to better explain what I'm saying. Thanks again!