469,903 Members | 1,499 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

User Control Postback

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?
Nov 18 '05 #1
5 9542
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" <gd****@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...
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?

Nov 18 '05 #2
>> 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" <re*************@monarchmed.com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
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" <gd****@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...
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?


Nov 18 '05 #3
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" <gd****@hotmail.com> wrote in message
news:uP*************@TK2MSFTNGP11.phx.gbl...
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" <re*************@monarchmed.com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
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" <gd****@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...
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?



Nov 18 '05 #4
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.btnDoSea rch_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" <gd****@hotmail.com> wrote in message
news:uP*************@TK2MSFTNGP11.phx.gbl...
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" <re*************@monarchmed.com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
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" <gd****@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...

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?


Nov 18 '05 #5
Ireney, thank you. That worked perfectly.

"Ireney Berezniak" <ir**************@gmail.com> 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.btnDoSea rch_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" <gd****@hotmail.com> wrote in message
news:uP*************@TK2MSFTNGP11.phx.gbl...
>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 theuser control would raise? The webform would capture that event.

Can you point me to an example that demonstrates that?

Thank you
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...

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" <gd****@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...

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


Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Duwayne | last post: by
3 posts views Thread by Cathie | last post: by
3 posts views Thread by buran | last post: by
3 posts views Thread by Guadala Harry | last post: by
1 post views Thread by Robert Howells | last post: by
6 posts views Thread by Jim | last post: by
8 posts views Thread by Mats Lycken | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.