Results 1 to 5 of 5

Thread: Custom Controls

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Custom Controls

    Does anyone know of any good online tutorials guiding you through custome controls.

    Making custom controls is easy enough I know but I am not sure how to get 2 or more custom controls and the page itself to work together.

    Cheers Chucky Eggs

    Parksie

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Depends if you use C# or VB.

    I like the C# way but I'm sure there's equivalent VB code.
    Create a C# Web project with a WebForm1.aspx and WebUserControl1.ascx

    Drag and drop a Button1 and Button2 to WebUserControl1.ascx

    In WebUserControl1.ascx.cs define the following class members and methods (you can name them whatever you like):
    Code:
    public class WebUserControl1 : System.Web.UI.UserControl
    {
    	protected System.Web.UI.WebControls.Button Button1;
    	protected System.Web.UI.WebControls.Button Button2;
    
    	public delegate void FirstButtonClickHandler();
    	public delegate void SecondButtonClickHandler();
    
    	public event FirstButtonClickHandler FirstButtonClick;
    	public event SecondButtonClickHandler SecondButtonClick;
    
    	public void OnFirstButtonClick()
    	{
    		FirstButtonClick();
    	}
    
    	public void OnSecondButtonClick()
    	{
    		SecondButtonClick();
    	}
    
    	// other class code
    	
    }
    Now double click the Buttons on the user control to bring up the default event handlers and call the public methods you just defined.
    Code:
    private void Button1_Click(object sender, System.EventArgs e)
    {
    	OnFirstButtonClick();
    }
    
    private void Button2_Click(object sender, System.EventArgs e)
    {
    	OnSecondButtonClick();
    }
    Drag the user control file onto WebForm1.

    I don't know why, but the environment doesn't declare the control in your code behind like regular controls so you have to declare it yourself. If your user control is WebUserControl1 then it should be named WebUserControl11 so I rename it uc1.

    On the WebForm1 Page_Load event (or maybe the class constructor) define event handlers for the user control you just declared. After you type the '+=' Visual Studio (2003) will suggest function names and you can press tab to insert the event stub and a second time to go to the event handler function.
    Code:
    public class WebForm1 : System.Web.UI.Page
    {
    	protected WebApplication1.WebUserControl1 uc1;
    
    	private void Page_Load(object sender, System.EventArgs e)
    	{
    		// define event handlers
    		uc1.FirstButtonClick += new WebApplication1.WebUserControl1.FirstButtonClickHandler(uc1_FirstButtonClick);
    		uc1.SecondButtonClick += new WebApplication1.WebUserControl1.SecondButtonClickHandler(uc1_SecondButtonClick);
    	}
    
    	private void uc1_FirstButtonClick()
    	{
    		Response.Write("Button1 Click");
    	}
    
    	private void uc1_SecondButtonClick()
    	{
    		Response.Write("Button2 Click");
    	}
    
    	// other class code
    
    }
    Last edited by wey97; Aug 3rd, 2004 at 08:48 AM.

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    Thanks.
    That was a great explanation/example.

    One more question though.
    What about 2 user controls on 1 page ?
    How can these fire off eachothers events ?

    e.g
    I have a page "MyPage.aspx"
    I have a user control "control1" which consists of a text box and button.
    I have another user control "control2" which contains a datagrid.

    When the user enters text into "control1" and hits the button I want to search a database table for that ID or name and display the results in the datagrid in control2.

    I'm just not sure how these 3 elements can intract with eachother. Which onload event fires first ?

    Parksie

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    You shouldn't have to worry about the onload events...
    This should work:

    1. Write a public method GetText() in your 'control1' to return the text from the text box.

    2. Write an event handler for the 'control1' button just like the code above.

    3. Write a public function search(string str) in the 'control2' passing the method a string for the ID or name, search the database, and populate the datagrid.

    4. On the main page, define the event handler for the 'control1' button click.

    5. On the button click method, call the GetText() method of control1 and pass it to the Search() method of control 2

    control2.Search(control1.GetText());

  5. #5

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    Thanks for your time.
    It doesn't seem to bad when someone explains it to you.

    Cheers


    Parksie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width