?
Solved

Custom Controls from userControl - create a search bar

Posted on 2004-10-01
6
Medium Priority
?
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 33

Accepted Solution

by:
raterus earned 672 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 664 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 664 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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