Answered by:
Can't apply Skin to Custom Control

Question
-
User-1716946620 posted
I have create a custom control that adds a checkbox to a gridview, but I can't apply my Skin to the control. I just get the error
Unknown server tag 'custom:CheckBoxGrid'.
My Skin File looks like this
<%@ Register TagPrefix="custom" Namespace="Application.Controls" %>
<custom:CheckBoxGrid
SkinId="Default"
runat="server"
CssClass="GridViewStyle">
<RowStyle CssClass="GridViewRowStyle" />
<FooterStyle CssClass="GridViewFooterStyle" />
<PagerStyle CssClass="GridViewPagerStyle" />
<SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
</custom:CheckBoxGrid>
This is my control's code.
using System;
using System.Collections;using System.ComponentModel;using System.Configuration;using System.Data;using System.Security.Permissions;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;[assembly: TagPrefix("Sage.Web.DeploymentManager.Controls", "asp")][assembly: WebResource("Sage.Web.DeploymentManager.CheckBoxGridJS.js", "text/javascript")]namespace Sage.Web.DeploymentManager.Controls{[Themeable(true)][AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), ToolboxData("<{0}:CheckBoxGrid runat=\"server\"></{0}:CheckBoxGrid>")]public partial class CheckBoxGrid : GridView{protected override void OnInit(EventArgs e){base.OnInit(e);this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "CheckBoxGridJS", Page.ClientScript.GetWebResourceUrl(this.GetType(), "Sage.Web.DeploymentManager.CheckBoxGridJS.js"));}protected override void OnRowCreated(GridViewRowEventArgs e){base.OnRowCreated(e);if (IncludeColumnCheckBox){if (e.Row.RowType == DataControlRowType.Header){((CheckBox)e.Row.FindControl("ChkHeader")).Attributes.Add("onclick", "ObjJs_" + this.ID + ".CheckUnCheckRows(this.checked)");}}}public void Rendercontrol(System.Web.UI.HtmlTextWriter writer){base.RenderControl(writer);ScriptManager.RegisterStartupScript(Page, this.GetType(), this.ID + "JSBlock", "var ObjJs_" + this.ID + " = new GridClass('" + this.ClientID + "');", true);}[Category("Behavior")][Themeable(true)][Description("Add checkbox column to the gridview.")][DefaultValue(false)]public bool IncludeColumnCheckBox{get{if (string.IsNullOrEmpty(ViewState["IncludeColumnCheckBox"].ToString())){return false;}else{return (bool)ViewState["IncludeColumnCheckBox"];}}set { ViewState["IncludeColumnCheckBox"] = value; }}[Browsable(false)]public string GetCheckedRows{get{string _collectionIds = string.Empty;if (DataKeys.Count > 0){for (int i = 0; i <= this.Rows.Count - 1; i++){if (Rows[i].RowType == DataControlRowType.DataRow){CheckBox oChk = (CheckBox)Rows[i].FindControl("ChkItem");if (oChk.Checked){if (_collectionIds == string.Empty)_collectionIds = (string)DataKeys[Rows[i].RowIndex].Value;else_collectionIds += "," + DataKeys[Rows[i].RowIndex].Value;}}}}return _collectionIds;}}protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource){ICollection columnList = base.CreateColumns(dataSource, useDataSource);if (!IncludeColumnCheckBox){return columnList;}ArrayList list = new ArrayList(columnList);MyTemplateField _CheckBoxColumn = new MyTemplateField();list.Insert(0, _CheckBoxColumn);return list;}sealed class MyHeaderTemplate : ITemplate{public void InstantiateIn(Control owner){CheckBox chkboxHeader = new CheckBox();chkboxHeader.ID = "ChkHeader";owner.Controls.Add(chkboxHeader);}}sealed class MyItemTemplate : ITemplate{public void InstantiateIn(Control owner){CheckBox chkboxItem = new CheckBox();chkboxItem.ID = "ChkItem";owner.Controls.Add(chkboxItem);}}class MyTemplateField : TemplateField{private TemplateOwner _owner;private MyHeaderTemplate _header;private MyItemTemplate _item;public MyTemplateField(){_owner = new TemplateOwner();_header = new MyHeaderTemplate();_item = new MyItemTemplate();_header.InstantiateIn(_owner);this.HeaderTemplate = _header;_item.InstantiateIn(_owner);this.ItemTemplate = _item;this.HeaderStyle.VerticalAlign = VerticalAlign.Middle;this.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;this.HeaderStyle.Width = Unit.Percentage(3);this.ItemStyle.VerticalAlign = VerticalAlign.Middle;this.ItemStyle.HorizontalAlign = HorizontalAlign.Center;}}[ToolboxItem(false)]public class TemplateOwner : WebControl{}}}using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Security.Permissions;
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;
[assembly: TagPrefix("Application.Controls", "asp")]
[assembly: WebResource("Application.CheckBoxGridJS.js", "text/javascript")]
namespace Sage.Web.DeploymentManager.Controls
{
[Themeable(true)]
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), ToolboxData("<{0}:CheckBoxGrid runat=\"server\"></{0}:CheckBoxGrid>")]
public partial class CheckBoxGrid : GridView
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "CheckBoxGridJS", Page.ClientScript.GetWebResourceUrl(this.GetType(), "Sage.Web.DeploymentManager.CheckBoxGridJS.js"));
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
base.OnRowCreated(e);
if (IncludeColumnCheckBox)
{
if (e.Row.RowType == DataControlRowType.Header)
{
((CheckBox)e.Row.FindControl("ChkHeader")).Attributes.Add("onclick", "ObjJs_" + this.ID + ".CheckUnCheckRows(this.checked)");
}
}
}
public void Rendercontrol(System.Web.UI.HtmlTextWriter writer)
{
base.RenderControl(writer);
ScriptManager.RegisterStartupScript(Page, this.GetType(), this.ID + "JSBlock", "var ObjJs_" + this.ID + " = new GridClass('" + this.ClientID + "');", true);
}
[Category("Behavior")]
[Themeable(true)]
[Description("Add checkbox column to the gridview.")]
[DefaultValue(false)]
public bool IncludeColumnCheckBox
{
get
{
if (string.IsNullOrEmpty(ViewState["IncludeColumnCheckBox"].ToString()))
{
return false;
}
else
{
return (bool)ViewState["IncludeColumnCheckBox"];
}
}
set { ViewState["IncludeColumnCheckBox"] = value; }
}
[Browsable(false)]
public string GetCheckedRows
{
get
{
string _collectionIds = string.Empty;
if (DataKeys.Count > 0)
{
for (int i = 0; i <= this.Rows.Count - 1; i++)
{
if (Rows[i].RowType == DataControlRowType.DataRow)
{
CheckBox oChk = (CheckBox)Rows[i].FindControl("ChkItem");
if (oChk.Checked)
{
if (_collectionIds == string.Empty)
_collectionIds = (string)DataKeys[Rows[i].RowIndex].Value;
else
_collectionIds += "," + DataKeys[Rows[i].RowIndex].Value;
}
}
}
}
return _collectionIds;
}
}
protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource)
{
ICollection columnList = base.CreateColumns(dataSource, useDataSource);
if (!IncludeColumnCheckBox)
{
return columnList;
}
ArrayList list = new ArrayList(columnList);
MyTemplateField _CheckBoxColumn = new MyTemplateField();
list.Insert(0, _CheckBoxColumn);
return list;
}
sealed class MyHeaderTemplate : ITemplate
{
public void InstantiateIn(Control owner)
{
CheckBox chkboxHeader = new CheckBox();
chkboxHeader.ID = "ChkHeader";
owner.Controls.Add(chkboxHeader);
}
}
sealed class MyItemTemplate : ITemplate
{
public void InstantiateIn(Control owner)
{
CheckBox chkboxItem = new CheckBox();
chkboxItem.ID = "ChkItem";
owner.Controls.Add(chkboxItem);
}
}
class MyTemplateField : TemplateField
{
private TemplateOwner _owner;
private MyHeaderTemplate _header;
private MyItemTemplate _item;
public MyTemplateField()
{
_owner = new TemplateOwner();
_header = new MyHeaderTemplate();
_item = new MyItemTemplate();
_header.InstantiateIn(_owner);
this.HeaderTemplate = _header;
_item.InstantiateIn(_owner);
this.ItemTemplate = _item;
this.HeaderStyle.VerticalAlign = VerticalAlign.Middle;
this.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
this.HeaderStyle.Width = Unit.Percentage(3);
this.ItemStyle.VerticalAlign = VerticalAlign.Middle;
this.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
}
}
[ToolboxItem(false)]
public class TemplateOwner : WebControl
{
}
}
}
I would grately appreciate any insight on to why this is not working.
Friday, August 7, 2009 1:25 PM
Answers
-
User-2106054853 posted
Hi,
Please refer to:
http://odetocode.com/Blogs/scott/archive/2005/09/01/2145.aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 12, 2009 4:51 AM
All replies
-
User-84650026 posted
add control information in Web.config.
<pages enableEventValidation="false"> <controls> <add tagPrefix="MyCustomControlPrefix" namespace="My.Controls" assembly="My.Controls"/> <add tagPrefix="rad" namespace="Telerik.WebControls" assembly="RadGrid.Net2"/> <add tagPrefix="rad" namespace="Telerik.WebControls" assembly="RadCalendar.Net2"/> <add tagPrefix="rad" namespace="Telerik.WebControls" assembly="RadInput.Net2"/> <add tagPrefix="rad" namespace="Telerik.WebControls" assembly="RadTreeView.Net2"/> <add tagPrefix="rad" namespace="Telerik.WebControls" assembly="RadMenu.Net2"/> <add tagPrefix="rad" namespace="Telerik.WebControls" assembly="RadWindow.Net2"/> <add tagPrefix="rad" namespace="Telerik.WebControls" assembly="RadSplitter.Net2"/> <add tagPrefix="rad" namespace="Telerik.WebControls" assembly="radtoolbar.Net2"/> </pages>
Like asp.net control has an <asp:Label format, the first line will be for your custom control. It will show up as <MyCustomControlPrefix:YourCustomControl
Friday, August 7, 2009 4:14 PM -
User-1675817941 posted
Hi,
this links may helps you
http://msdn.microsoft.com/en-us/library/dd185519.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.skinid.aspx
http://www.velocityreviews.com/forums/t104698-skin-and-custom-controls-asp-20.html (fourms)
http://dotnetslackers.com/ASP_NET/re-8377_Themes_for_Custom_Controls_in_ASP_NET.aspx
Thanks :)
Sunday, August 9, 2009 3:42 PM -
User-1716946620 posted
Thank you for the information I am no longer getting the error, skin file doesn't recognize custom section tag. But my control still doesn't have the theme applied to it. is there something in the web.config that I have to set globally, or possible a property I missed in the custom control?
Monday, August 10, 2009 9:51 AM -
User-2106054853 posted
Hi,
Please refer to:
http://odetocode.com/Blogs/scott/archive/2005/09/01/2145.aspx
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 12, 2009 4:51 AM