Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Problem creating a custom control in vs.net 2003

ruchika_ruruchika_ru Posts: 52Member
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



Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    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:


    [code]
    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);
    }
    [/code]

    : 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
    :
    :
    :
    :

  • ruchika_ruruchika_ru Posts: 52Member
    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:
    :
    :
    : [code]
    : 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);
    : }
    : [/code]



  • iwilld0itiwilld0it Posts: 1,134Member
    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:

    [code]
    public delegate void MyDeleg(int someValue);
    [/code]

    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:

    [code]
    private void DoSomething([b]MyDeleg[/b] pFunc)
    {
    pFunc(10);
    }
    [/code]

    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:

    [code]
    void SomeFunc(int n)
    {
    Response.Write(n);
    }
    [/code]

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

    I can pass the candidate function into DoSomething like so:

    [code]
    DoSomething(new MyDeleg(SomeFunc));
    [/code]

    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:

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

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

    I could then set it up like so:

    [code]
    MyDeleg pFunc = new MyDeleg(SomethingA);
    pFunc += new MyDeleg(SomethingB);

    DoSomething(pFunc);
    [/code]

    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:

    [code]
    void SomeFunctionName(object sender, EventArgs e);
    [/code]

    So essentially this code below ...

    [code]
    SubmitButton.Click += new System.EventHandler (this.SubmitButton_Click)
    [/code]

    ... 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:

    [code]
    delegateVariable += new SomeDelegateObject(mappedFunction);
    [/code]

    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:
    : :
    : :
    : : [code]
    : : 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);
    : : }
    : : [/code]
    :
    :
    :
    :

  • ruchika_ruruchika_ru Posts: 52Member
    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:
    :
    : [code]
    : public delegate void MyDeleg(int someValue);
    : [/code]
    :
    : 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:
    :
    : [code]
    : private void DoSomething([b]MyDeleg[/b] pFunc)
    : {
    : pFunc(10);
    : }
    : [/code]
    :
    : 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:
    :
    : [code]
    : void SomeFunc(int n)
    : {
    : Response.Write(n);
    : }
    : [/code]
    :
    : The candiate function must have the same argument types and return type.
    :
    : I can pass the candidate function into DoSomething like so:
    :
    : [code]
    : DoSomething(new MyDeleg(SomeFunc));
    : [/code]
    :
    : 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:
    :
    : [code]
    : void SomethingA(int n)
    : {
    : Response.Write("SomethingA: " + n.ToString());
    : }
    :
    : void SomethingB(int i);
    : {
    : Response.Write("SomethingB: " + i.ToString());
    : }
    : [/code]
    :
    : I could then set it up like so:
    :
    : [code]
    : MyDeleg pFunc = new MyDeleg(SomethingA);
    : pFunc += new MyDeleg(SomethingB);
    :
    : DoSomething(pFunc);
    : [/code]
    :
    : 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:
    :
    : [code]
    : void SomeFunctionName(object sender, EventArgs e);
    : [/code]
    :
    : So essentially this code below ...
    :
    : [code]
    : SubmitButton.Click += new System.EventHandler (this.SubmitButton_Click)
    : [/code]
    :
    : ... 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:
    :
    : [code]
    : delegateVariable += new SomeDelegateObject(mappedFunction);
    : [/code]
    :
    : 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:
    : : :
    : : :
    : : : [code]
    : : : 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);
    : : : }
    : : : [/code]
    : :
    : :
    : :
    : :
    :
    :



Sign In or Register to comment.