[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 943
  • Last Modified:

OnClick event on user control to update results on another user control

Hi Experts,

I am working on ASP.net2.0 using C#

In my aspx page, I have 2 different control.

1. Search User Control - where user can search by clicking search button
2. List User Control - where list will be displayed based on the search crieteria entered by user.


I am not able to do the following

- On click of search button on search control, call method on list user control to update the results

Please help me how to do this.

I have created a test code.

Please find my aspx and user control below

Please help me how to proceed

Many Thanks


ASPX CODE
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="callUserControlEvents.aspx.cs" Inherits="callUserControlEvents" %>

<%@ Register Src="controls/Usercontrol_List.ascx" TagName="Usercontrol_List" TagPrefix="uc1" %>
<%@ Register Src="controls/usercontrolSearch.ascx" TagName="usercontrolSearch" TagPrefix="uc2" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:Usercontrol_List ID="Usercontrol_List1" runat="server" />
        <br />
        <uc2:usercontrolSearch ID="UsercontrolSearch1" runat="server" />
    </div>
    </form>
</body>
</html>

Open in new window


ASPX CODE BEHIND
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class callUserControlEvents : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Open in new window


SEARCH USER CONTROL
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="usercontrolSearch.ascx.cs" Inherits="controls_usercontrolSearch" %>
<asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Search" />

Open in new window


SEARCH USER CONTROL CODE BEHIND
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class controls_usercontrolSearch : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

    }
}

Open in new window


LIST USER CONTROL
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Usercontrol_List.ascx.cs" Inherits="controls_Usercontrol_List" %>
<asp:Label ID="lblMessage" runat="server"></asp:Label><br />

Open in new window


LIST USER CONTROL CODE BEHIND
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class controls_Usercontrol_List : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lblMessage.Text = "Click Event not called";
        }
    }

    public void updateLabelInList()
    {
        lblMessage.Text = "Event Called";        
    }
}

Open in new window


Please help
0
tia_kamakshi
Asked:
tia_kamakshi
  • 2
1 Solution
 
Rahul AgarwalTeam LeaderCommented:
0
 
tia_kamakshiAuthor Commented:
Thanks agarwalrahul,

I have downloaded the code and understood.

But they are using xml to save data and another control is reading xml to bring list.

In my case I have no xml data. I need to take data from search control and produce list from the same data in list conrol.

Please suggest
0
 
Swapnil PipariaArchitectCommented:
Hi tia_kamakshi,

     You need to raise event from search user control which you main page can subscribe to and In your main page you can handle the event of your search control and populate List control's data. Below is modified code to solve your problem.

SEARCH USER CONTROL CODE BEHIND
.....
public partial class controls_usercontrolSearch : System.Web.UI.UserControl
{
    public event EventHandler SearchClick;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
          if (this.SearchClick != null)
             this.SearchClick(sender, e);
    }
}

Open in new window

                                 

ASPX CODE BEHIND
......

public partial class callUserControlEvents : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
           this.UsercontrolSearch1.SearchClick += new EventHandler(UsercontrolSearch1_SearchClick);
    }

    public void UsercontrolSearch1_SearchClick(object sender, EventArgs e)
   {
         Usercontrol_List1.updateLabelInList();
   }

}

Open in new window

                                 

Thanks,
Netswap.
0
 
tia_kamakshiAuthor Commented:
Many Many Thanks

It works great
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now