[英]AutoPostBack=“true” doesn't work for DropDownList with DataSource
我有以下* .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>
和* .aspx.cs页面
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
public partial class Admin_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataClassesDataContext datacontext = new DataClassesDataContext();
DropDownList1.DataSource = datacontext.GetAllDepartments(false);
DropDownList1.DataBind();
}
}
}
当我在浏览器中的dropdownList中更改值时,它会执行PostBack,但是会选择PostBack之后列表的第一项-它不会保存我的值。
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的每个FieldKey都返回0-因此,存在项的重复值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.