The Wagner Blog
Development Notes, News and Trivia









Subscribe to "The Wagner Blog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.
 

 

ASP.NET - Custom properties for UserControls

There is a wealth of functionality available through UserControls, which can help you to create a better web-applicaton architecture than had previously been possible. One aspect that is paramount to this is the ability to influence a property that belongs to a UserControl from whatever page contains this control. In order to do this you need to think in terms of object oriented programming, not web scripting. Therefore, rather than using the classic ASP approach of passing a value on the Querystring, you address the property of the UserControl directly by setting it in code.

Here is an example:

The user control named "uc_bug_workflow" contains a private member variable which can be set via public accessor methods (Get / Set) . The member variable value is called BugID and contains to a primary key value from a db table. (The example comes from my NET software defect tracker).

In the code-behind file of the UserControl the BugID property is defined like this:

public abstract class uc_bug_workflow : System.Web.UI.UserControl

{

    private int _bugID=0;

public int BugID {

      get

{if(!(_bugID == 0))

{return _bugID;}

        else
{
return 0; }

   }

       set {_bugID = value;}

   }

    // other class related code here

}

Assuming you have a completed UserControl, you will need to tell the page that is supposed to contain it some details about this control. This is done in the HTML portion of the aspx page - not in he code behind file (aspx.cs). Registration of the UserControl looks something like this:

@ Register TagPrefix="uc1" TagName="uc_bug_workflow" Src="uc_bug_workflow.ascx"

This one was actually generated by VS.NET as a result of dragging the UserControl from the Project tree onto the page itself. VS has a habit of naming everything xxx1, xxx2 , xxx3 etc. Of course you can change it.  Your page header should look similar to this screenshot:

If you used VS.NET to drag & drop the control, the actual control tag was created for you already. After all, the header above simply tells the page what to expect. Where the control is going to be rendered, on the other hand, is a function of the markup language. The general formula is a tag that refers to the registration in the header as well as the actual file name of the control. Something like

<uc1:uc_bug_workflow id=Uc_bug_workflow1 runat="server"></uc1:uc_bug_workflow>

One especially odd part to watch out for is the fact that VS.NET assigns its own id value to the tag - again using the number 1 as an extension. You can change that as well.

At this point we have the control registered and set up for rendering. But as you can see, we haven't touched the actual property assignment yet. Here is how its done. In the code-behind file for the page that contains the UserControl you will need to add a reference.

protected uc_bug_workflow Uc_bug_workflow1;

Just add that line in the constructor area, right underneath all the other textboxes, grids and whatever else is used by the page. Notice that this line refers to the proper file name of the control "uc_bug_workflow" as well as that odd ID that was given by VS in the markup section of the aspx page. (see right above). Thats it! Your control now is completely wired. In order to set the BugID property to some value that comes from the parent page, here is what it might look like:

private void Page_Load(object sender, System.EventArgs e) {

  if (Request.QueryString["id"]!=null)
  {
   
Uc_bug_workflow1.BugID = Convert.ToInt32Request.QueryString["id"]);
  }

This line simply picks the ID off the Querystring, converts it to an int and sets the property of the UserControl with it.

Voila - it's that easy!


Click here to visit the Radio UserLand website. © Copyright 2004 Thomas Wagner.
Last update: 5/8/2004; 1:05:10 PM.
This theme is based on the SoundWaves (blue) Manila theme.