Solved

Custom Controls from userControl - create a search bar

Posted on 2004-10-01
6
296 Views
Last Modified: 2008-02-01
My question is --> what is the best way to convert userControl to a Custom control.

I already have been able to create a very simple header and footer Custom controls.

But now I want to convert a search-bar userControl to Custom control -->
it has 3labels, textbox, go button, and dropdownlist where search result are displayed.
Idea is that you pick the customer from the list and userControl gives you the CustomerID.
So it uses a database to do the search - and datareader to put the results in dropDownList.

it also has events --> customerID_changed event for example (when you select different customer on the list)
0
Comment
Question by:MargusLehiste
6 Comments
 
LVL 33

Accepted Solution

by:
raterus earned 168 total points
ID: 12205380
The key event when dealing with custom controls over usercontrols is "CreateChildControls".  Since you don't have an aspx page to structure your layout, you have to do this manually.

Override this method, and add all the other webcontrols you want to the "controls" property of your custom control.  You can also look up "Composite controls", same concept here.

Hope this helps!
--Michael
0
 
LVL 10

Assisted Solution

by:jnhorst
jnhorst earned 166 total points
ID: 12205535
This is where things do get complicated.  You need to wire up your own events.  A good overview of all that is involved in this can be found here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconeventsinwebformscontrols.asp

In a user control you just combine a bunch of other server controls.  In this case, you probably want to create a class that inherits from ListControl.  The reason for this is that it has all the databinding functionality you will need to bind to a data reader that will execute your query.  The key will be to override the Render() method to write HTML to the browser that has all the various elements you need.  Raterus refers to CreateChildControls().  When you override this method, you want to create instances of the labels, the textbox and the button.  Then in the constructor (the New() method if you are using VB) you want to call EnsureChildControls().  This will cause the base class to fire your CreateChildControls() so that the instances of the labels, textbox and button are created.  Here are some fine points on this:

Declare variables for these controls at the class level.

private _label1 as Label
private _label2 as Label
private _label3 as Label
private _criteria as TextBox
private _go as Button
private _list as DropDownList

Then:

protected override sub CreateChildControls()
     me._label1 = new Label()
     ' ... and so on.

     ' add controls to base.
     MyBase.Controls.Add(me._label1)
     ' ... and so on.
end sub

Then override Render()

protected override sub Render(writer as HtmlTextWriter)
     ' ... see below
end sub

Overriding the Render method is really the critical part, because this is where the control actually writes HTML to the browser.  But it is also called when you drop the control on your form in design time, so you have to render differently for design time than for run time.  The easiest way I have found to do this is this:

' context will be null at design time in the VS ide, but not null at run time when actually rendered to a client.
If Me.Context Is Null Then
     ' write some html tag for the web form designer:
     writer.Write("<span>This is my control</span>")
Else
     ' write out the actual controls.
     Me._label1.RenderControl(writer)
     Me._label2.RenderControl(writer)
     Me._label3.RenderControl(writer)
     Me._criteria.RenderControl(writer)
     
     ' iterate through the Items collection of the base class, adding each to your _list instance.
     Dim li As ListItem
     For Each li in MyBase.Items
          Me._list.Items.Add(li)
     Next

     ' render the dropdown list.
     Me._list.RenderControl(writer)
End If

This is by no means the everything you need to do, but it gives you the idea of what is involved in rendering a composite control like this.  Good luck with your project and I think you will find pursuing this very rewarding.  It will expose you to a whole new level of flexibility that ASP.NET provides.

John
0
 

Assisted Solution

by:altamed
altamed earned 166 total points
ID: 12208654
Hi  MargusLehiste.
The quicker and not too dirty option is to get your user control loaded dynamically by the custom control using LoadControl.
The benefit of this solution would be greater flexibility and extensibility of your custom control.

In order to make user control manageable from within custom control you need to utilize interfaces.
User controls intended for deployment by your custom control must implement custom interface. For ex.:

public interface ISearchBarUserControl {
   event EventHandler OnCustomerIdChanged;
}

Then in your custom control there should be something like this.

protected void doOnCustomerIdChanged(object sender, EventArgs e) {
// TODO
}

private string m_ucURL;
public string UserControlURL { get { return m_ucURL; } set { m_ucURL = value; } }

protected override void CreateChildControls() {
   doLoadUC();
}

protected doLoadUC() {
   try {
      UserControl uc = this.LoadControl(m_ucURL);
      ISearchBarUserControl sb_uc = uc as ISearchBarUserControl;
      if (sb_uc != null) {
         sb_uc.OnCustomerIdChanged += new EventHandler(this.doOnCustomerIdChanged);
      }
   } catch {
      // handle the error
   }
}

And in user control, implement ISearchBarUserControl interface and put something like:

protected void customerIdChanged() {
   if (OnCustomerIdChanged != null)
      OnCustomerIdChanged(this, new EventArgs());
}

This worked for me on couple of occasions.
HTH
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now