Dynamically loading user control based on querystring

F

footballhead

I have a site that has MANY different clothing categories. I have a
page set up for displaying the products (productsearch.aspx) when a
user clicks one of the category links on the navigation menu. I would
like to have a different user control that gets loaded as a sub-header
for each category (ie, tee shirts, shoes, backpacks, etc.) The category
user controls include images and links to refined searches specific to
each category.

So far I have succesfully set up the different ASCX files for each
category. I am stuck when it comes to loading them.

The querystring contains a variable named CategoryID. Each of my user
controls are named with the same values used in the CategoryID
variable. How do I load the correct user control?

I am somewhat limited in that I do _NOT_ have access to code-behind.
All code I use must be inline on the ASPX and the ASCX pages.

If any of you have solutions or suggestions I would be very grateful!

Thanks,

five40
 
J

Jeremy S.

Curious as to why you say you can't use code-behind.

You basically have a PlaceHolder control declared in your aspx where you
want to user control to appear at runtime. Then you have logic that
determines which user control to load, ten add the user control to the
PlaceHolder's Controls collection. Pretty simple, really.

// to get value from querystring (f you were going to get an integer value
from it):
someValue = Convert.ToInt32(Request.Params["thisIsFromQueryString"]);

// Use LoadControl() to instantiate your ascx... like this:
System.Web.UI.Control myUserControlObject =
LoadControl("../SomePath/MyUserControl.ascx");
MyPlaceHolder.Controls.Add(myUserControlObject);

-HTH
 
F

footballhead

Jeremy said:
Curious as to why you say you can't use code-behind.


Jeremy-

Thanks for your fast reply. I am going to play with that code and see
if I can get it to work.

The reason I can't use the code behind is that this site was developed
by a company that will not give me access to code behind. Of course I
can pay them to do it but at this point it's sort of a challege and I'd
like to see if I can do it myself.
 
J

Jeremy S.

I've never heard of someone doing both code-behind and code inline (in the
ascx) - but I suppose it might work. In any case that's a lot of
functionality you are adding without proper integration with whatever the
vendor has already supplied in the code behind. Debugging your new
functionality might be difficult or impossible and regression testing will
be nearly impossible. Sure you want to do this? Furthermore, from the
vendor's perspective - you're trying to extend their system without their
knowledge. You might lose their support altogether.

But back to technical considerations. You might be better off dynamically
loading different data (per product category) into just one user control -
rather than having a bunch of user controls dynamically loaded.

Just some random thoughts....
 
F

footballhead

Actually, the programmers at the vendor are way cool and they have
sorta encouraged me to learn more .NET stuff so I could do some of
these things on my own. Company policy dictaes that I can't access the
code behind but for 'small' things like this they allow some inline
code. Also, they are very busy with big $ clients. I'm a small fish.
They get their monthly hosting fees from me and are happy.

I am going to try to make this work on a staging site. Nothing I can
hurt over there. Currently there is no ASCX for this, I'm adding it so
I won't have to deal with code-behind on that part. One hint they gave
me was to do something page.findcontrol but after reading a few pages
 
J

Jeremy S.

Here's a great article on FindControl(). If you ever need it, you'll benefit
from reading this article.
http://odetocode.com/Articles/116.aspx

Right off I agree with you that I don't see how it could be too useful.
Perhaps it would be safer for your inline code to use FindControl simply as
safety check to help ensure that you aren't attempting to insert a control
with an identical name of a control that already exists there.

But perhaps I"m wandering way off here... still don't know if it's even
possible to do both code -behind and inline. If not the discussion is moot.
Maybe someone else will weigh in here.
 

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