Hi,
I am adding a user control into a rad dock programatically on click of a button.
There is a Rad grid with in the user control that is created and added dynamically to the controls of Web User Control.
This grid does not show up in the Web User Control added to thedock.
As an alternative, i tried adding the grid in the design view and try to set the data source in the Page_Init function. When doing this, null reference exception comes up as the grid seems to be a null object.
Objective is to set different properties to the grid (in the web user control) based on the button click event and add that user control to the dock.
1 Answer, 1 is accepted
0
Hi Anitha,
You could try to invoke method LoadControl with parameters, e.g.:
ASPX:
Code-behind:
ASCX:
User Control code-behind:
All the best,
Petko
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
You could try to invoke method LoadControl with parameters, e.g.:
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> |
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
<!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:ScriptManager ID="ScriptManager1" runat = "server"></asp:ScriptManager> |
<asp:Button ID="Button1" runat = "Server" Text = "Load Grid with PARAMETER 1" OnClick = "Button1_Click" /> |
<asp:Button ID="Button2" runat = "Server" Text = "Load Grid with PARAMETER 2" OnClick = "Button2_Click" /> |
<telerik:RadDock ID="RadDock1" runat = "server" Top="100px" Left="100px" DefaultCommands="none"> |
</telerik:RadDock> |
</div> |
</form> |
</body> |
</html> |
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; |
using Telerik.Web.UI; |
using System.Reflection; |
using System.Configuration; |
using System.Collections.Generic; |
public partial class Default : System.Web.UI.Page |
{ |
protected void Button1_Click(object sender, EventArgs e) |
{ |
Control widget = LoadControl("Grid.ascx", 1); |
RadDock1.ContentContainer.Controls.Add(widget); |
} |
protected void Button2_Click(object sender, EventArgs e) |
{ |
Control widget = LoadControl("Grid.ascx", 2); |
RadDock1.ContentContainer.Controls.Add(widget); |
} |
private UserControl LoadControl(string UserControlPath, params object[] constructorParameters) |
{ |
List<Type> constParamTypes = new List<Type>(); |
foreach (object constParam in constructorParameters) |
{ |
constParamTypes.Add(constParam.GetType()); |
} |
UserControl ctl = Page.LoadControl(UserControlPath) as UserControl; |
// Find the relevant constructor |
ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray()); |
//And then call the relevant constructor |
if (constructor == null) |
{ |
throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString()); |
} |
else |
{ |
constructor.Invoke(ctl, constructorParameters); |
} |
// Finally return the fully initialized UC |
return ctl; |
} |
} |
ASCX:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Grid.ascx.cs" Inherits="WebUserControl" %> |
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
<telerik:radgrid id="radgrid1" runat="server"> |
</telerik:radgrid> |
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; |
using Telerik.Web.UI; |
public partial class WebUserControl : System.Web.UI.UserControl |
{ |
int currentIndex = 0; |
protected void Page_Load(object sender, EventArgs e) |
{ |
if (currentIndex == 1) |
{ |
gridBind(); |
} |
else if (currentIndex == 2) |
{ |
gridBind2(); |
} |
} |
public WebUserControl() |
{ |
} |
public WebUserControl(int passedIndex) |
{ |
currentIndex = passedIndex; |
} |
protected void gridBind() |
{ |
ArrayList source = new ArrayList(); |
source.Add("one"); |
source.Add("two"); |
source.Add("three"); |
radgrid1.DataSource = source; |
radgrid1.DataBind(); |
} |
protected void gridBind2() |
{ |
ArrayList source = new ArrayList(); |
source.Add("1"); |
source.Add("2"); |
source.Add("3"); |
radgrid1.DataSource = source; |
radgrid1.DataBind(); |
} |
} |
All the best,
Petko
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.