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

AJAX and ASCX controls

2 Answers 408 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 01 Aug 2009, 12:06 AM
Hi I've built a ASP.NET user control (ascx) with a radgrid in it.
The control is placed on a ASPX page. When a user click on a row in the radgrid
 
When a user click on the radgrid in the ASCX I want to do an ajax update of a couple of graphs on the ASPX page. 

What's the best way to get the event in the ascx to the aspx page and update the controls based on the current selection in the grid?

Thanks!

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 04 Aug 2009, 09:38 AM
Hi Mike,

I have one suggestion to do this, you can Bubble the event From Web User Controls(ascx) to the parent webform(aspx),
I have done the same for button( ie the button click event from ascx page is handled in the parent webpage.), here is my codes.

WebUserControl.ascx
<%@ Control Language=”C#” AutoEventWireup=”true” CodeFile=”WebUserControl.ascx.cs” Inherits=”Dalegate_WebUserControl” %> 
 
<asp:Button ID=”btnTest” runat=”server” Text=”User Control” OnClick=”btnTest_Click” /> 
 
 

On WebUserControl.ascx.cs write the  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); 
 
            
    } 

Parent ASPX Page: Default.aspx

<%@ 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> 
         
        <uc1:WebUserControl ID=”WebUserControl1″ runat=”server” /> 
    
    </div> 
    </form> 
</body> 
</html> 

Default.aspx.cs
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(“Usercontrol click Event"); 
      }    

Let me know how it goes.

-shinu.



0
Mike
Top achievements
Rank 1
answered on 06 Aug 2009, 08:27 PM
Thanks for the reply.  I actually found another method that worked quite nicely.  In the code-behind of the page, I programatically create the reference to the 1st (and only) control inside the user control.

Example:

RadAjaxManager1.AjaxSettings.AddAjaxSetting(myUserControl.Controls[0], RadAjaxPanel1);

 

Tags
Ajax
Asked by
Mike
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Mike
Top achievements
Rank 1
Share this question
or