Learning about Ajax toolskit to solve the fault: registerdataitem can only be called during an async PostBack.

Source: Internet
Author: User
Document directory
  • The button is released

When you use the ajaxcontroltoolkit ajaxtoolkit: popupcontrolextender control, if a button exists in the pop-up box, the above error is often encountered.

The following is a discussion page on Google about this issue:
Http://forums.asp.net/p/1038571/1464448.aspx

Ncipollina:

I get this error message when I attempt to call the popupextender's commit function from a button's click event. it does not give this error message when called from a calendar's selecteditemchanged event. I have written two user controls, one that has a button which is to apply the results of the popup control to my Textbox, and one that uses a calendar to apply the results to the textbox. the fun Ny thing is, I have an imagebutton on the calendar control, to cancel the popup. It also throws the exception. Can a button not be used to commit the popupextender?

In fact, Microsoft's David Anson gave the correct answer to this question:

Try Setting usesubmitbehavior = false for the button-if that doesn't help, please reply with a complete, simple, self-contained sample page that demonstrates the problem so that we can investigate the specific behavior you're seeing. thank you!

Below is my testing webpage to reproduce the error:

Default. aspx

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<% @ Register

Assembly = "ajaxcontroltoolkit"

Namespace = "ajaxcontroltoolkit"

Tagprefix = "ajaxtoolkit" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.1 // en" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">

<Head runat = "server">

<Title> untitled page </title>

</Head>

<Body>

<Form ID = "form1" runat = "server">

<Ajaxtoolkit: toolkitscriptmanager id = "scriptmanager1" runat = "server"/> & nbsp;

<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>

<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"/>

<Ajaxtoolkit: modalpopupextender id = "modalpopupextender1" runat = "server" popupcontrolid = "Panel1" targetcontrolid = "button1">

</Ajaxtoolkit: modalpopupextender>

<Asp: Panel id = "Panel1" runat = "server" Height = "88px" width = "190px">

& Nbsp;

<Asp: button id = "btnok" runat = "server" enabletheming = "false" onclick = "btnok_click"

TEXT = "OK"/>

<Asp: button id = "btnno" runat = "server" text = "no"/>

<Asp: textbox id = "textbox2" runat = "server"> </ASP: textbox> </ASP: Panel>

<Ajaxtoolkit: popupcontrolextender id = "popupcontrolextender1" runat = "server" offsetx = "50"

Offsety = "50" popupcontrolid = "panel2" position = "right" targetcontrolid = "textbox1">

</Ajaxtoolkit: popupcontrolextender>

& Nbsp;

<Asp: Panel id = "panel2" runat = "server" Height = "50px" width = "125px">

<Asp: updatepanel id = "updatepanel1" runat = "server">

<Contenttemplate>

<Asp: textbox id = "textbox3" runat = "server"> </ASP: textbox>

<Asp: button id = "btninup" runat = "server" onclick = "btninup_click" text = "button"/> & nbsp;

</Contenttemplate>

</ASP: updatepanel>

</ASP: Panel>

</Form>

</Body>

</Html>

Default. aspx. CS

Using system;

Using system. Data;

Using system. configuration;

Using system. Web;

Using system. Web. Security;

Using system. Web. UI;

Using system. Web. UI. webcontrols;

Using system. Web. UI. webcontrols. webparts;

Using system. Web. UI. htmlcontrols;

Public partial class _ default: system. Web. UI. Page

{

Protected void page_load (Object sender, eventargs E)

{

}

Protected void button#click (Object sender, eventargs E)

{

This. modalpopupextender1.show ();

}

Protected void btnok_click (Object sender, eventargs E)

{

Textbox1.text = textbox2.text + "aaaaaaaaa ";

}

Protected void btninup_click (Object sender, eventargs E)

{

// Textbox1.text = textbox3.text; // OK

Popupcontrolextender1.commit (textbox3.text); // an error occurs.

}

}

Follow the David Anson method to correct the following:

Then everything is normal.

Description of this behavior on msdn:

MS-help: // Ms. VSCC. v80/ms. msdn. v80/ms. visualstudio. v80.chs/dv_aspnetcon/html/66b3ce28-3b93-4f0a-951f-42fb5bb5fddf.htm

The button is released

When you click the button control, the page is sent back to the server. By default, this page is sent back to itself, where the same page is regenerated and the event handler of the control on this page is processed.

You can configure buttons to send the current page back to another page. This may be useful for creating multi-Page forms. For more information, see cross-page sending in ASP. NET.

By default,ButtonThe control uses the HTML post operation to submit the page.LinkbuttonAndImagebuttonThe widget cannot directly support HTML post operations. Therefore, when these buttons are used, they add client scripts to the page to allow the controls to submit the page programmatically. (ThereforeLinkbuttonAndImagebuttonThe control requires that client scripts be enabled on the browser .)

In some cases, you may wantButtonThe control also uses client scripts to execute sending back. This may be useful when you want to programmatically perform sending back (such as attaching the sending back to other elements on the page. You can setButtonSet the usesubmitbehavior attribute of the controlTrueTo enableButtonControls use client-based scripts for sending back.

Process client events of the button control

The button control can both trigger server events and client events. Server events occur after sending back, and these events are processed in the server-side code compiled for the page. Client events are processed in client scripts (usually ecmascript (JavaScript) and triggered before the page is submitted. By adding client events to the ASP. NET button control, you can execute tasks (for example, a confirmation dialog box is displayed before the submission page, and the submission may be canceled ). For more information, see the client script in the ASP. NET webpage and how to: Respond to the button Web Server Control event in the client script.

Buttons in the Data Control

Button web server controls are commonly used in data controls (such as datalist, gridview, and repeater list controls. In this case, the response to the button control event is usually different from the use of the button control exclusively on the form. When you click a button in the data control, the event message is sent to the data control, which triggers an event specific to the Data Control. For exampleDatalistIn the control, a button may be triggeredDatalistThe itemcommand event of the control instead of triggeringButtonControlClickEvent.

Because the list web server control can contain many different buttons, you can set the commandargument attribute of the button to specify a value passed as part of the event. Then, you can test the parameter to determine which button is clicked.

Conclusion

When using the Ajax update panel space as the popup panel, make sure that all the internal server-side controls do not use client-side script-based sending back.

The text in the msdn document seems to have been written incorrectly. According to the test results, you should change "true" to "false:

.... You can setButtonSet the usesubmitbehavior attribute of the controlFalseTo enableButtonControls use client-based scripts for sending back.

About http get and HTTP POST

HTTP-GET and HTTP-POST are standard protocol verbs that use HTTP to encode and transmit variable name/variable value pair parameters and use the relevant request semantics. Each http-Get and HTTP-POST is composed of a series of HTTP request headers that define what the client requests from the server, and the response is composed of a series of HTTP Response Headers and response data, if the request is successful, a response is returned.

The HTTP-GET Passes parameters in the format of urlencoded text that uses the MIME type application/X-WWW-form-urlencoded. Urlencoding is a character encoding that ensures that transmitted parameters are composed of compliant texts. For example, the encoding of a space is "% 20 ". The additional parameter can also be considered as a query string.

Like a HTTP-GET, HTTP-POST parameters are URL encoded. However, the variable name/variable value is not transmitted as part of the URL, but is transmitted within the actual HTTP request message.

The difference between get and post is: (for CGI)

If the data is transmitted in get mode, the parameters attached to the CGI program URL are directly transmitted to the server and can be read from the QUERY_STRING environment variable on the server;

If post transmission is used, the parameter is packaged and transmitted to the server in the datagram mode and can be read from the content_length environment variable.

Another case is that you use the get method, but the transmitted parameter is the path, for example:

---- <Ahref = "/cgi-bin/a. pl/usr/local/bin/pine"> CGI </a>

---- The passed parameter "/usr/local/bin/pine" is stored in the environment variable path_info. The environment variable is read by $ STR =$ env {'query _ string '};

Theoretically, GET requests data from the server, and post requests data to the server. In fact, the get method adds the data parameter Queue (query string) to a URL, and the values correspond to the form one by one. For example, name = John. In the queue, values and forms are separated by an & symbol, spaces are replaced by a + sign, and special symbols are converted into hexadecimal code. Because this queue is in the URL, the queue parameters can be viewed, recorded, or changed. The get method also limits the character size. In fact, the POST method can transmit data to the server without time restrictions, and the user cannot see this process on the browser side. Therefore, the POST method is suitable for sending a confidential (such as a credit card number) or a large amount of data is sent to the server.

Post is a method that allows transmission of a large amount of data. The get method attaches the data to be transmitted to the URL and delivers the data to the server together. Therefore, the amount of data transmitted is limited, however, the execution efficiency is better than the POST method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.