Click here to Skip to main content
15,881,172 members

__EVENTTARGET is undefined

GGev asked:

Open original thread
I have a web page(.aspx), on which I'm loading some user control by jQuery ajax.
Something like this:

JavaScript
$.ajax({
               type: "POST",
               url: "WebService.asmx/GetGrid",
               data: data,
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function(result){
                    $('#divAjaxGridViewContainer').html(result.d);
               },
               error: function(error) {
                   alert("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:


C#
[WebMethod]
    public string GetPage(object[] criteria)
    {
        Page page = new Page {ViewStateMode = ViewStateMode.Disabled};
 
        AjaxGridView grid = (AjaxGridView)page.LoadControl("~/Controls/AjaxGridView.ascx");
        grid.ViewStateMode = ViewStateMode.Disabled;
 
        grid.BindData(criteria);
 
        HtmlForm form = new HtmlForm {ViewStateMode = ViewStateMode.Disabled};
 
        form.Controls.Add(grid);
 
        page.Controls.Add(form);
 
        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:
JavaScript
function __doPostBack(eventTarget, eventArgument) {
       if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
           theForm.__EVENTTARGET.value = eventTarget;
           theForm.__EVENTARGUMENT.value = eventArgument;
           theForm.submit();
       }
   }



Any ideas?

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

var theForm = document.forms['ctl00'];
JavaScript
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:

C#
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'];
JavaScript
if (!theForm) {
    theForm = document.ctl00;
}

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

JavaScript
theForm is undefined

exception.
Tags: Javascript, WebForms, Ajax, jQuery, ASP.NET, Service

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900