我在IE9中遇到javascript错误,我没有在Chrome或FF中获取,我无法找到解决问题的好方法。代码 在兼容模式下工作。
在示例代码中,我有一个自定义用户控件,它由一些基本控件和一个AJAX modelpopupextender组成。 listview填充了一些产品,我附加了一个onclick事件onitemdatabound,它填充了一个标签,并将modelpopup关闭到listview项的每个htmlrow。
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;"> </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;">
</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="Chai";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="Chang";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="Outback Lager";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="Flotemysost";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="Mozzarella di Giovanni";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="Röd Kaviar";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="Longlife Tofu";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="Rhönbräu Klosterbier";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="Lakkalikööri";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="Original Frankfurter grüne Soße";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);")
唯一的问题是,我要么必须每页只放置一个此用户控件,要么为每个控件注册客户端脚本服务器端的唯一函数名称。
至少我得到了它的工作。
答案 0 :(得分:1)
您可能想尝试更改:
row.Attributes.Add("onclick", Label1.ClientID + ".innerText=""" + product + """;" + Button2.ClientID + ".click();")
为:
row.Attributes.Add("onclick", Label1.ClientID + ".innerText=""" + product + """;document.getElementById(""" + Button2.ClientID + """).click();")
答案 1 :(得分:0)
我有自定义WebControl的类似问题。我将问题追溯到ASP.NET。
简而言之,ASP.NET不了解IE9浏览器ID,因此返回默认的兼容性标志,包括EcmaScriptVersion(请参阅System.Web.HttpContext.Current.Request.Browser),它会关闭基类中的脚本生成。
我真的希望他们在IE9发布后更新.net,但不会。
这些是(独立的)解决方案,我用它来规避这种烦恼:
将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>
了解如何强制控件生成所需的脚本,无论是什么(这个脚本仍处于待处理状态)。