2012-02-06 44 views
0

我是一個asp.net新手。基本上,我有一個ListDown中的DropDownList和EditItemTemplate。當在下拉菜單中選擇一個新項目時,用戶不希望點擊LinkBut​​ton進行更新,而是自動進行更新。我已經在javascript中使用代碼背後的OnSelectedIndexChanged =「ddlrank_itemChanged」以及OnChange =「MyFoo()」進行了實驗,但是要做的事情的詳細信息在我身上。在ListView中觸發DropDownList控件的自動更新

我希望我正確地包含代碼示例。任何建議將不勝感激。謝謝。

<asp:ListView ID="ListView1" runat="server" DataKeyNames="rankingID" DataSourceID="SqlDataSource1" OnItemUpdated="ListView1_Item_Updated"> 


    <LayoutTemplate> 
     <table cellpadding="2" width="640px" border="1" runat="server" id="tblRankings"> 
     <tr id="Tr1" runat="server"> 
      <th id="Th1" runat="server">Action</th> 
      <th id="Th3" runat="server">Rank</th> 
      <th id="Th4" runat="server">Committee name</th> 
      <th id="Th5" runat="server">Committee type</th> 
     </tr> 
     <tr runat="server" id="itemPlaceholder" /> 
     </table> 
     <asp:DataPager runat="server" ID="RankingDataPager" PageSize="100"> 
     <Fields> 
      <asp:NextPreviousPagerField ShowFirstPageButton="true" ShowLastPageButton="true" 
      FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|" 
      NextPageText=" &gt; " PreviousPageText=" &lt; " /> 
     </Fields> 
     </asp:DataPager> 
    </LayoutTemplate> 
    <ItemTemplate > 
     <tr id="Tr2" runat="server"> 
     <td> 
      <asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit" /> 
     </td> 
     <td valign="top"> 
      <asp:Label ID="RankLabel" runat="Server" Text='<%#Eval("rank") %>' /> 
     </td> 
     <td valign="top"> 
      <asp:Label ID="CommitteeNameLabel" runat="Server" Text='<%#Eval("committeename") %>' /> 
     </td> 
     <td valign="top"> 
      <asp:Label ID="CommitteeTypeLabel" runat="Server" Text='<%#Eval("committeetype") %>' /> 
     </td> 
     </tr> 
    </ItemTemplate> 
    <EditItemTemplate > 
     <tr style="background-color: #ADD8E6"> 
     <td> 
      <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />&nbsp; 
      <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" /> 
     </td> 
     <td> 
      <asp:DropDownList ID="ddlrank" 
      DataSourceID="sdsrank" 
      DataValueField="vchvalue" 
      DataTextField="vchvalue" 
      OnSelectedIndexChanged="ddlrank_itemChanged" 
      OnChange="MyFoo()" 
      SelectedValue='<%# Bind("rank") %>' runat="server" > 
      </asp:DropDownList> 
     </td> 
     <td> 
      <asp:TextBox ID="CommitteeNameTextBox" runat="server" Enabled="false" ReadOnly="true" Text='<%#Bind("committeename") %>' 
      MaxLength="200" /><br /> 
     </td> 
     <td> 
      <asp:TextBox ID="CommitteeTypeTextBox" runat="server" Enabled="false" ReadOnly="true" Text='<%#Bind("committeetype") %>' 
      MaxLength="20" /><br /> 
     </td> 
     </tr> 
    </EditItemTemplate> 

</asp:ListView> 

回答

3

我認爲您的下拉列表屬性中缺少AutoPostBack="true"。希望能幫助到你。

+0

好一點。我沒有在我的回答中提到這一點,這可能是海報的問題。 :-) – Jay 2012-02-06 22:14:35

+0

即使這樣,OP也需要編寫額外的代碼來處理Update操作,因爲他/她基於這些更新的SQL數據源。 OP需要繼承下拉列表類並創建一個Command控件http://aspadvice.com/blogs/joteke/archive/2006/01/21/14794.aspx – 2012-02-06 22:17:05

0

你正朝着正確的方向前進。您可以使用「OnSelectedIndexChanged」,或者您可以在處理SelectedIndexChanged事件的代碼隱藏頁面中創建一個函數。無論哪種方式,當用戶從下拉列表中進行選擇時,您會得到一個回傳,並執行該函數。在這個功能中你可以做任何你想做的事情。在這種情況下,可能意味着檢查所選值是什麼,並根據新選擇在屏幕上設置其他值。當該功能退出時,新的屏幕將以任何更新的數據發送到用戶的瀏覽器。

0

感謝您的回覆,他們幫助指導和完善我的互聯網搜索!

我終於找到了一個簡單的解決方案。

它只是需要在下拉列表中定義的OnSelectedIndexChanged代碼隱藏功能的兩行代碼:

protected void ddlrank_itemChanged(object sender, EventArgs e) 
{ 
    ListViewDataItem item = (ListViewDataItem)((DropDownList)sender).Parent; 
    ListView1.UpdateItem(item.DisplayIndex, false); 
} 
相關問題