如何在用户控件中获取列表框选定的值

时间:2018-09-11 13:44:40

标签: c# asp.net webforms listbox

我在UserControl中有一个列表框,我想检索选定的项目,并根据先前选择的值填充第二个列表框。

我不知道为什么,但是当我选择一个项目时,我的事件方法没有被触发。

UserControl .ascx内的列表框

<asp:ListBox ID="ddlViewType" 
             SelectionMode="Multiple" 
             ClientIDMode="Static" 
             CssClass="multiselect-control field-value-name" 
             runat="server" 
             OnSelectedIndexChanged="ddlViewType_SelectedIndexChanged"> 
</asp:ListBox>

UserControl .cs上的ddlViewType_SelectedIndexChanged事件

public void ddlViewType_SelectedIndexChanged(object sender, EventArgs e)
{
    //It should to get here.
}

1 个答案:

答案 0 :(得分:0)

发件人是您的列表框:

要根据asp.net listbox on selectindexchanged doesn't fire an event

中的说明设置事件AutoPostBack="true"
public void ddlViewType_SelectedIndexChanged(object sender, EventArgs e)
{
     ListBox listBox = (ListBox)sender;

     //Use any of those:          
     int selectedIndex = listBox.SelectedIndex;
     var selectedItem = listBox.SelectedItem;

     //You should be able to get the selectedItem here:
}