Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 18th, 2004, 11:17 PM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default Web User Control Best Practices

Does anyone have any tips? hehe what a question right?

Well, My question is a general one at least i think.

When you professionals right your user controls, do you put all of the functionality and data handling into it's ascx.vb file, or do you try to make them as simple as possible by simply giving it a template to display, and whatever .aspx file you are using it in, have that .aspx.vb file tell it what information to bind to?

Is the assumption that user controls are static and cannot display dynamic information correct? Meaning that you give it some static html to follow and that's it? Or is there a way to simply have a user control have a repeater/datalist/ or datagrid or set of datagrids and programatically set what information for it to display? So for instance is there a way so that i can programatically write an html design and bind some data to it ? or will i have to write a Custom Server Control? thanks

Flyin

Reply With Quote
  #2 (permalink)  
Old May 19th, 2004, 02:11 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi flyin,

User controls are very capable in displaying dynamic data. In fact, User Controls are little ASP.NET pages, so you can anything with a UC that you can also do with a page.

There are a couple of methods to pass data to a UC. For example, you can use form fields, query strings, cookies etc for the dynamic data, like you would in any other page.

However, another, more common scenario is to have a UC expose public properties that you can set in the page that loads the UC. For example, you can have a menu that displays main and sub categories on a site. Whenever a specific article is loaded, the article itself knows to what main and subcategory it belongs. So, the article page that loads the menu UC can change public properties on the UC to preselect some items in the menu.
To be able to set the properties, you can load a control in a page using LoadControl, cast it to a specific type and then set the properties.

Here's a quick example. The example shows a public property on a UC, and a way to load the UC and set the property from a page.
Code:
[Code Behind of UC]
public int MenuItem
{
    get
    {
        return menuItem;
    }
    set
    {
        menuItem = value;
    }
}
The ASP.NET page that uses this UC can now set its properties, like this:
Code:
[Code Behind of page using UC]
MyUserControl MyUserControl1 = (MyUserControl) LoadControl("/Controls/MyUserControl.ascx");
MyUserControl1.MenuItem = 35;
MyPlaceHolder.Controls.Add(MyUserControl1);
The LoadControl method returns a generic Control object, so you need to cast it to your control type to be able to set its property MenuItem. Finally, the control is added to the Controls collection of another control, a PlaceHolder in this example.

Inside the control, you can now use menuItem to change appearance, state, load data etc etc.

So, if you look at this, you can see that UCs can be very dynamic. Since you can programmatically access them, they can do about anything you want. You could have a UC with a "Top 5" products with a repeater that displays the 5 most popular products based on a specific category. Or you could have a UC that displays different information depending on the role or access level of the currently logged on user. Or you can have a UC to display a shopping cart, showing data ranging from nothing, to a complete shopping list with order summary etc.

For all this, you could also write server controls, but for many tasks, the time it takes to write them properly, is way too much when compared with a quick UC. Of course server controls have other benefits like design time support that make them very useful in some scenarios....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #3 (permalink)  
Old May 20th, 2004, 10:45 AM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default

Hello Imar, yes this article is very helpful. I've done something similar to this in that i have a userControl that has a specific layout, and i also have a repeater that uses the uc to display the data. So for instnace,

My user controls looks like this:

MyControl.ascx
<table>
<tr>
<td>Company Name: <%# Databinder.Eval(Container.Item, "CompanyName"</td>
</tr>
</table>

MyControl2.ascx
<div>Company Name: <%# Databinder.Eval(Container.Item, "CompanyName"</div>

In my index.aspx page i simply declare a repeater, but do not specify any attributes:
<asp:repeater id=rpt1 runat=server>

In the Index.aspx.vb
Sub Page_load()
 if chkUseUserControl1.Checked then
  rpt1.LoadTemplate("MyControl.ascx")
 else
  rpt1.LoadTemplate("MyControl2.ascx")
 end if

 SQL = "SELECT CompanyName FROM Companies"
 ...
 rpt1.DataSource = _dataTable.DefaultView
 rpt1.DataBind()
End Sub

There ya have it, dynamically loaded templates!! Not sure if you knew about this, you probably did!

But yes imar, this discussion has helped tremendously! I'm sure i'll be back with more questions!

Reply With Quote
  #4 (permalink)  
Old May 23rd, 2004, 07:49 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by flyin
When you professionals right your user controls, do you put all of the functionality and data handling into it's ascx.vb file, or do you try to make them as simple as possible by simply giving it a template to display, and whatever .aspx file you are using it in, have that .aspx.vb file tell it what information to bind to?
If your control is specific to some functionality, then let it do everything it needs. For example: If you want to construct a user control to show your product list, then put all the necessary functionality into that control. Otherwise, you'll have to duplicate the "get my products" code in every page that wants to show products. To expand on this example: Say you want to build a user control that is a special layout for a grid that you want to use in many places. (Things like this are often called "widgets", but in the .net paradigm "user/server control" or just "control" is usually the term used). This grid could be used for different things so you would need to have the consumer tell it what the data is. In this case, you build a user control that expects a datasource. In this whole scenario, you could have three levels to your page:

  MyPage (ASPX)
   +-MyProducts (ASCX)
      +-MySpecialGrid (ASCX)

MyProducts handles the code to get the product data, and it tells the MySpecialGrid control about that data. So you have a combination of a smart and dumb user control to yield a useful end result.

Peter
-------------------------
Work smarter, not harder
Reply With Quote
  #5 (permalink)  
Old May 24th, 2004, 07:57 AM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default

yes peter i like that setup. I am setting my User control system is that way. I have a base class that derives from System.Web.UI.UserControl that handles the getting of the data, and derive different classes to display this data in different layouts. I guess i was it correctly somewhat in the beginning! Well what do ya know! :). Thanks peter, always good to hear from both you and Imar on a subject

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Web User Control Problem jbenson001 ASP.NET 1.x and 2.0 Application Design 12 January 18th, 2005 03:56 PM
Web user control EvilGuyWhoEatsBrains General .NET 1 December 14th, 2004 01:21 PM
Web User Control Problem shmacgregor VS.NET 2002/2003 18 February 22nd, 2004 06:29 PM



All times are GMT -4. The time now is 01:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.