Click here to Skip to main content
13,050,904 members (63,980 online)
Rate this:
Please Sign up or sign in to vote.
I have a web page(.aspx), on which I'm loading some user control by jQuery ajax.
Something like this:

               type: "POST",
               url: "WebService.asmx/GetGrid",
               data: data,
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function(result){
               error: function(error) {

In WebService.asmx.cs Web Service I have a GetGrid WebMethod which generates and returns the html of user control, that contains grid view. Here is the code:

    public string GetPage(object[] criteria)
        Page page = new Page {ViewStateMode = ViewStateMode.Disabled};
        AjaxGridView grid = (AjaxGridView)page.LoadControl("~/Controls/AjaxGridView.ascx");
        grid.ViewStateMode = ViewStateMode.Disabled;
        HtmlForm form = new HtmlForm {ViewStateMode = ViewStateMode.Disabled};
        string result = String.Empty;
        using (StringWriter output = new StringWriter())
            page.Server.Execute(page, output, false);
            result = output.ToString();
        return result;

AjaxGridView is a user control, that contains a GridView, and binds it to some data depending on some criteria with BindData(criteria) public method.

All this code works fine. I've used this technique several times and it used to work fine.

This time, after loading the html from service onto the page, clicking the controls that should make autopostback (such as asp:Button, asp:DropDownList, asp:CheckBoxList), throws the following js esception:
__EVENTTARGET is undefined

on this part of code:
function __doPostBack(eventTarget, eventArgument) {
       if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
           theForm.__EVENTTARGET.value = eventTarget;
           theForm.__EVENTARGUMENT.value = eventArgument;

Any ideas?

I've figured out something.
theForm is being set like this:

var theForm = document.forms['ctl00'];
if (!theForm) {
    theForm = document.ctl00;

As I have form in my dynamically generated html, theForm is referring to that form, which doesn't have __EVENTTARGET.

I decided to remove form tag from generated html in service:

if (!String.IsNullOrEmpty(result))
                Regex frmOpenFinder = new Regex("(<form)[^>]*>");
                Match frmOpen = frmOpenFinder.Match(result);
                string frm = frmOpen.Value;
                result = result.Replace(frm, "").Replace("</form>", "");

But in this case, theForm is again

var theForm = document.forms['ctl00'];
if (!theForm) {
    theForm = document.ctl00;

But there isn't any form with such id and now I get:

theForm is undefined

Posted 7-Apr-13 23:36pm
Updated 8-Apr-13 4:27am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 8 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100