javascript中的DropDownlist onchange事件更新面板内没有触发DropDownlist
我在更新面板中有一个dropdownlist,在这里我从服务器端点击按钮加载dropdownlist中的数据。 现在我有了一个javascript中的document.getElementById'dropdownlist'.onchange事件处理程序,它在从javascript中选择值时不会被调用 无需使用“更新”面板,即可正常工作。但随后,整个页面都会重新发布到按钮上,单击按钮可在下拉列表中加载数据 以下是aspx代码:javascript中的DropDownlist onchange事件更新面板内没有触发DropDownlist,javascript,asp.net,drop-down-menu,updatepanel,onchange,Javascript,Asp.net,Drop Down Menu,Updatepanel,Onchange,我在更新面板中有一个dropdownlist,在这里我从服务器端点击按钮加载dropdownlist中的数据。 现在我有了一个javascript中的document.getElementById'dropdownlist'.onchange事件处理程序,它在从javascript中选择值时不会被调用 无需使用“更新”面板,即可正常工作。但随后,整个页面都会重新发布到按钮上,单击按钮可在下拉列表中加载数据 以下是aspx代码: <asp:Content ID="BodyContent" r
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Label ID="Label1" runat="server" Text="From Date:" style="display:inline" Font-Size="12"></asp:Label>  
<asp:TextBox ID="TextBox1" runat="server" ReadOnly = "false" ></asp:TextBox>
<img src="calender.png" />  
<asp:Label ID="Label2" runat="server" Text="To Date:" style="display:inline" Font-Size="12"></asp:Label>  
<asp:TextBox ID="TextBox2" runat="server" ReadOnly = "false" ></asp:TextBox>
<img src="calender.png" style="display:inline"/>  
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btnGetTop20Cust" runat="server" Text="Get Top 20 Customers in the selected date range" onclick="btnGo_Click" /><br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" ></asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
检查下拉列表的id,它可能会在更新面板中更改。请在pageLoad中添加更改功能代码,每次更新面板刷新后都会调用pageLoad
function pageLoad() {
document.getElementById('MainContent_DropDownList1').onchange = function () {
var dropDown = document.getElementById('MainContent_DropDownList1');
var value = dropDown[dropDown.selectedIndex].value;
var startDate = document.getElementById('MainContent_TextBox1').value;
var endDate = document.getElementById('MainContent_TextBox2').value;
GetData(value, startDate, endDate);
}
}
除了我上面描述的方法之外,还有其他方法来解决这个问题。
从中我得到了一个非常有用的主意
ASP.NET AJAX确实提供了相应的事件。Init事件在每次页面加载时只触发一次,非常适合一次性初始化任务。它不是通过快捷方式功能提供的,需要稍微谨慎一点,但可以达到以下目的:
<asp:ScriptManager runat="server" />
<script type="text/javascript">
Sys.Application.add_init(function() {
// Initialization code here, meant to run once.
});
</script>
请注意,对Application.add_init的调用位于ScriptManager之后。这是必要的,因为ScriptManager在该位置注入了对MicrosoftAjax.js的引用。尝试在该点之前引用Sys对象将导致“Sys未定义”JavaScript错误。使用类似的代码
然后更改您的javascript代码
document.getElementById'MainContent\u DropDownList1'.onchange=函数{}
到
$document.readyfunction{
document.getElementById'DropDownList1'.onchange=函数{}
};
或
你可以改变
及
你能在这里发布你的代码吗?请添加你的代码,这样我们可以检查它有什么问题。
<asp:ScriptManager runat="server" />
<script type="text/javascript">
Sys.Application.add_init(function() {
// Initialization code here, meant to run once.
});
</script>
function ddlchange(ddl)
{
var value = ddl.value;
var startDate = document.getElementById('MainContent_TextBox1').value;
var endDate = document.getElementById('MainContent_TextBox2').value;
GetData(value, startDate, endDate);
}