How to hiddenly pass parameters to a web user control ?

C

craigkenisston

Hi,

I have a web user control that displays "items" by user, by category,
newest "items", etc. It works very well and does what I want, this is
done with parameters in the page that holds the control.

However, now I want to put this web control on the main page and I
don't want the main page to look "default.aspx?items=newest", for
example, since it would look a bit ugly.

Any idea or suggested approach to do this ?
 
W

Wilco Bauwer

I see 2 solutions:
- URL rewriting. Allows you to rewrite paths like /newest to
default.aspx?items=newest. This makes it totally transparent for the
end-user.
- Use postbacks/hidden state to pass parameters around and display
content based on those parameters.

I would certainly not use postbacks for this for several reasons (one
of them is the ugly back button behaviour, another is bookmarks, etc).
Url rewriting is probably the way to go. You can do a search on google
for one - there are several free downloads. You can also implement your
own, by for example implementing a custom httpmodule which rewrites a
path.

HTH.
 
J

Joshua Flanagan

Instead of having the user control read the parameter from the
querystring, why not have it expose a property that the containing page
can set?

In your control you would have a property:

// use the 'items' variable everywhere you used to use the querystring value
private string items;
public string Items { get { return items; } set { items = value; } }

Now in the main page, you can either set the property programmatically:

myUserControl.Items = "newest"

Or declaratively in the HTML:

<uc1:MyUserControl id="myUserControl" runat="server" Items="newest" />


-Joshua Flanagan
http://flimflan.com/blog
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top