This is a migrated thread and some comments may be shown as answers.

RadMenu As UserControl

1 Answer 98 Views
Menu
This is a migrated thread and some comments may be shown as answers.
L
Top achievements
Rank 1
L asked on 03 Feb 2009, 04:53 PM
hi

I have a menu as a usercontrol which has 26 items value from A to Z,

I placed this userControl on a page and I want it to act as filter.

For example, when i click item A the grid would populate with all the records that starts with A.

Now the thing is how do i call the click event of the menu items on a page? Thanks

1 Answer, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 04 Feb 2009, 04:47 AM
Hi L,

You can Pass information from User Control to Main form using Event And delegates,ie using the  delegate you can raise event from user control to main page,
here is one sample code that I have used in my application, try altering that and let me know how it goes.

Here is my user control,

<%@ Control Language=”C#” AutoEventWireup=”true” CodeFile=”WebUserControl.ascx.cs” Inherits=”Dalegate_WebUserControl” %>

<asp:Button ID=”btnTest” runat=”server” Text=”I am Inside User Control” OnClick=”btnTest_Click” />

On WebUserControl.ascx.cs I have written simple delegate and event handler as shown below,

public partial class Dalegate_WebUserControl : System.Web.UI.UserControl
{

    // Delegate declaration
    public delegate void OnButtonClick(string strValue);

    // Event declaration
    public event OnButtonClick btnHandler;
    
     protected void btnTest_Click(object sender, EventArgs e)
    {
           // Check if event is null
           if (btnHandler != null)
               btnHandler(string.Empty);

    }
}

Now take a look at aspx page,

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”Dalegate_Default” %>

<%@ Register Src=”WebUserControl.ascx” TagName=”WebUserControl” TagPrefix=”uc1″ %>

<!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>Untitled Page</title>
</
head>
<
body>
    <form id=”form1″ runat=”server”>
    <div>
        <asp:Label ID=”lblText” Text=”I am On Main Page : “
                   runat
=”server”></asp:Label>
                 <br />
        <uc1:WebUserControl ID=”WebUserControl1″ runat=”server” />
   
   
</div>
    </form>
</
body>
</
html>
Lets look at cs file,

public partial class Dalegate_Default : System.Web.UI.Page
{
      protected void Page_Load(object sender, EventArgs e)
      {
             // Declare and Define Event of User Control. When User Clicks on button
             (which is inside UserControl)
             // below event is raised as I have called raised that event on Button Click
             WebUserControl1.btnHandler += new 
             Dalegate_WebUserControl
.OnButtonClick(WebUserControl1_btnHandler);
       
      }

      void WebUserControl1_btnHandler(string strValue)
      {
             Response.Write(“Main Page Event);
      }   
}

 Now when you run the application and clicks on button you can see that when user click on button the user control raise the click event and calls the WebUserControl1_btnHandler(string strValue) method on main page.


Thanks,
Shinu.

Tags
Menu
Asked by
L
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or