简体   繁体   English

IE9 Javascript未定义错误

[英]IE9 Javascript undefined error

I'm getting a javascript error in IE9 that I'm not getting in Chrome or FF, and I can't figure out a good way to solve it. 我在IE9中遇到了一个JavaScript错误,我没有在Chrome或FF中获得错误,并且我找不到解决它的好方法。 The code does work in Compatibility Mode. 该代码确实在兼容模式下工作。

In the example code, I've got a custom user control that consists of a few basic controls and an AJAX modelpopupextender. 在示例代码中,我有一个自定义用户控件,该控件包含一些基本控件和一个AJAX模型popupextender。 A listview populates some products, and I attach an onclick event onitemdatabound that populates a label and closes the modelpopup to each htmlrow of the listview items. 列表视图填充了一些产品,我附加了一个onclick事件onitemdatabound,该事件填充标签并关闭modelpopup到列表视图项的每个htmlrow。

IE9 can't find Button2, and fails to complete the javascript function. IE9找不到Button2,并且无法完成javascript函数。 Current versions of Chrome and FF perform as intended. 当前版本的Chrome和FF可以正常运行。 Is there something I'm missing? 有什么我想念的吗?

Source: 资源:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="SelectProduct.ascx.vb" Inherits="SelectProduct" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<style type="text/css">
.modalBackground 
{
    background-color: Gray;
    filter: alpha(opacity=70);
    opacity: 0.7;
}
</style>
<asp:Panel ID="Panel1" runat="server" BackColor="Wheat">    
    <table style="padding:5px;">
        <tr>
            <td style="width:40px;">&nbsp;</td>
            <td style="width:400px;text-align:center;">
                <b style="font-size:12pt;color:#155184;">Select a Product</b>
            </td>
            <td style="width:40px;text-align:right;">
                <asp:Button ID="Button2" runat="server" Text="X" CausesValidation="false" />
            </td>
        </tr>
    </table>
    <asp:Panel ID="Panel2" runat="server" Height="500" ScrollBars="Auto" BackColor="White">
        <table style="padding:5px;">
            <tr>
                <td colspan="3">
                    <asp:ListView ID="ListView1" runat="server" DataSourceID="sqlProducts" DataKeyNames="productid, productname">
                        <LayoutTemplate>
                            <table cellpadding="4" cellspacing="0" style="width:480px;border:solid 1px #cccccc;" rules="all">
                                <tr>
                                    <th style="width:100px;">ID</th>
                                    <th style="width:280px;">Name</th>
                                    <th style="width:100px;">Price</th>
                                </tr>
                                <tr id="itemPlaceHolder" runat="server"></tr>
                            </table>
                        </LayoutTemplate>
                        <ItemTemplate>
                                <tr id="selectedProduct" runat="server" style="cursor:pointer;">
                                    <td style="width:100px;text-align:center;">
                                        <asp:Label ID="lblID" runat="server" Text='<%# Eval("productid") %>' />
                                    </td>
                                    <td style="width:280px;text-align:center;">
                                        <asp:Label ID="lblName" runat="server" Text='<%# Eval("productname") %>' />
                                    </td>
                                    <td style="width:100px;padding-left:4px;">
                                        <asp:Label ID="lblPrice" runat="server" Text='<%# Eval("unitprice") %>' />
                                    </td>
                                </tr>
                        </ItemTemplate>
                    </asp:ListView>
                    <asp:SqlDataSource ID="sqlProducts" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:Northwind %>" 
                        SelectCommand="select productid, productname, unitprice from products">
                    </asp:SqlDataSource>
                </td>
            </tr>
        </table>  
    </asp:Panel>
</asp:Panel>
<asp:Button ID="Button1" runat="server" Text="Select a Product..." CausesValidation="false" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" BackgroundCssClass="modalBackground" 
    PopupControlID="Panel1" TargetControlID="Button1" CancelControlID="Button2">
</asp:ModalPopupExtender>

Code: 码:

Partial Class SelectProduct
    Inherits System.Web.UI.UserControl

    Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound

        Dim row As HtmlTableRow = CType(e.Item.FindControl("selectedProduct"), HtmlTableRow)
        Dim product As String = ListView1.DataKeys(CType(e.Item, ListViewDataItem).DataItemIndex).Values(1).ToString()
        Dim id As String = ListView1.DataKeys(CType(e.Item, ListViewDataItem).DataItemIndex).Values(0).ToString()
        row.Attributes.Add("onclick", Label1.ClientID + ".innerText=""" + product + """;" + Button2.ClientID + ".click();")

    End Sub
End Class

Generated HTML: 生成的HTML:

<div>
    <style type="text/css">
        .modalBackground
        {
            background-color: Gray;
            filter: alpha(opacity=70);
            opacity: 0.7;
        }
    </style>
    <div id="SelectProduct1_Panel1" style="background-color: Wheat;">
        <table style="padding: 5px;">
            <tr>
                <td style="width: 40px;">
                    &nbsp;
                </td>
                <td style="width: 400px; text-align: center;">
                    <b style="font-size: 12pt; color: #155184;">Select a Product</b>
                </td>
                <td style="width: 40px; text-align: right;">
                    <input type="submit" name="SelectProduct1$Button2" value="X" id="SelectProduct1_Button2" />
                </td>
            </tr>
        </table>
        <div id="SelectProduct1_Panel2" style="background-color: White; height: 500px; overflow: auto;">
            <table style="padding: 5px;">
                <tr>
                    <td colspan="3">
                        <table cellpadding="4" cellspacing="0" style="width: 480px; border: solid 1px #cccccc;"
                            rules="all">
                            <tr>
                                <th style="width: 100px;">
                                    ID
                                </th>
                                <th style="width: 280px;">
                                    Name
                                </th>
                                <th style="width: 100px;">
                                    Price
                                </th>
                            </tr>
                            <tr id="SelectProduct1_ListView1_ctrl0_selectedProduct" style="cursor: pointer;"
                                onclick="SelectProduct1_Label1.innerText=&quot;Chai&quot;;SelectProduct1_Button2.click();">
                                <td style="width: 100px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl0_lblID">1</span>
                                </td>
                                <td style="width: 280px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl0_lblName">Chai</span>
                                </td>
                                <td style="width: 100px; padding-left: 4px;">
                                    <span id="SelectProduct1_ListView1_ctrl0_lblPrice">18.0000</span>
                                </td>
                            </tr>
                            <tr id="SelectProduct1_ListView1_ctrl1_selectedProduct" style="cursor: pointer;"
                                onclick="SelectProduct1_Label1.innerText=&quot;Chang&quot;;SelectProduct1_Button2.click();">
                                <td style="width: 100px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl1_lblID">2</span>
                                </td>
                                <td style="width: 280px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl1_lblName">Chang</span>
                                </td>
                                <td style="width: 100px; padding-left: 4px;">
                                    <span id="SelectProduct1_ListView1_ctrl1_lblPrice">19.0000</span>
                                </td>
                            </tr>                                
                            <tr id="SelectProduct1_ListView1_ctrl69_selectedProduct" style="cursor: pointer;"
                                onclick="SelectProduct1_Label1.innerText=&quot;Outback Lager&quot;;SelectProduct1_Button2.click();">
                                <td style="width: 100px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl69_lblID">70</span>
                                </td>
                                <td style="width: 280px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl69_lblName">Outback Lager</span>
                                </td>
                                <td style="width: 100px; padding-left: 4px;">
                                    <span id="SelectProduct1_ListView1_ctrl69_lblPrice">15.0000</span>
                                </td>
                            </tr>
                            <tr id="SelectProduct1_ListView1_ctrl70_selectedProduct" style="cursor: pointer;"
                                onclick="SelectProduct1_Label1.innerText=&quot;Flotemysost&quot;;SelectProduct1_Button2.click();">
                                <td style="width: 100px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl70_lblID">71</span>
                                </td>
                                <td style="width: 280px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl70_lblName">Flotemysost</span>
                                </td>
                                <td style="width: 100px; padding-left: 4px;">
                                    <span id="SelectProduct1_ListView1_ctrl70_lblPrice">21.5000</span>
                                </td>
                            </tr>
                            <tr id="SelectProduct1_ListView1_ctrl71_selectedProduct" style="cursor: pointer;"
                                onclick="SelectProduct1_Label1.innerText=&quot;Mozzarella di Giovanni&quot;;SelectProduct1_Button2.click();">
                                <td style="width: 100px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl71_lblID">72</span>
                                </td>
                                <td style="width: 280px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl71_lblName">Mozzarella di Giovanni</span>
                                </td>
                                <td style="width: 100px; padding-left: 4px;">
                                    <span id="SelectProduct1_ListView1_ctrl71_lblPrice">34.8000</span>
                                </td>
                            </tr>
                            <tr id="SelectProduct1_ListView1_ctrl72_selectedProduct" style="cursor: pointer;"
                                onclick="SelectProduct1_Label1.innerText=&quot;Röd Kaviar&quot;;SelectProduct1_Button2.click();">
                                <td style="width: 100px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl72_lblID">73</span>
                                </td>
                                <td style="width: 280px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl72_lblName">Röd Kaviar</span>
                                </td>
                                <td style="width: 100px; padding-left: 4px;">
                                    <span id="SelectProduct1_ListView1_ctrl72_lblPrice">15.0000</span>
                                </td>
                            </tr>
                            <tr id="SelectProduct1_ListView1_ctrl73_selectedProduct" style="cursor: pointer;"
                                onclick="SelectProduct1_Label1.innerText=&quot;Longlife Tofu&quot;;SelectProduct1_Button2.click();">
                                <td style="width: 100px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl73_lblID">74</span>
                                </td>
                                <td style="width: 280px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl73_lblName">Longlife Tofu</span>
                                </td>
                                <td style="width: 100px; padding-left: 4px;">
                                    <span id="SelectProduct1_ListView1_ctrl73_lblPrice">10.0000</span>
                                </td>
                            </tr>
                            <tr id="SelectProduct1_ListView1_ctrl74_selectedProduct" style="cursor: pointer;"
                                onclick="SelectProduct1_Label1.innerText=&quot;Rhönbräu Klosterbier&quot;;SelectProduct1_Button2.click();">
                                <td style="width: 100px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl74_lblID">75</span>
                                </td>
                                <td style="width: 280px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl74_lblName">Rhönbräu Klosterbier</span>
                                </td>
                                <td style="width: 100px; padding-left: 4px;">
                                    <span id="SelectProduct1_ListView1_ctrl74_lblPrice">7.7500</span>
                                </td>
                            </tr>
                            <tr id="SelectProduct1_ListView1_ctrl75_selectedProduct" style="cursor: pointer;"
                                onclick="SelectProduct1_Label1.innerText=&quot;Lakkalikööri&quot;;SelectProduct1_Button2.click();">
                                <td style="width: 100px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl75_lblID">76</span>
                                </td>
                                <td style="width: 280px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl75_lblName">Lakkalikööri</span>
                                </td>
                                <td style="width: 100px; padding-left: 4px;">
                                    <span id="SelectProduct1_ListView1_ctrl75_lblPrice">18.0000</span>
                                </td>
                            </tr>
                            <tr id="SelectProduct1_ListView1_ctrl76_selectedProduct" style="cursor: pointer;"
                                onclick="SelectProduct1_Label1.innerText=&quot;Original Frankfurter grüne Soße&quot;;SelectProduct1_Button2.click();">
                                <td style="width: 100px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl76_lblID">77</span>
                                </td>
                                <td style="width: 280px; text-align: center;">
                                    <span id="SelectProduct1_ListView1_ctrl76_lblName">Original Frankfurter grüne Soße</span>
                                </td>
                                <td style="width: 100px; padding-left: 4px;">
                                    <span id="SelectProduct1_ListView1_ctrl76_lblPrice">13.0000</span>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <input type="submit" name="SelectProduct1$Button1" value="Select a Product..." id="SelectProduct1_Button1" />
    <span id="SelectProduct1_Label1"></span>
</div>

UPDATE: 更新:

Ok, I found a workaround. 好的,我找到了解决方法。 I added a js function that performs the processes. 我添加了一个执行该过程的js函数。

<script type="text/javascript">
    function selectProduct(prodName) {
        document.getElementById('<%= Label1.ClientID %>').innerText = prodName;
        document.getElementById('<%= Button2.ClientID %>').click();
    }
</script>

... ...

Dim row As HtmlTableRow = CType(e.Item.FindControl("selectedProduct"), HtmlTableRow)
Dim lblName As Label = e.Item.FindControl("lblName")
Dim product As String = ListView1.DataKeys(CType(e.Item, ListViewDataItem).DataItemIndex).Values(1).ToString()
Dim id As String = ListView1.DataKeys(CType(e.Item, ListViewDataItem).DataItemIndex).Values(0).ToString()
row.Attributes.Add("onclick", "selectProduct(" + lblName.ClientID + ".innerText);")

The only issue is that I'll either have to only place one of this user control per page, or register the client script server-side with a unique function name for each control. 唯一的问题是,我要么只需要在每个页面上放置一个用户控件,要么在服务器端用每个控件的唯一函数名注册客户端脚本。

At least I got it to work. 至少我知道了。

You might want to try changing: 您可能想尝试更改:

row.Attributes.Add("onclick", Label1.ClientID + ".innerText=""" + product + """;" + Button2.ClientID + ".click();")

to: 至:

row.Attributes.Add("onclick", Label1.ClientID + ".innerText=""" + product + """;document.getElementById(""" + Button2.ClientID + """).click();")

I have similar problem with custom WebControl. 自定义WebControl也有类似的问题。 I traced the problem back to ASP.NET. 我将问题追溯到ASP.NET。

In short, ASP.NET doesn't know about IE9 browser ID, thus returning default compatibility flags, including EcmaScriptVersion (see System.Web.HttpContext.Current.Request.Browser ), which turns off script generation in base class. 简而言之,ASP.NET不了解IE9浏览器ID,因此会返回默认的兼容性标志,包括EcmaScriptVersion(请参阅System.Web.HttpContext.Current.Request.Browser ),该标志将关闭基类中的脚本生成。

I really hoped that they'll update .net after IE9 release, but no. 我真的希望他们能在IE9发布后更新.net,但没有。

These are (independent) solutions that I was using to circumvent this annoyance: 这些是我用来避免这种烦恼的(独立)解决方案:

  1. Turn on compatibility view on the page with control (see X-UA-Compatible ), preferably as meta tag to force page reload; 使用控件打开页面上的兼容性视图(请参阅X-UA-Compatible ),最好将其作为强制重新加载页面的meta标签;
  2. Add browserCaps section to your web.config (this will effectively turn on cookies and javascript for all browsers): browserCaps部分添加到您的web.config (这将为所有浏览器有效地打开cookie和javascript):

     <configuration> <system.web> <browserCaps> <use var="HTTP_USER_AGENT" /> browser=Unknown cookies=true javascript=true ecmascriptversion=1.0 </browserCaps> </system.web> </configuration> 
  3. Find how to force the control to generate the required scripts no matter what (this one is still pending). 查找如何不管如何强制控件生成所需的脚本(此脚本仍在处理中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM