2010-08-31 66 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?

相關問題