3
Answers

How to call same event of user control on multiple aspx.cs pages

Ask a question
Ajay Gautam

Ajay Gautam

14y
4.4k
1

Hi All,

I have one user control which has 2 gridview first grid has columns (GroupName, Description, chkRemove checkbox) another gridview has columns (GroupName, Description, chkAdd), apart from that i dont have any code on .cs file of usercontrol.

I want to call same user control on 4 aspx pages and datagrid binding, sorting, paging and search functionality i want to do on respective aspx.cs pages not on user control where i am calling this uercontrol because for every aspx page field name is same but when i am binding the data to the grid that logic is different.

For first aspx page i called my usercontrol like this and everything is working fine: -

 <asp:View ID="vManageGroups" EnableTheming="true" EnableViewState="true" runat="server">
<ajax:TabContainer runat = "server">
<ajax:TabPanel HeaderText="Default Role Groups" ID="tpRoleGroups" runat="server">
<HeaderTemplate>
Default Role Groups
</HeaderTemplate>
<ContentTemplate>
<Level3Groups:GroupSelector ID="gsAU3Groups" runat="server" OnGroupRebind="gsAU3GroupRebind"
OnGroupAdded="gsAU3GroupAdded" OnGroupRemoved="gsAU3GroupRemoved" OnGroupSort="gsAU3GroupSort" />
</ContentTemplate>
</ajax:TabPanel>
</ajax:TabContainer>

and on user control i have created delegate and event.

public delegate void GroupAction(object sender, EventArgs e, string groupName);
    public event GroupAction GroupAdded;
    public event GroupAction GroupRemoved;

    public delegate void GridViewPaging(object sender, GridViewPageEventArgs e, string groupName);
    public event GridViewPaging GroupRebind;

    public delegate void GridViewSort(object sender, GridViewSortEventArgs e);
    public event GridViewSort GroupSort;

then in user control for chkAdd checkbox i have added this code and after that i did all my
coding on aspx.cs page and it is working fine.
usercontrol.ascs.cs
protected void chkAdd_Click(object sender, EventArgs e)
    {
        CheckBox chkAdd = (CheckBox)sender;
        GridViewRow viewRow = (GridViewRow)chkAdd.Parent.Parent;
        HiddenField hfAddName = (HiddenField)viewRow.FindControl("hfaddName");
        string groupAddName = hfAddName.Value;
        GroupAdded(sender, e, groupAddName);
    }
and Aspx.cs
protected void gsAU3GroupAdded(object sender, EventArgs e, string groupName)
{
// My code here
}
Same type of thing i did for ChkRemove_Click and paging and sorting.

Now problem is i want to call this usercontrol on second aspx page then how to handle
that page's logic inside the chkadd_click event on usercontrol.ascx.cs.
I want any if like condition, if this page is clicked then go on this event like this

let me know if you need any other information. Any help is appreciated.

Answers (3)