开发者

The problem when loading a web user control dynamicly

The web user control: 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" />

And the part of behind codes for this web user control:

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

Next, the web user control "FilterLabel" will be used in another web user control "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>

And the part of behind codes of this web user control is:

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 = filterC开发者_开发百科ontent;
    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
}

So now the problem is the btnRemove_Click event isn't activated when I click the image button "btnRemove".


You should try with bubble event. with your solution you will need on each postback to rebind event handler on each usercontrol for existing filters

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

the rest will be handled by ViewState it self, so you don't need to bind data back to FilterLabel back

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜