我使用masterpage.In ascx页面我的selectedindexchange事件没有触发。
这是我的代码:
我的ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FilterList.ascx.cs"
Inherits="F8.B2B.WEB.UserControls.Common.FilterList.FilterList" %>
<asp:UpdatePanel UpdateMode="Always" runat="server">
<ContentTemplate>
<div id="filterList" runat="server">
</div>
</ContentTemplate>
</asp:UpdatePanel>
我的ascx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
createFilterLists();
}
}
private void createFilterLists()
{
ListBox dpList = new ListBox()
{
ID = ControlID
};
dpList.Items.Clear();
if (lst_ListItem != null)
{
foreach (ListItem item_ in lst_ListItem)
{
dpList.Items.Add(item_);
}
dpList.Items[0].Selected = true;
dpList.AutoPostBack = true;
dpList.EnableViewState = true;
dpList.SelectedIndexChanged += new EventHandler(myListBox_SelectedIndexChanged);
filterList.Controls.Add(dpList);
}
}
protected void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
// might be entered on change
}
答案 0 :(得分:4)
因为您的列表框是由代码动态生成的,所以即使它是回发,您也需要在每个页面加载中添加它。
答案 1 :(得分:0)
在OnInit事件中创建动态控件并添加事件处理程序
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateControls();
myDataGrid.SomeEventHandler += new ...
}
在OnLoad事件中绑定数据和填充控件
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillControls();
myDataGrid.DataSource = somedatasource;
myDataGrid.DataBind();
}
}
private void CreateControls()
{
ListBox dpList = new ListBox()
{
ID = ControlID
};
dpList.AutoPostBack = true;
dpList.EnableViewState = true;
dpList.SelectedIndexChanged += new EventHandler(myListBox_SelectedIndexChanged);
filterList.Controls.Add(dpList);
}
private void FillControls()
{
dpList.Items.Clear();
if (lst_ListItem != null && lst_ListItem.Count > 0)
{
foreach (ListItem item_ in lst_ListItem)
{
dpList.Items.Add(item_);
}
dpList.Items[0].Selected = true;
}
}