This approach is quite useful when you want to load grid instances with equal predefined settings on different pages in our web site.
For this purpose you can use XSL transformation to import the grid content into XML file (using StringWriter to transfer the data from the xsl definition to the .xml file). After this operation you can generate StringBuilder object and get the StringWriter instance content. Finally, simply call the Page.ParseControl(stringDefinition) method and add the control returned by that method to the Controls collection of a placeholder control. This placeholder control should reside in the webform on which you want to visualize the grid.
 |
Note: the steps described above should take place in the PageInit handler. |
| XSL |
Copy Code |
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:radG="remove">
<xsl:template match="/"> <div> <rad:RadGrid ID="RadGrid1" CssClass="RadGrid" DataSourceID="AccessDataSource1" Width="95%" EnableAJAX="True" AllowFilteringByColumn= "True" AllowSorting="True" PageSize="15" ShowFooter="True" ShowGroupPanel= "True" AllowPaging="True" AllowMultiRowSelection="True" runat="server"> <ClientSettings ReorderColumnsOnClient="True" AllowDragToGroup="True" AllowColumnsReorder="True"> <Scrolling AllowScroll="True" UseStaticHeaders="True" /> <Resizing AllowRowResize="True" AllowColumnResize="True" /> <Selecting AllowRowSelect="True" /> </ClientSettings> <GroupPanel> <PanelItemsStyle CssClass="GroupPanelItems" /> <PanelStyle CssClass="GroupPanel" /> </GroupPanel> <HeaderStyle CssClass="GridHeader" /> <FooterStyle CssClass="GridFooter" /> <GroupHeaderItemStyle CssClass="GroupHeader" /> <PagerStyle Mode="NumericPages" CssClass="GridPager" /> <ItemStyle CssClass="GridRow" /> <AlternatingItemStyle CssClass="GridRow" /> <SelectedItemStyle BackColor="#f8f6f0"/> <MasterTableView CssClass="MasterTable"> <RowIndicatorColumn> <ItemStyle CssClass="ResizeItem" /> </RowIndicatorColumn> </MasterTableView> </rad:RadGrid> </div> </xsl:template>
</xsl:stylesheet> |
| XML |
Copy Code |
|
<?xml version="1.0" encoding="utf-8" ?> <root> <foo/> </root> |
In the code-behind:
| C# |
Copy Code |
|
public void Page_Init(object sender, EventArgs e) { XPathDocument MyXPathDocument = new XPathDocument(Server.MapPath("XMLFile.xml"));
XslTransform MyXslTransform = new XslTransform(); MyXslTransform.Load(Server.MapPath( "XSLTFile.xsl"));
StringWriter MyStringWriter = new StringWriter(); MyXslTransform.Transform(MyXPathDocument, null, MyStringWriter);
StringBuilder MyStringBuilder = new StringBuilder();
MyStringBuilder.Append(@ "<%@ Register Assembly=""RadGrid.Net2"" Namespace=""Telerik.WebControls"" TagPrefix=""rad"" %>"); MyStringBuilder.Append(MyStringWriter.ToString()); MyStringBuilder = MyStringBuilder.Replace("xmlns:radG=\"remove\"", "");
Control ctrl = Page.ParseControl(MyStringBuilder.ToString());
PlaceHolder1.Controls.Add(ctrl); } |
| VB.NET |
Copy Code |
|
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Init Dim MyXPathDocument As XPathDocument = New XPathDocument(Server.MapPath("XMLFile.xml"))
Dim MyXslTransform As XslTransform = New XslTransform() MyXslTransform.Load(Server.MapPath( "XSLTFile.xsl"))
Dim MyStringWriter As StringWriter = New StringWriter() MyXslTransform.Transform(MyXPathDocument, Nothing, MyStringWriter)
Dim MyStringBuilder As StringBuilder = New StringBuilder()
MyStringBuilder.Append( "<%@ Register Assembly=""RadGrid.Net2"" Namespace=""Telerik.WebControls"" TagPrefix=""rad"" %>") MyStringBuilder.Append(MyStringWriter.ToString()) MyStringBuilder = MyStringBuilder.Replace("xmlns:radG=""remove""", "")
Dim ctrl As Control = Page.ParseControl(MyStringBuilder.ToString())
PlaceHolder1.Controls.Add(ctrl) End Sub |
You can see this example live here.