IE9 Javascript未定义错误

IE9 Javascript undefined error

本文关键字:错误 未定义 Javascript IE9      更新时间:2023-09-26

我在IE9中得到一个javascript错误,我没有在Chrome或FF中得到,我找不到一个好的方法来解决它。代码在兼容模式下工作。

在示例代码中,我有一个自定义用户控件,它由几个基本控件和一个AJAX模型popupextender组成。listview填充了一些产品,我附加了一个onclick事件onitemdatabound,该事件填充了一个标签,并关闭了listview项的每个htmlrow的modelpopup。

IE9找不到Button2,无法完成javascript功能。当前版本的Chrome和FF执行预期。我是不是漏掉了什么?

源:

<%@ 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>
代码:

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

生成的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>

更新:

好的,我找到了一个解决方法。我添加了一个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);")

唯一的问题是,我要么必须在每个页面上只放置一个用户控件,要么为每个控件注册具有唯一函数名的客户端脚本服务器端。

您可以尝试更改:

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

:

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

我有类似的自定义WebControl问题。我把问题追溯到ASP.NET。

简而言之,ASP。. NET不知道IE9浏览器ID,因此返回默认的兼容性标志,包括EcmaScriptVersion(见System.Web.HttpContext.Current.Request.Browser),它关闭基类中的脚本生成。

我真的希望他们能在IE9发布后更新。net,但是没有。

这些是我用来解决这个烦恼的(独立的)解决方案:

  1. 在带有控件的页面上打开兼容性视图(参见X-UA-Compatible),最好作为元标签强制页面重新加载;
  2. 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. 查找如何强制控件生成所需的脚本,无论如何(这一个仍然悬而未决)。