我有以下* .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)是一个存储过程,它返回一个具有两个属性的对象List - FieldKey和Name。
答案 0 :(得分:1)
您可以像这样设置事件:
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();
}
}
答案 1 :(得分:1)
在我看来,ViewState存在问题。你以某种方式禁用了ViewState吗?
答案 2 :(得分:0)
我找到了答案 - * .dbml已过时,GetAllDepartments会将每个FieldKey重新调整为0 - 因此,项目的值重复。