本文介绍了__EVENTTARGET未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页(.aspx),我正在通过jQuery ajax加载一些用户控件。

这样的事情:



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

$.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");
               }
           });





在WebService.asmx.cs Web服务中我有一个GetGrid WebMethod,它生成并返回用户控件的html,包含网格视图。这是代码:







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:


[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是一个用户控件,它包含一个GridView,并根据某些条件将其绑定到某些数据BindData(标准)公共方法。



所有这些代码都可以正常工作。我曾经多次使用过这种技术,以前工作得很好。



这次,将服务中的html加载到页面上后,单击应该控制的控件make autopostback(如asp:Button,asp:DropDownList,asp:CheckBoxList),抛出以下js esception:

__EVENTTARGET未定义



在这部分代码中:



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;
           theForm.submit();
       }
   }







有什么想法吗?



编辑

我已经想出了什么。

theForm正在设置如下:



var theForm = document.forms [''ctl00''];




Any ideas?

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

var theForm = document.forms[''ctl00''];

if (!theForm) {
    theForm = document.ctl00;
}



由于我在动态生成的html中有表单,因此该表单指的是没有__EVENTTARGET的表单。



我决定从服务中生成的html中删除表格标签:




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>", "");
            }



但在这种情况下,theForm又是



var theForm = document.forms [''ctl00''];


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



例外。


exception.

推荐答案






在WebService.asmx.cs Web服务中我有一个GetGrid WebMethod,它生成并返回用户控件的html,包含网格视图。这是代码:







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:


[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是一个用户控件,它包含一个GridView,并根据某些条件将其绑定到某些数据BindData(标准)公共方法。



所有这些代码都可以正常工作。我曾经多次使用过这种技术,以前工作得很好。



这次,将服务中的html加载到页面上后,单击应该控制的控件make autopostback(如asp:Button,asp:DropDownList,asp:CheckBoxList),抛出以下js esception:

__EVENTTARGET未定义



在这部分代码中:



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;
           theForm.submit();
       }
   }







有什么想法吗?



编辑

我已经想出了什么。

theForm正在设置如下:



var theForm = document.forms [''ctl00''];




Any ideas?

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

var theForm = document.forms[''ctl00''];

if (!theForm) {
    theForm = document.ctl00;
}



由于我在动态生成的html中有表单,因此该表单指的是没有__EVENTTARGET的表单。



我决定从服务中生成的html中删除表格标签:




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>", "");
            }



但在这种情况下,theForm又是



var theForm = document.forms [''ctl00''];


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



例外。


exception.


这篇关于__EVENTTARGET未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 08:39