User Control Postback

Discussion in 'ASP .Net' started by George Durzi, Jun 24, 2004.

  1. George Durzi

    George Durzi Guest

    I have a simple user control with a text box and a submit button. When the
    user enters some text into the textbox and clicks the submit button, a
    record is created in the database.

    I'm using this user control inside another webform. The webform has a "Next"
    button which is initially disabled. When the user control successfully adds
    a record I want the Next button on the webform to get enabled.

    Checking if the record was added successfully (return the count of a sql
    query) in the Page_Load of the webform is too soon, since the Page_Load
    event of the webform is raised before anything happens on the user control.

    How can my user control "notify" my webform that it has successfully
    completed postback?
    George Durzi, Jun 24, 2004
    #1
    1. Advertising

  2. George Durzi

    George Durzi Guest

    >> How does the page know that anything happened on the control?
    That's what I'm trying to figure out. Can I define a custom event that the
    user control would raise? The webform would capture that event.

    Can you point me to an example that demonstrates that?

    Thank you


    "Eliyahu Goldin" <> wrote in message
    news:...
    > How does the page know that anything happened on the control? Does the

    user
    > control raise an event? If it does, you can enable the button in the event
    > handler.
    >
    > Eliyahu
    >
    > "George Durzi" <> wrote in message
    > news:...
    > > I have a simple user control with a text box and a submit button. When

    the
    > > user enters some text into the textbox and clicks the submit button, a
    > > record is created in the database.
    > >
    > > I'm using this user control inside another webform. The webform has a

    > "Next"
    > > button which is initially disabled. When the user control successfully

    > adds
    > > a record I want the Next button on the webform to get enabled.
    > >
    > > Checking if the record was added successfully (return the count of a sql
    > > query) in the Page_Load of the webform is too soon, since the Page_Load
    > > event of the webform is raised before anything happens on the user

    > control.
    > >
    > > How can my user control "notify" my webform that it has successfully
    > > completed postback?
    > >
    > >

    >
    >
    George Durzi, Jun 24, 2004
    #2
    1. Advertising

  3. George Durzi

    George Durzi Guest

    In my user control I implemented the IPostBackEventHandler interface and did
    the following:

    private static readonly object ClickEvent = new object();

    public event EventHandler Click
    {
    add
    {
    Events.AddHandler(ClickEvent, value);
    }
    remove
    {
    Events.RemoveHandler(ClickEvent, value);
    }
    }

    protected virtual void OnClick(EventArgs e)
    {
    EventHandler clickEventDelegate = (EventHandler)Events[ClickEvent];
    if (clickEventDelegate != null)
    clickEventDelegate(this, e);
    }

    public void RaisePostBackEvent(String eventArgument)
    {
    OnClick(new EventArgs());
    }

    Am I on the right track? How would a webform that has this user control in
    it, consume this event?

    "George Durzi" <> wrote in message
    news:...
    > >> How does the page know that anything happened on the control?

    > That's what I'm trying to figure out. Can I define a custom event that the
    > user control would raise? The webform would capture that event.
    >
    > Can you point me to an example that demonstrates that?
    >
    > Thank you
    >
    >
    > "Eliyahu Goldin" <> wrote in message
    > news:...
    > > How does the page know that anything happened on the control? Does the

    > user
    > > control raise an event? If it does, you can enable the button in the

    event
    > > handler.
    > >
    > > Eliyahu
    > >
    > > "George Durzi" <> wrote in message
    > > news:...
    > > > I have a simple user control with a text box and a submit button. When

    > the
    > > > user enters some text into the textbox and clicks the submit button, a
    > > > record is created in the database.
    > > >
    > > > I'm using this user control inside another webform. The webform has a

    > > "Next"
    > > > button which is initially disabled. When the user control successfully

    > > adds
    > > > a record I want the Next button on the webform to get enabled.
    > > >
    > > > Checking if the record was added successfully (return the count of a

    sql
    > > > query) in the Page_Load of the webform is too soon, since the

    Page_Load
    > > > event of the webform is raised before anything happens on the user

    > > control.
    > > >
    > > > How can my user control "notify" my webform that it has successfully
    > > > completed postback?
    > > >
    > > >

    > >
    > >

    >
    >
    George Durzi, Jun 24, 2004
    #3
  4. How does the page know that anything happened on the control? Does the user
    control raise an event? If it does, you can enable the button in the event
    handler.

    Eliyahu

    "George Durzi" <> wrote in message
    news:...
    > I have a simple user control with a text box and a submit button. When the
    > user enters some text into the textbox and clicks the submit button, a
    > record is created in the database.
    >
    > I'm using this user control inside another webform. The webform has a

    "Next"
    > button which is initially disabled. When the user control successfully

    adds
    > a record I want the Next button on the webform to get enabled.
    >
    > Checking if the record was added successfully (return the count of a sql
    > query) in the Page_Load of the webform is too soon, since the Page_Load
    > event of the webform is raised before anything happens on the user

    control.
    >
    > How can my user control "notify" my webform that it has successfully
    > completed postback?
    >
    >
    Eliyahu Goldin, Jun 24, 2004
    #4
  5. You are putting too much work into this. Use RaiseBubbleEvent to
    communicate to the parent that a click has been executed. While
    creating your own events and handlers certainly make sense in some
    cases, for simple child to parent communication, intrinsic event
    management should suffice.

    So:

    Bind the button click to some delegate in your control, for instance:

    this.btnDoSearch.Click += new
    System.Web.UI.ImageClickEventHandler(this.btnDoSearch_Click);

    In the delegate method, raise the bubble event:

    private void btnDoSearch_Click(object sender,
    System.Web.UI.ImageClickEventArgs e)
    {
    RaiseBubbleEvent(this, e);
    }

    Then, in your parent, capture the bubbled event.

    protected override bool OnBubbleEvent(object sender, EventArgs e)
    {
    nextButton.Visible = true

    return true;
    }

    This is of course a short, raw version of the idea. You'll probably
    modify it to your own needs and make it more robust. For instance, you
    should check the sender object to ensure that the bubble event is coming
    from the correct control. Also, you might want to create your own
    custom event arguments, CommandEventArgs for instance, that you could
    use to send more information about your control's click event to the parent.

    Makes sense?

    ib.


    George Durzi wrote:

    > In my user control I implemented the IPostBackEventHandler interface and did
    > the following:
    >
    > private static readonly object ClickEvent = new object();
    >
    > public event EventHandler Click
    > {
    > add
    > {
    > Events.AddHandler(ClickEvent, value);
    > }
    > remove
    > {
    > Events.RemoveHandler(ClickEvent, value);
    > }
    > }
    >
    > protected virtual void OnClick(EventArgs e)
    > {
    > EventHandler clickEventDelegate = (EventHandler)Events[ClickEvent];
    > if (clickEventDelegate != null)
    > clickEventDelegate(this, e);
    > }
    >
    > public void RaisePostBackEvent(String eventArgument)
    > {
    > OnClick(new EventArgs());
    > }
    >
    > Am I on the right track? How would a webform that has this user control in
    > it, consume this event?
    >
    > "George Durzi" <> wrote in message
    > news:...
    >
    >>>>How does the page know that anything happened on the control?

    >>
    >>That's what I'm trying to figure out. Can I define a custom event that the
    >>user control would raise? The webform would capture that event.
    >>
    >>Can you point me to an example that demonstrates that?
    >>
    >>Thank you
    >>
    >>
    >>"Eliyahu Goldin" <> wrote in message
    >>news:...
    >>
    >>>How does the page know that anything happened on the control? Does the

    >>
    >>user
    >>
    >>>control raise an event? If it does, you can enable the button in the

    >
    > event
    >
    >>>handler.
    >>>
    >>>Eliyahu
    >>>
    >>>"George Durzi" <> wrote in message
    >>>news:...
    >>>
    >>>>I have a simple user control with a text box and a submit button. When

    >>
    >>the
    >>
    >>>>user enters some text into the textbox and clicks the submit button, a
    >>>>record is created in the database.
    >>>>
    >>>>I'm using this user control inside another webform. The webform has a
    >>>
    >>>"Next"
    >>>
    >>>>button which is initially disabled. When the user control successfully
    >>>
    >>>adds
    >>>
    >>>>a record I want the Next button on the webform to get enabled.
    >>>>
    >>>>Checking if the record was added successfully (return the count of a

    >
    > sql
    >
    >>>>query) in the Page_Load of the webform is too soon, since the

    >
    > Page_Load
    >
    >>>>event of the webform is raised before anything happens on the user
    >>>
    >>>control.
    >>>
    >>>>How can my user control "notify" my webform that it has successfully
    >>>>completed postback?
    >>>>
    >>>>
    >>>
    >>>

    >>

    >
    >
    Ireney Berezniak, Jun 24, 2004
    #5
  6. George Durzi

    George Durzi Guest

    Ireney, thank you. That worked perfectly.

    "Ireney Berezniak" <> wrote in message
    news:DyICc.8$o4.2@clgrps13...
    > You are putting too much work into this. Use RaiseBubbleEvent to
    > communicate to the parent that a click has been executed. While
    > creating your own events and handlers certainly make sense in some
    > cases, for simple child to parent communication, intrinsic event
    > management should suffice.
    >
    > So:
    >
    > Bind the button click to some delegate in your control, for instance:
    >
    > this.btnDoSearch.Click += new
    > System.Web.UI.ImageClickEventHandler(this.btnDoSearch_Click);
    >
    > In the delegate method, raise the bubble event:
    >
    > private void btnDoSearch_Click(object sender,
    > System.Web.UI.ImageClickEventArgs e)
    > {
    > RaiseBubbleEvent(this, e);
    > }
    >
    > Then, in your parent, capture the bubbled event.
    >
    > protected override bool OnBubbleEvent(object sender, EventArgs e)
    > {
    > nextButton.Visible = true
    >
    > return true;
    > }
    >
    > This is of course a short, raw version of the idea. You'll probably
    > modify it to your own needs and make it more robust. For instance, you
    > should check the sender object to ensure that the bubble event is coming
    > from the correct control. Also, you might want to create your own
    > custom event arguments, CommandEventArgs for instance, that you could
    > use to send more information about your control's click event to the

    parent.
    >
    > Makes sense?
    >
    > ib.
    >
    >
    > George Durzi wrote:
    >
    > > In my user control I implemented the IPostBackEventHandler interface and

    did
    > > the following:
    > >
    > > private static readonly object ClickEvent = new object();
    > >
    > > public event EventHandler Click
    > > {
    > > add
    > > {
    > > Events.AddHandler(ClickEvent, value);
    > > }
    > > remove
    > > {
    > > Events.RemoveHandler(ClickEvent, value);
    > > }
    > > }
    > >
    > > protected virtual void OnClick(EventArgs e)
    > > {
    > > EventHandler clickEventDelegate = (EventHandler)Events[ClickEvent];
    > > if (clickEventDelegate != null)
    > > clickEventDelegate(this, e);
    > > }
    > >
    > > public void RaisePostBackEvent(String eventArgument)
    > > {
    > > OnClick(new EventArgs());
    > > }
    > >
    > > Am I on the right track? How would a webform that has this user control

    in
    > > it, consume this event?
    > >
    > > "George Durzi" <> wrote in message
    > > news:...
    > >
    > >>>>How does the page know that anything happened on the control?
    > >>
    > >>That's what I'm trying to figure out. Can I define a custom event that

    the
    > >>user control would raise? The webform would capture that event.
    > >>
    > >>Can you point me to an example that demonstrates that?
    > >>
    > >>Thank you
    > >>
    > >>
    > >>"Eliyahu Goldin" <> wrote in message
    > >>news:...
    > >>
    > >>>How does the page know that anything happened on the control? Does the
    > >>
    > >>user
    > >>
    > >>>control raise an event? If it does, you can enable the button in the

    > >
    > > event
    > >
    > >>>handler.
    > >>>
    > >>>Eliyahu
    > >>>
    > >>>"George Durzi" <> wrote in message
    > >>>news:...
    > >>>
    > >>>>I have a simple user control with a text box and a submit button. When
    > >>
    > >>the
    > >>
    > >>>>user enters some text into the textbox and clicks the submit button, a
    > >>>>record is created in the database.
    > >>>>
    > >>>>I'm using this user control inside another webform. The webform has a
    > >>>
    > >>>"Next"
    > >>>
    > >>>>button which is initially disabled. When the user control successfully
    > >>>
    > >>>adds
    > >>>
    > >>>>a record I want the Next button on the webform to get enabled.
    > >>>>
    > >>>>Checking if the record was added successfully (return the count of a

    > >
    > > sql
    > >
    > >>>>query) in the Page_Load of the webform is too soon, since the

    > >
    > > Page_Load
    > >
    > >>>>event of the webform is raised before anything happens on the user
    > >>>
    > >>>control.
    > >>>
    > >>>>How can my user control "notify" my webform that it has successfully
    > >>>>completed postback?
    > >>>>
    > >>>>
    > >>>
    > >>>
    > >>

    > >
    > >
    George Durzi, Jun 24, 2004
    #6
  7. George Durzi

    PNeems

    Joined:
    Jun 5, 2008
    Messages:
    1
    What if my button is in a FormView i dont have access on the on click events. The on click just updates the form details but it does not cause a post back on the parent page. Does anyone know how I would do this? Any help would be greatly appreciated

    Thanks
    PNeems, Jun 5, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Rowton
    Replies:
    2
    Views:
    642
    Tom Rowton
    Aug 1, 2003
  2. vatech1993
    Replies:
    4
    Views:
    5,670
    =?Utf-8?B?U8Opcmdpbw==?=
    Dec 11, 2004
  3. Mad Scientist Jr
    Replies:
    0
    Views:
    622
    Mad Scientist Jr
    Mar 22, 2006
  4. gnewsgroup
    Replies:
    1
    Views:
    378
    gnewsgroup
    Feb 13, 2008
  5. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    525
    Jonathan Wood
    Feb 2, 2010
Loading...

Share This Page