当用户在asp.net listview中更改下拉列表时如何触发事件?

时间:2014-10-22 08:03:19

标签: asp.net listview

我有一个Listview,我使用像gridview。在itemtemplate中我只有一个可修改的'字段,下拉列表。

我想解雇一个' Save'用户更改下拉列表时的事件。我知道我必须设置下拉列表中的autopostback = true,但我不知道如何解雇该事件,因为视觉工作室不允许我创建变更事件'在列表视图中的下拉列表。

这是我的代码示例

<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>

2 个答案:

答案 0 :(得分:1)

您可能无法在designer视图中看到此内容,但如果您直接添加event处理程序,它肯定会有效。以下是此代码段。

  1. OnSelectedIndexChanged="myComboBox_SelectedIndexChanged"添加到myComboBox
  2.  <asp:DropDownList runat="server" ID="myComboBox" DataSourceID="dsAgenti" DataTextField="customer"
                                DataValueField="customer" Width="150px" AutoPostBack="true" OnSelectedIndexChanged="myComboBox_SelectedIndexChanged">
                            </asp:DropDownList>
    
    1. 接下来在服务器端,使用以下event处理程序。
    2. 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 :(得分:0)

protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
    {


    DropDownList ddl = (DropDownList)sender;
    ListViewItem dst = (ListViewItem)ddlListFind.NamingContainer;
    DropDownList gddl = (DropDownList)dst.FindControl("dropdownlist1");

    HiddenField hid_msg = (HiddenField)item1.FindControl("hidmsg");
    hid_msg.Visible = true; hid_msg.Value = "Dropowntext : " + gddl.SelectedItem.Text.Trim() + " and value of dropdown is : " + gddl.SelectedItem.Value.Trim();


    }

或访问此链接以获取更多详细信息: https://forums.asp.net/t/1357900.aspx?SelectedIndexChanged+of+a+DropDownList+which+is+inside+a+ListView