User Control Postback

G

George Durzi

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

George Durzi

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
 
G

George Durzi

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

Eliyahu Goldin

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
 
I

Ireney Berezniak

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

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



event

sql

Page_Load
 
G

George Durzi

Ireney, thank you. That worked perfectly.

Ireney Berezniak said:
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 said:
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?

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



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


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?
 
Joined
Jun 5, 2008
Messages
1
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top