Solved

Custom Controls from userControl - create a search bar

Posted on 2004-10-01
6
299 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

790 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