2014-10-22 43 views
0

我有一个列表视图,我像一个GridView使用。在itemtemplate里面我只有一个'modifyble'字段,一个下拉列表。如何在用户更改asp.net listview内的下拉列表时触发事件?

我想在用户更改下拉列表时触发“保存”事件。我知道我必须设置dropdownlist的autopostback = true,但是我不知道如何激发事件,因为Visual Studio不允许我在下拉列表中创建“更改事件” 。

这是我的代码示例

<asp:ListView ID="lvDmr" runat="server" DataSourceID="dsDmr" DataKeyNames="id"> 
     <ItemTemplate> 
      <table style="width: 100%;" cellspacing="0" cellpadding="8"> 
       <tr style="width: 100%;"> 
        <td class="colonna-griglia" style="width: 5%;"> 
         <%# Convert.ToDateTime(Eval("data_rilevazione")).ToString("d") %> 
        </td> 
        <td class="colonna-griglia"> 
         <%# Eval("rivista")%> 
        </td> 
        <td class="colonna-griglia"> 
         <asp:DropDownList runat="server" ID="myComboBox" DataSourceID="dsAgenti" DataTextField="customer" 
          DataValueField="customer" Width="150px" AutoPostBack="true"> 
         </asp:DropDownList> 
        </td> 
      ... 
      .... 
    </asp:listview> 

回答

1

designer鉴于您可能没有获得,但如果你直接添加event处理它肯定会工作。以下是此代码段。

  1. OnSelectedIndexChanged="myComboBox_SelectedIndexChanged"加到myComboBox
<asp:DropDownList runat="server" ID="myComboBox" DataSourceID="dsAgenti" DataTextField="customer" 
          DataValueField="customer" Width="150px" AutoPostBack="true" OnSelectedIndexChanged="myComboBox_SelectedIndexChanged"> 
         </asp:DropDownList> 
  • 接着,在服务器端,使用以下为event处理程序。
  • protected void myComboBox_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        DropDownList ddlListFind = (DropDownList)sender; 
        ListViewItem item1 = (ListViewItem)ddlListFind.NamingContainer; // item1, is current row of Listview, which hold the dropdownlist that caused postback. 
    } 
    

    更多的帮助 - http://forums.asp.net/t/1357900.aspx?SelectedIndexChanged+of+a+DropDownList+which+is+inside+a+ListView

    +1

    许多许多..thanks! – stighy 2014-10-22 08:44:56