Monday, March 26, 2012

Causing postback

I have written some javascript that async calls a webservice every second. If the service returns true, I want to cause a postback, with client script that can be handled on the server and tell a particular update panel to update in the server side handler.

I also wish to construct event arguments on the client to be passed as part of the post back.

How do I do this?

I need to do something similar, and I'm wondering if you could do it by having your page implement the IPostBackEventHandler. This interface requires that you implement a RaisePostBackEvent method. This method takes a string argument, and you could update your updatepanels in this method. Does anyone know if this approach would work?

Thanks,

Nick


hello.

ipostbackeventhandler is not the answer if you need to update updatepanels. what you could do is:

1. use the postbackaction

2. add a hidden dummy button to the updatepanel and force the submit by calling its click method


Nope, dont need to do that. ipostbackeventhandler is the way to go.

I add some javascript to the page via:

Microsoft.Web.UI.ScriptManager.RegisterClientScriptBlock(
this,
this.GetType(),
"UIService_Agent",
string.Format(
"function CheckAgentTimer()" +
"{{" +
"UIService.ShouldInteruptAgent(OnAgentTimerSucceeded,OnAgentTimerFailure);" +
"}}" +
"function OnAgentTimerSucceeded(result)" +
"{{" +
"if(eval(result))" +
"{{" +
"{0};" +
"}}" +
"setTimeout(\"CheckAgentTimer()\",{1});" +
"}}" +
"function OnAgentTimerFailure()" +
"{{" +
"setTimeout(\"CheckAgentTimer()\",60000);" +
"}}" +
"setTimeout(\"CheckAgentTimer()\",10000);",
this.Page.ClientScript.GetPostBackEventReference(this,AgentTasksTimer.AGENT_PROCESS),
this.timerInterval
),
true);

and then I implement the interface ipostbackeventhandler


public void RaisePostBackEvent(string eventArgument)
{
if (eventArgument == AGENT_PROCESS)
{
//Is There Work for the Agent?
if (WorkItemActionRequired != null && IsWorkItemActionRequired())
{
WorkItemActionRequired(this, new WorkItemEventArgs(ResourceReceiverService.GetUserWork(((IUserPrincipal)Context.User).Credentials.UserId, true)));
}
}
}

Messy I know, but bare with me...

You can then request that update panels are updated on the server i.e. myUpdatePanel.Update

This works!


Luis Abreu:

hello.

ipostbackeventhandler is not the answer if you need to update updatepanels. what you could do is:

1. use the postbackaction

2. add a hidden dummy button to the updatepanel and force the submit by calling its click method

How do you call the click method on a hidden dummy button?


hello.

you must get ?a refernce for the html control and call its click method:

$get("your_button_client_side_id").click();

But will this cause a partial or full post back?

Thanks,

Nick


hello.

it depend: if it's an asp.net button and it's placed inside an updatepanel (or if it's outside the updatepanel but it's configured as a trigger) you'll get a partial postback.

That might do the trick then. Right now I have implemented IPostBackHandler, but it causes a full post-back. I just want to do a partial. How do I make the button invisible, set Visible to false, or set visible to hidden in the button style?

Thanks,

Nick


hello.

it depend: if it's an asp.net button and it's placed inside an updatepanel (or if it's outside the updatepanel but it's configured as a trigger) you'll get a partial postback.
hello again.
well, i'd just add syle="display:none" in the asp.net button declaration.

This will also allow me to put code server-side?

Thanks,

Nick

No comments:

Post a Comment