2010-08-31 64 views
0
不起作用

我有以下* .aspx页面中的AutoPostBack = “真” 的DropDownList的数据源与

<%@ 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的对象列表。

+1

请在page_load方法中验证回发控件进入里面(!Postback)。 – Ramakrishnan 2010-08-31 03:29:42

+1

您还可以使用SelectedIndexChanged事件,在方法中创建一个断点并查找DropDownList1.SelectedValue的值 – Ramakrishnan 2010-08-31 03:32:16

+1

即时消息混淆 - 您想要对选定索引做什么改变?您发布的索引已更改,但没有事件处理程序。 (或Page_Load中的逻辑来处理回发) – RPM1984 2010-08-31 05:26:13

回答

0

我发现了一个答案 - * .dbml已过时,并且GetAllDepartments每个FieldKey返回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

这在我看来,有一些错误的ViewState中。你有没有办法禁用ViewState?

相关问题