Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Custom Controls from userControl - create a search bar

Posted on 2004-10-01
6
Medium Priority
?
304 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

609 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