[英]How to fire an event when user change a dropdownlist inside an asp.net listview?
我有一个像gridview一样使用的Listview。 在itemtemplate里面,我只有一个'modifyble'字段,一个下拉列表。
当用户更改下拉列表时,我想触发“保存”事件。 我知道我必须为dropdownlist设置autopostback = true,但是我不知道如何触发该事件,因为Visual Studio不允许我在下拉列表中创建dropdownlist的“ on change event” 。
这是我的代码示例
<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>
您可能无法在designer
视图中获得此功能,但是如果直接添加event
处理程序,它将肯定可以工作。 以下是此代码段。
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. }
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.