C# 自动回邮=";“真的”;不';使用DataSource不适用于DropDownList

C# 自动回邮=";“真的”;不';使用DataSource不适用于DropDownList,c#,asp.net,postback,C#,Asp.net,Postback,我有以下*.aspx页面 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Admin_Test" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3

我有以下*.aspx页面

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Test.aspx.cs" Inherits="Admin_Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form2" runat="server">
<div>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataTextField="Name" DataValueField="FieldKey" />

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

</div>
</form>
</body>
</html>
当我在dropdownList(在浏览器中)中更改值时,它会回发,但它会选择回发后列表的第一项-它不会保存我的值


GetAllDepartments(isDeleted)是一个存储过程,它返回具有两个属性的对象列表—FieldKey和Name。

您可以如下设置事件:

protected void Page_Load(object sender, EventArgs e)
{
    DropDownList1.SelectedIndexChanged += new System.EventHandler(this.On_SelectedIndexChanged);

    if (!IsPostBack)
    {
        DataClassesDataContext datacontext = new DataClassesDataContext();
        DropDownList1.DataSource = datacontext.GetAllDepartments(false);
        DropDownList1.DataBind();
    }


}

在我看来,ViewState似乎有问题。您是否以某种方式禁用了ViewState?

我找到了一个答案-*。dbml已过时,GetAllDepartments将0作为每个字段键-因此,存在重复的项值。

在回发时,请验证控件是否在页面加载方法中(!postback)进入内部。您还可以使用SelectedIndexChanged事件,在方法中做一个断点,找出DropDownList1的值。SelectedValueim混淆了-您试图对已更改的选定索引执行什么操作?您正在发回已更改的索引,但没有事件处理程序。(或加载页面中的逻辑以处理回发)抱歉,我找到了一个答案-*.dbml已过时,GetAllDepartments将0作为每个字段键返回-因此,存在重复的项值。
protected void Page_Load(object sender, EventArgs e)
{
    DropDownList1.SelectedIndexChanged += new System.EventHandler(this.On_SelectedIndexChanged);

    if (!IsPostBack)
    {
        DataClassesDataContext datacontext = new DataClassesDataContext();
        DropDownList1.DataSource = datacontext.GetAllDepartments(false);
        DropDownList1.DataBind();
    }


}