动态加载Web用户控件时的问题

时间:2011-02-21 00:30:52

标签: asp.net

网络用户控件:FilterLabel

<span style=" display:block; float:left; margin:5px; padding: 5px; border: 1px inset #000000; font-family: Tahoma; font-size: small;">
<asp:Label ID="lblFilterDisplay" runat="server" Text="Product/Service: This is a testing"></asp:Label>
&nbsp;
<asp:ImageButton ID="btnRemove" runat="server" ImageUrl="~/Images/remove.png" OnClick="btnRemove_Click" />

此Web用户控件的后面代码部分:

public event EventHandler RemoveClick;
......
public void btnRemove_Click(object sender, EventArgs e)
{
    if (RemoveClick != null)
        RemoveClick(this, EventArgs.Empty);
}

接下来,Web用户控件“FilterLabel”将在另一个Web用户控件“FilterList”中使用:

<%@ Register TagPrefix="uc" TagName="FilterLabel" Src="~/Controls/FilterLabel.ascx" %>
<asp:Panel ID="FilterList" runat="server" Width="100%" GroupingText="Filter List" CssClass="filterList">
</asp:Panel>

此Web用户控件的后面代码部分是:

protected override void OnInit(EventArgs e)
{
    if (IsPostBack)
    {
        BindFilters();
    }

}

public void BindFilters()
{
    List<FilterLabel> filters = Filters;
    foreach (FilterLabel filter in filters)
    {
        FilterList.Controls.Add(filter);
    }
}

public List<FilterLabel> Filters
{
    get
    {
        List<FilterLabel> filters = Session["Filters"] as List<FilterLabel>;
        if (filters == null)
        {
            filters = new List<FilterLabel>();
        }
        return filters;
    }
}

public void AddFilter(string filterName, string filterContent, string filterValue = null)
{
    FilterLabel filter = LoadControl("~/Controls/FilterLabel.ascx") as FilterLabel;
    filter.ID = "Filter";
    filter.FilterContent = filterContent;
    filter.FilterName = filterName;
    filter.Value = filterValue;
    filter.RemoveClick += new EventHandler(RemoveFilter);
    List<FilterLabel> filters = Filters;
    filters.Add(filter);
    Session["Filters"] = filters;
    BindFilters();
}

private void RemoveFilter(object sender, EventArgs e)
{
    //some handling codes
}

所以现在问题是当我单击图像按钮“btnRemove”时,btnRemove_Click事件未激活。

1 个答案:

答案 0 :(得分:0)

你应该尝试泡泡事件。使用您的解决方案,您将需要在每次回发时为每个用户控件重新绑定事件处理程序以获取现有过滤器

public int FilterCount 
{
    get{
        if(ViewState["filter_count"] == 0){
            FilterCount = 0;
        }
        return (int)ViewState["filter_count"];
    }
    set{
        ViewState["filter_count"] = value;
    }
}

protected void RecreateFilterControlls()
{
    for(int i =0; i < FilterCount; i++){
        FilterLabel filter = LoadControl("~/Controls/FilterLabel.ascx") as FilterLabel;
        FilterList.Controls.Add(filter);
        filter.RemoveClick += new EventHandler(RemoveFilter);
    }
}


protected override void OnInit(EventArgs e)
{
    if (IsPostBack)
    {
        RecreateFilterControlls();
    }

}
// this is for button which will add new FilterLabel UserControl
protected void AddNewFilter_Click(object sender, EventArgs e)
{
    // just create filter
    AddFilter(<FilterNameTextBox|combobox>, <FilterContentTextBox>, <filterValue>);
}


public void AddFilter(string filterName, string filterContent, string filterValue = null)
{
    FilterLabel filter = LoadControl("~/Controls/FilterLabel.ascx") as FilterLabel;
    filter.ID = "Filter";
    filter.FilterContent = filterContent;
    filter.FilterName = filterName;
    filter.Value = filterValue;
    filter.RemoveClick += new EventHandler(RemoveFilter);
    // increament filter count
    FilterCount++;

}

private void RemoveFilter(object sender, EventArgs e)
{
    //some handling codes
    //remove control which caused this event
    FilterList.Controls.Remove(sender); // or you will need to ask for IndexOf(sender);
    FilterCount--;
}

其余的将由ViewState自己处理,因此您不需要将数据绑定回FilterLabel