2015-05-13 24 views
1

在ListView的EditTemplate中,我有一個DropDownList控件。 DropDownList填充在ListViewItemDataBound方法中(我發現只有這樣才能讓列表在EditTemplate中填充...... DropDownList只存在於EditTemplate中;它必須是ItemTemplate中的標籤)。ListView的OnItemUpdating方法中的ASP.NET DropDownList始終具有0的SelectedIndex

該列表將填充,並且它可以工作。我遇到的問題是點擊更新按鈕並調用ItemUpdating方法時,DropDownList的SelectedIndex總是爲零,無論當時選擇了哪個值。

的ASPX代碼,減去多餘的模板看起來是這樣的:

<asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound" OnItemUpdating="ListView1_ItemUpdating" OnItemEditing="ListView1_ItemEditing"> 
<EditItemTemplate> 
    <tr style=""> 
    <td><asp:DropDownList CssClass="vaultDropDownList" ID="EditStaffList" runat="server" AutoPostBack="false" CausesValidation="True"></asp:DropDownList></td> 
    <td> 
     <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" /> 
     <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" /> 
    </td> 
    </tr> 
    </EditItemTemplate> 
</asp:ListView> 

而隱藏代碼:

protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e) 
{    
    ListView1.EditIndex = e.NewEditIndex; 
} 

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
    if (e.Item.DisplayIndex == ListView1.EditIndex) 
    { 
     DropDownList ddl = e.Item.FindControl("EditStaffList") as DropDownList; 
     BindDropDownList(ddl); 
    } 
} 

protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e) 
{ 
    DropDownList staff = ListView1.Items[e.ItemIndex].FindControl("EditStaffList") as DropDownList; 

    //SQL update stuff will go here 
    ListView1.EditIndex = -1; 
} 

如果我嘗試填充的DropDownList在Page_Load方法使用IsPostBack,將下拉控件始終返回null

任何幫助非常感謝!

編輯:ListView控件綁定:

protected void DisplayListView(object sender, EventArgs e) 
{ 
    string connString = SQLstringTooLong; 
    SqlConnection sqlConnection = new SqlConnection(connString); 
    DataTable initialTable = new DataTable(); 
    string queryStatement = "SELECT lotsastuff"; 
    SqlDataAdapter dataAdapter = new SqlDataAdapter(queryStatement, sqlConnection); 
    dataAdapter.Fill(initialTable); 
    ListView1.DataSource = initialTable; 
    ListView1.DataBind(); 
} 

EDIT2:這裏的下拉列表綁定方法:

public static void CreateStaffList(DropDownList list, string searchGroup) 
{ 
    ArrayList userList = new ArrayList(); 
    //more SQL stuff 
    list.DataSource = userList; 
    list.DataBind(); 
} 
+0

我們可以看看你是如何將數據綁定到你的ListView的嗎?我懷疑你在每個PostBack上綁定它。 –

+0

我像上面那樣在ItemDataBound方法中綁定它。我懷疑自己也是,但不知道如何只在EditTemplate中綁定它,而不是在ListTemplate的ItemDataBound方法的任何地方的ItemTemplate中。 – caerwyn589

+0

「ItemDataBound」事件實際上只是數據綁定過程的一部分。我在尋找的是當你真正設置一個數據源並調用DataBind。代碼看起來像「ListView1.DataSource = someDataSource;'和'ListView1.DataBind();'。我的猜測是這些行在你的'Page_Load'事件中。我可以請看看你是如何做這個DataBinding? –

回答

0

確保你是不是在每次回髮結合您的ListView。把它包裹在一個條件來檢查。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     DisplayListView(); 
    } 
} 

如果不這樣做,你的整個的ListView,包括你的DropDownLists將重新綁定。這會導致DropDownList將其SelectedIndex重置爲0.

+0

現在還不行,但想再次表示感謝。 – caerwyn589

+0

不是問題!歡迎來到StackOverflow! –

相關問題