Wednesday, March 28, 2012

Page with AJAX controls posts back twice to the server

I have a form containing three AJAX UpdatePanels, two of which contain a pair of listboxes and a pair of buttons, with the third one containing a pair of radio buttons in a radiobutton list and either three CascadingDropDown lists or two CascadingDropDown lists and a textbox depending on which radio button is selected. The only other control on my form is a button to submit the information on the form to the server.

One thing I've noticed is the information gets posted back to the server twice when the button is clicked. This causes a duplicate record to be written to two tables and a SQL exception being thrown when it tries to write a duplicate record to a third table since it violates that table's primary key constraint. At first, I thought the cause might've been due to the submit button being contained in the third UpdatePanel I mentioned, so I removed it from there and placed it by itself ... but the problem still occurs. One other thing I've noticed is that when data for one of the UpdatePanels is to be updated via a postback to the server, the other two UpdatePanels act as if they're also being posted back to the server; in other words, all three panels noticeably flicker simultaneously. I have a sneaking suspicion this may somehow tie into my entire page being posted back twice when the submit button is clicked, but I'm not at all sure.

What can I do to prevent my page from being posted back twice?

Hi,

any sync postback at the server side is almost identical to regual postback including:

- all control values are posted to the server
- Viewstate is transferred to the server and back

The most important difference is in the rendering phass: only update panels are rendered and their content transferred to the client

So, you must design your application in such a way, that removing all update panels will not break server logic. UpdatePanels reduce flickering, but they don't change how server code process data.

-yuriy


I think there a couple things that can cause this... I think ImageButtons can get rendered as "Submit" buttons, and have a __doPostBack call attached to their OnClick, which seems to make a double postback. Also, I think a button has a UseSubmitBehavior property... try setting that to false.

Page with update panel doesnt show correct information when I back into the page

I have this page that I'd like to use update panels on. There are three controls on the page. I'd like to have the first dropdown control fire a second dropdown and then that one fire a gridview. I'd like the 2nd dropdown and the gridview to be in update panels. So when a user makes a general category selection in the first dropdown, the 2nd one will list the more specific categories (those related to the first dropdown selection) and then when the user picks a more specific category the gridview is filled with with data specific to that 2nd more specific category. I've got this all working fine. The the user clicks the "details" link in the gridview and they see full details about their selection from the gridview. But then when they click the "back" button in their browser, it takes them back to the page with the 2 dropdowns and the gridview but it's not in the state they left it. Ideally, they should see the same thing they saw before they clicked the "details" link.... their first selection int he first dropdown, their more specific category in the 2nd dropdown and the gridview filled with info relating to that 2nd dropdown. But that's not what I get. What am I missing?

hello.

well, you're not missing anything. it's simply the way ajax pages work. i think that you should take a look at the history control available on the future bits

http://msmvps.com/blogs/luisabreu/archive/2007/05/07/may-future-bits-the-history-control.aspx

Page.ClientScript.RegisterStartupScript in updatepanel problem with Beta1.0 ??

Open a new window fails!!! (but with CTP OK!)

protected override void OnClick(EventArgs e)
{
if (grid != null && grid.DataKeyNames.Length > 0)
{
int[] selected = grid.GetSelectedIndexes();
if (selected.Length > 0)
{
grid.SelectedIndex = selected[0];
DataKey dataKey = grid.SelectedDataKey;
if (dataKey != null)
{
string separator1;
if (DialogNavigateUrl.ToString().IndexOf("?") != -1)
{
separator1 = "&";
}
else
{
separator1 = "?";
}
StringBuilder sb = new StringBuilder();
string separator2 = string.Empty;
IDictionaryEnumerator enumerator = dataKey.Values.GetEnumerator();
while (enumerator.MoveNext())
{
sb.Append(separator2);
sb.Append(enumerator.Key);
sb.Append("=");
sb.Append(enumerator.Value);
separator2 = "&";
}
string format;
format = "window.open('{0}{5}{6}{7}Parent={4}',null,'height={1},width={2},status=1,toolbar=0,menubar=0,location={3},resizable=1,scrollbars=1');";
string script =
String.Format(CultureInfo.CurrentCulture, format, DialogNavigateUrl.ToString().Replace("~/", ""), DialogHeight.Value, DialogWidth.Value,
Convert.ToByte(DialogLocation), this.ClientID, separator1, sb.ToString(), separator2);
Type type = this.GetType();
if (!Page.ClientScript.IsStartupScriptRegistered("clientScript"))
{
Page.ClientScript.RegisterStartupScript(type, "clientScript", script, true);
}
}
}
}
base.OnClick(e);
}

For the control to work inside an UpdatePanel you need to call the new static registration APIs on the ScriptManager class. They have basically the same parameters as the Page.ClientScript methods but the new first parameter is the control doing the registration (usually "this").

Thanks,

Eilon


More info about script registration in my recent post:http://forums.asp.net/thread/1440058.aspx

Thanks,

Eilon

Page.ClientScript.RegisterStartupScript in Ajax beta 2

----Code in Module (app_code) folder

Public Sub ShowMessage(ByVal msg As String, ByVal ObjPage As Page)

Try
Dim RegKeyname As String = "infmsg"
If ObjPage.ClientScript.IsStartupScriptRegistered(RegKeyname) Then

RegKeyname = RegKeyname & Now.GetHashCode.ToString("x")
Else
RegKeyname = "infmsg"
End If

ObjPage.ClientScript.RegisterStartupScript(ObjPage.GetType, RegKeyname, String.Format("alert('{0}');", msg), True)


Catch ex As Exception

End Try

End Sub

----------

I used to call this function from any of my asp.net Pages to display an alert message this worked till atlas july ctp now it doenst seem to work in ajax beta 2.

Note : This worked in Normal Pages as well as Pages having an Update panel

Any one any clue ... how to get it back to work

Hi Asifsolkar

I think you should use ScriptManger'sRegisterStartupScript method. There was change between CTP and BETA versions of ASP.NET AJAX ext.

(seehttp://ajax.asp.net/docs/mref/8b90a607-02c9-3c22-6cec-4628c98ccd25.aspx )

Have a nice day

Milo

Page.IsCallback equivalent

What is the Atlas equivalent to the value returned by Page.IsCallback?

As I understand it, since the call is just an intercepted postback, Page.IsPostBack will be True on an Atlas callback. I don't think there is a way to tell if you're dealing with a "True" PostBack or a callback, but I also don't think the event could be called more than one way, so if you need to check later on in your code how you got to where you are, set a member variable in the event handler.


ScriptManager.GetCurrent(Page).IsInPartialRenderingMode

Thanks folks. I had noticedIsInPartialRenderingMode and found it seems to do the job. I do need a definitive answer as it affects how I create my products. Can anyone of the Atlas team chime in?


hello.

as you can see, i'm not in the atlas team. however, i'd like to confirm Rama's answer. btw, currently, a partial postaback is identified by a header called delta which has its value set to true during a partial postback. during the init event, the scriptmanager control looks for that header, and when it has the value true, it initializes the _inPartialRenderingMode field which is used to "feed" the result of the IsInPartialRenderingMode property.

Page.Redirect From Control Event Using Atlas

I am a newbie in atlas..just a couple of hour of experience.

I've managed to create a simple project with sucess, and all works well. But, in a certain event of an treeview control, the "SelectedNodeChanged", i need to redirect the page to another URL. The Page.Redirect runs it the server, but the page doesn't do the postback. How can i force the postback, in a certain event?

Thank you very much.

Well...i found out that the Page.Redirect in Atlas doesn't work very well.

I've got this solution, it's not the best way to do this, but it works,

Page.ClientScript.RegisterStartupScript(this.GetType(),"redirect",@."window.location.href='default.aspx';",true)

Page.RegisterClientScriptBlock breaks Ajax?

Hi -

I am using

Page.RegisterClientScriptBlock to write a script dynamically to my webpage. For some reason, this seems to break some of my other java script (don't get called at all).

I'd be grateful for any idea experience with this!!! Thanks!!!!

Oliver

You want to use the ScriptManager version of that same method instead, the Page's version doesn't work withthe ajax framework.


Paul -

thanks so much. This was quite useful. May I ask you a follow-up question? While Ajax now works on the page, one additional control that loads its Javascript from a file still doesn't ... is there anything special I need to consider?

Thanks again,

Oliver


Well, if that control doesn't load the file through the scriptmanager, that'll be a problem for you as well. What's the control? Also, any .js files need to have a line at the end of them:

if(Sys && Sys.Application)
Sys.Application.notifyScriptLoaded();