将 AutoPostback 添加到 DropdownList 时出现 System.ArgumentNullException
当我将 AutoPostback = True 添加到我的 DropDownList (使用 OnSelectedIndexChanged)时,我得到一个 System.ArgumentNullException:值不能为空。
我的控件如下所示:
<asp:DropDownList ID="dropdown" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdown_OnSelectedIndexChanged" />
填充控件的代码:
dropdown.Items.Add(new ListItem("Item 1", "1"));
dropdown.Items.Add(new ListItem("Item 2", "2"));
dropdown.Items.Add(new ListItem("Item 3", "3"));
更新
在使用以下代码添加空页面时我也收到错误:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_dev_test_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server" />
</div>
</form>
</body>
</html>
When I add AutoPostback = True to my DropDownList (with OnSelectedIndexChanged) I get a System.ArgumentNullException: Value cannot be null.
My control looks like this:
<asp:DropDownList ID="dropdown" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdown_OnSelectedIndexChanged" />
And code to fill control:
dropdown.Items.Add(new ListItem("Item 1", "1"));
dropdown.Items.Add(new ListItem("Item 2", "2"));
dropdown.Items.Add(new ListItem("Item 3", "3"));
Update
I get the error when adding an empty page with the following code too:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_dev_test_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server" />
</div>
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了问题。我在 web.config 中使用的一个组件导致了这个问题。因此,我在与使用错误组件的页面相同的类别中添加了另一个 web.config,这解决了问题。 (只要我不在该页面上使用 AutoPostBack ;))
Solved the problem. A component I was using inside my web.config was causing the problem. So I added another web.config in the same category as the page using the faulty component and that solved the problem. (as long as I don't use AutoPostBack on that page ;))