ASP.NET

Moderators: None (Apply to moderate this forum)
Number of threads: 1727
Number of posts: 3292

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problem creating a custom control in vs.net 2003 Posted by ruchika_ru on 20 Jan 2007 at 11:23 AM
Hi,

I am creating a web custom control in asp.net using c#.net

I am not able to understand the following code, I mean why is it used?

The code is as follows:-

protected override CreateChildControls()
{
//Code for Creation and addition of certain server controls.

/*Now associating the click event of the SubmitButtoncontrol with the event handler, SubmitButton_Click (I need to know why are we doing this and what for are we using '+=' ?)*/

SubmitButton.Click += new System.EventHandler (this.SubmitButton_Click)

}


Please let me know if the question is'nt presented clearly...

Looking forward for a response soon...

Regards,
Ruchika



Report
Re: Problem creating a custom control in vs.net 2003 Posted by iwilld0it on 22 Jan 2007 at 7:54 AM
The CreateChildControls is a function defined by .NET Control class that gets called during a controls life-cycle on your behalf.

You override this function when you are creating a custom composite control. Composite meaning that you plan to combine 1 or more existing controls into one control.

So say you create a custom login form that will have two text-boxes (username / password) and a login button.
You would create and add these controls to the custom controls control hierarchy like so:


protected override CreateChildControls()
{
    TextBox userName = new TextBox();
    TextBox password = new TextBox();
    Button login = new Button();

    // Set properties and events ...

    this.Controls.Add(userName);
    this.Controls.Add(password);
    this.Controls.Add(login);
}


: Hi,
:
: I am creating a web custom control in asp.net using c#.net
:
: I am not able to understand the following code, I mean why is it used?
:
: The code is as follows:-
:
: protected override CreateChildControls()
: {
: //Code for Creation and addition of certain server controls.
:
: /*Now associating the click event of the SubmitButtoncontrol with the event handler, SubmitButton_Click (I need to know why are we doing this and what for are we using '+=' ?)*/
:
: SubmitButton.Click += new System.EventHandler (this.SubmitButton_Click)
:
: }
:
:
: Please let me know if the question is'nt presented clearly...
:
: Looking forward for a response soon...
:
: Regards,
: Ruchika
:
:
:
:

Report
Re: Problem creating a custom control in vs.net 2003 Posted by ruchika_ru on 23 Jan 2007 at 12:53 AM
Thanks a lot for the help but the doubts still persist!

I guess that "+=" sign in the following code indicates a delegate. I am unable to know as to how and why is this delegate used for the click event of a button named SubmitButton?

The code is as follows -

/*We are associating the click event of the SubmitButtoncontrol with the event handler, SubmitButton_Click (I need to know why are we doing this and what for are we using '+=' ?)*/

SubmitButton.Click += new System.EventHandler (this.SubmitButton_Click)


Thankingyou in anticipation

Regards,
Ruchika



: The CreateChildControls is a function defined by .NET Control class that gets called during a controls life-cycle on your behalf.
:
: You override this function when you are creating a custom composite control. Composite meaning that you plan to combine 1 or more existing controls into one control.
:
: So say you create a custom login form that will have two text-boxes (username / password) and a login button.
: You would create and add these controls to the custom controls control hierarchy like so:
:
:
:
: protected override CreateChildControls()
: {
:     TextBox userName = new TextBox();
:     TextBox password = new TextBox();
:     Button login = new Button();
: 
:     // Set properties and events ...
: 
:     this.Controls.Add(userName);
:     this.Controls.Add(password);
:     this.Controls.Add(login);
: }
: 




Report
Re: Problem creating a custom control in vs.net 2003 Posted by iwilld0it on 23 Jan 2007 at 8:47 AM
A delegate is essentially a type-safe class wrapper around a function pointer. If you understand how callbacks work then you should understand how delegates work a little. Below is an example delegate:

public delegate void MyDeleg(int someValue);


This defines a delegate called MyDeleg that acts as a place-holder for any function that has the same arguments and return type.

Now say I wrote a function like so:

private void DoSomething(MyDeleg pFunc)
{
    pFunc(10);
}


See how I am using the delegate defined above as a data type in the function argument. Inside DoSomething, I am using the delegate to call whatever function is mapped to the delegate.

Now, lets look at a candidate function to call:

void SomeFunc(int n)
{
    Response.Write(n);
}


The candiate function must have the same argument types and return type.

I can pass the candidate function into DoSomething like so:

DoSomething(new MyDeleg(SomeFunc));


So when DoSomething is called, SomeFunc will be called (hence callback). Now w/ delegates you can chain multiple functions of the same general return and argument types. Say I wanted DoSomething to callback these two functions:

void SomethingA(int n)
{
    Response.Write("SomethingA: " + n.ToString());
}

void SomethingB(int i);
{
    Response.Write("SomethingB: " + i.ToString());
}


I could then set it up like so:

MyDeleg pFunc = new MyDeleg(SomethingA);
pFunc += new MyDeleg(SomethingB);

DoSomething(pFunc);


Now when DoSomething is called, both SomethingA and SomethingB is called in that order.

Now to answer your question the Click property of the Button control is a delegate of the type EventHandler that works with any function that has this format:

void SomeFunctionName(object sender, EventArgs e);


So essentially this code below ...

SubmitButton.Click += new System.EventHandler (this.SubmitButton_Click)


... means the SubmitButton_Click will be called on your behalf. This happens when you click the button. On the backend .NET looks to see if you have any delegates stored in the Click property, if so call those functions are called.

The += is just an operator understood by delegate variables that means to add a function to the delegate calling chain. -= means to remove a function from that calling chain. The perand on the left is the delegate variable and the operand on the write is a delegate object mapped to a proper function to be called. So here is the format explained:

delegateVariable += new SomeDelegateObject(mappedFunction);


Delegates are a complex subject, so I suggest you google the topic.


: Thanks a lot for the help but the doubts still persist!
:
: I guess that "+=" sign in the following code indicates a delegate. I am unable to know as to how and why is this delegate used for the click event of a button named SubmitButton?
:
: The code is as follows -
:
: /*We are associating the click event of the SubmitButtoncontrol with the event handler, SubmitButton_Click (I need to know why are we doing this and what for are we using '+=' ?)*/
:
: SubmitButton.Click += new System.EventHandler (this.SubmitButton_Click)
:
:
: Thankingyou in anticipation
:
: Regards,
: Ruchika
:
:
:
: : The CreateChildControls is a function defined by .NET Control class that gets called during a controls life-cycle on your behalf.
: :
: : You override this function when you are creating a custom composite control. Composite meaning that you plan to combine 1 or more existing controls into one control.
: :
: : So say you create a custom login form that will have two text-boxes (username / password) and a login button.
: : You would create and add these controls to the custom controls control hierarchy like so:
: :
: :
: :
: : protected override CreateChildControls()
: : {
: :     TextBox userName = new TextBox();
: :     TextBox password = new TextBox();
: :     Button login = new Button();
: : 
: :     // Set properties and events ...
: : 
: :     this.Controls.Add(userName);
: :     this.Controls.Add(password);
: :     this.Controls.Add(login);
: : }
: : 

:
:
:
:

Report
Re: Problem creating a custom control in vs.net 2003 Posted by ruchika_ru on 11 Feb 2007 at 1:58 AM
Thanks a lot...


: A delegate is essentially a type-safe class wrapper around a function pointer. If you understand how callbacks work then you should understand how delegates work a little. Below is an example delegate:
:
:
: public delegate void MyDeleg(int someValue);
: 

:
: This defines a delegate called MyDeleg that acts as a place-holder for any function that has the same arguments and return type.
:
: Now say I wrote a function like so:
:
:
: private void DoSomething(MyDeleg pFunc)
: {
:     pFunc(10);
: }
: 

:
: See how I am using the delegate defined above as a data type in the function argument. Inside DoSomething, I am using the delegate to call whatever function is mapped to the delegate.
:
: Now, lets look at a candidate function to call:
:
:
: void SomeFunc(int n)
: {
:     Response.Write(n);
: }
: 

:
: The candiate function must have the same argument types and return type.
:
: I can pass the candidate function into DoSomething like so:
:
:
: DoSomething(new MyDeleg(SomeFunc));
: 

:
: So when DoSomething is called, SomeFunc will be called (hence callback). Now w/ delegates you can chain multiple functions of the same general return and argument types. Say I wanted DoSomething to callback these two functions:
:
:
: void SomethingA(int n)
: {
:     Response.Write("SomethingA: " + n.ToString());
: }
: 
: void SomethingB(int i);
: {
:     Response.Write("SomethingB: " + i.ToString());
: }
: 

:
: I could then set it up like so:
:
:
: MyDeleg pFunc = new MyDeleg(SomethingA);
: pFunc += new MyDeleg(SomethingB);
: 
: DoSomething(pFunc);
: 

:
: Now when DoSomething is called, both SomethingA and SomethingB is called in that order.
:
: Now to answer your question the Click property of the Button control is a delegate of the type EventHandler that works with any function that has this format:
:
:
: void SomeFunctionName(object sender, EventArgs e);
: 

:
: So essentially this code below ...
:
:
: SubmitButton.Click += new System.EventHandler (this.SubmitButton_Click)
: 

:
: ... means the SubmitButton_Click will be called on your behalf. This happens when you click the button. On the backend .NET looks to see if you have any delegates stored in the Click property, if so call those functions are called.
:
: The += is just an operator understood by delegate variables that means to add a function to the delegate calling chain. -= means to remove a function from that calling chain. The perand on the left is the delegate variable and the operand on the write is a delegate object mapped to a proper function to be called. So here is the format explained:
:
:
: delegateVariable += new SomeDelegateObject(mappedFunction);
: 

:
: Delegates are a complex subject, so I suggest you google the topic.
:
:
: : Thanks a lot for the help but the doubts still persist!
: :
: : I guess that "+=" sign in the following code indicates a delegate. I am unable to know as to how and why is this delegate used for the click event of a button named SubmitButton?
: :
: : The code is as follows -
: :
: : /*We are associating the click event of the SubmitButtoncontrol with the event handler, SubmitButton_Click (I need to know why are we doing this and what for are we using '+=' ?)*/
: :
: : SubmitButton.Click += new System.EventHandler (this.SubmitButton_Click)
: :
: :
: : Thankingyou in anticipation
: :
: : Regards,
: : Ruchika
: :
: :
: :
: : : The CreateChildControls is a function defined by .NET Control class that gets called during a controls life-cycle on your behalf.
: : :
: : : You override this function when you are creating a custom composite control. Composite meaning that you plan to combine 1 or more existing controls into one control.
: : :
: : : So say you create a custom login form that will have two text-boxes (username / password) and a login button.
: : : You would create and add these controls to the custom controls control hierarchy like so:
: : :
: : :
: : :
: : : protected override CreateChildControls()
: : : {
: : :     TextBox userName = new TextBox();
: : :     TextBox password = new TextBox();
: : :     Button login = new Button();
: : : 
: : :     // Set properties and events ...
: : : 
: : :     this.Controls.Add(userName);
: : :     this.Controls.Add(password);
: : :     this.Controls.Add(login);
: : : }
: : : 

: :
: :
: :
: :
:
:






 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.