M H
M H

Reputation: 2183

How to communicate between user controls in Asp.Net

I have 5 files.

  1. Default.aspx
  2. Search.ascx
  3. SearchSQL.ascx
  4. Grid.ascx
  5. GridSQL.ascx

I have registered the ascx files in the default.aspx page and use properties to expose the controls to the default page. And that works great.

My issue is how do I send data back and fourth between the different ascx pages? If I register on any of those it will give me a Circular file reference error.

Using public properties, I have the Search.ascx registered on the GridSQL.ascx to pass the search parameters into Gridsql string, and then the GridSQL.ascx on the Grid.ascx file to pass the sql string to the grid databind.

There has got to be a much easier way to pass data BACK & FOURTH between pages, or am I wrong? When you try to register on the other page to pass data back to the page that sent it, you get the circular file reference error. I have heard a few resolutions like changing file structure, which I have tried, and also about Batch, but that kills performance. Believe i have spent days trying to find resolutions on this. I was going to comment on some questions but Stack does not allow me until I have 50 Rep.

My company is requiring us to use all separate files from now on and I just cant believe this is the best way to communicate between user controls.

Upvotes: 1

Views: 3759

Answers (1)

Win
Win

Reputation: 62301

Proper way is you want to bubble up the child control's event to parent.

Then let parent to forward the event to other controls.

Note: Here is the demo. You might want to rename delegates and methods which make sense to your scenario.

Search (User Control which fires the event)

<%@ Control Language="C#" AutoEventWireup="true" 
    CodeBehind="Search.ascx.cs" Inherits="DemoWebForm.Search" %>
<asp:TextBox runat="server" ID="SearchTextBox" />
<asp:Button runat="server" ID="SearchButton" 
    Text="Search" OnClick="SearchButton_Click" />

public delegate void MessageHandler(string searchText);

public partial class Search : System.Web.UI.UserControl
{
    public event MessageHandler SearchText;

    protected void SearchButton_Click(object sender, EventArgs e)
    {
        SearchText(SearchTextBox.Text);
    }
}

GridSql (User Control)

Finally, GridSql.ascx receives the search text.

<%@ Control Language="C#" AutoEventWireup="true" 
    CodeBehind="GridSql.ascx.cs" Inherits="DemoWebForm.GridSql" %>
<asp:Label runat="server" ID="SearchTextLabel"/>

public partial class GridSql : System.Web.UI.UserControl
{
    public void SearchTextMethod(string searchText)
    {
        SearchTextLabel.Text = searchText;
    }
}

Parent

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="Parent.aspx.cs" Inherits="DemoWebForm.Parent" %>

<%@ Register src="~/Search.ascx" tagname="Search" tagprefix="uc1" %>
<%@ Register src="~/GridSql.ascx" tagname="GridSql" tagprefix="uc2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <uc1:Search ID="Search1" runat="server" />
        <uc2:GridSql ID="GridSql1" runat="server" />
    </form>
</body>
</html>

public partial class Parent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Search1.SearchText += m => GridSql1.SearchTextMethod(m);
    }
}

Upvotes: 5

Related Questions