Solved

Custom Controls from userControl - create a search bar

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 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

24 Experts available now in Live!

Get 1:1 Help Now