Hello, I have a control, which I will place sample code below, and aspx page using the control, ill post code there too.
Basically, My control works like a panel, and in most cases it works fine.
However, There is a case, where in the OnInit Method of the page, I am trying to attach an event to a button inside the template. And the runtime tells me that my button is null or not an instance.
I was hoping someone may have some thoughts.....
Control....
(asp.net forum kind of killed my render code.... I can send it to anyone through email if you need more info to help me out.
basically, My panel should kinf of cuntion like an Update Panel, where the Controls in the template are known to the aspx or ascx that is hosting my control.
ASPX Page Usage:
<gbmwc:gbmcontentpanel id="ClusterListPanel" runat="server" contentheight="550" panelimagesrc="/images/gbmpanel/Loading_sm.gif"title="The Clusters"><contenttemplate><p class="defaulttext-small">Select cluster from list below to view group/access. Use edit button
<img align="absMiddle"alt="" hspace="5" src="/images/grids/edit.gif">image to edit basic details.</p> <asp:button id="btnAdd" runat="server" text="Add New..." visible="<%# CanEdit %>" /><br />
</contenttemplate></gbmwc:gbmcontentpanel>
aspx.cs broken init method:
override protected void OnInit(EventArgs e){
base.OnInit(e); this.btnAdd.Click += new EventHandler(this.btnAdd_Click);}
The Control
// [ParseChildren(true)]// [PersistChildren(false)]// [ParseChildren(true)] public class GBMContentPanel : WebControl
{
private ITemplate _ContentTemplate;
private Panel contentPanel;
[TemplateInstance(TemplateInstance.Single)]
[TemplateContainer(typeof(System.Web.UI.TemplateControl))]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public ITemplate ContentTemplate
{
get
{
return _ContentTemplate;
}
set
{
_ContentTemplate = value;
}
}
private Unit _Height = new Unit(-1);
[PersistenceMode(PersistenceMode.Attribute)]
public Unit ContentHeight
{
get
{
return _Height;
}
set
{
_Height = value;
}
}
private string _title;
public string Title
{
get
{
return _title;
}
set
{
_title = value;
}
}
private string _PanelImageSRC = string.Empty;
public string PanelImageSRC
{
get
{
return _PanelImageSRC;
}
set
{
_PanelImageSRC = value;
}
}
private bool _showExpandImage = true;
public bool ShowExpandImage
{
get
{
return _showExpandImage;
}
set
{
_showExpandImage = value;
}
}
public bool ShowTitleBar
{
get
{
return true;
}
}
protected override void OnPreRender(EventArgs e)
{
if (this.ContentTemplate != null)
{
contentPanel = new Panel();
this.Controls.Add(contentPanel);
this.ContentTemplate.InstantiateIn(this.contentPanel);
}
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter writer)
{
string titleImage = "";
if (!string.IsNullOrEmpty(this.PanelImageSRC))
{
titleImage = string.Format("<img src='{0}' />", this.PanelImageSRC);
}
string sHeight = sHeight = String.Format(" height=\"{0}\"", this.ContentHeight);
string cid = this.ClientID;
writer.WriteLine("<div id=\"{0}\" class=\"transBackground\">", cid);
writer.WriteLine("<div id=\"{0}_Cover\" class=\"transOFF\">", cid);
writer.WriteLine("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\" bgcolor=\"#F6F6F6\" layoutType=\"Vertical\" onmouseover=\"__gbm_Flip(true, event)\" onmouseout=\"__gbm_Flip(false, event)\" onclick=\"__gbm_ClickAjaxToolbar(event)\">");
int ColCount = 0;
if (this.ShowTitleBar == true)
{
writer.WriteLine(" <tr>");
writer.WriteLine(" <td width=\"1\"><img src=\"/images/GBMPanel/TopLeft.gif\" /></td>");
writer.WriteLine(" <td width=\"3\"><img src=\"/images/GBMPanel/TopLeftInside.gif\" /></td>");
ColCount += 2;
if (this.PanelImageSRC != String.Empty)
{
writer.WriteLine("<td background=\"/images/GBMPanel/Top.gif\" width=\"20\"><img src=\"{0}\" /></td>",
this.PanelImageSRC);
ColCount++;
}
writer.WriteLine("<td background=\"/images/GBMPanel/Top.gif\" style=\"padding-left:5px;\" width=\"100%\"><span class=\"defaulttext-small\" style=\"color:#555555\">{0}</span></td>", this.Title);
ColCount++;
writer.WriteLine("<td width=\"13\"><img onclick=\"MinMaxGBMPanel('{0}', event);\" src=\"/images/GBMPanel/Minimize.gif\" class=\"imgBtn\" title=\"Minimize this panel\" /></td>", cid);
ColCount++;
writer.WriteLine("<td width=\"3\"><img src=\"/images/GBMPanel/TopRightInside.gif\" /></td>");
writer.WriteLine("<td width=\"2\"><img src=\"/images/GBMPanel/TopRight.gif\" /></td>");
ColCount += 2;
}
else
{
writer.WriteLine("<tr height=\"2\">");
writer.WriteLine("<td width=\"1\"><img src=\"/images/GBMPanel/TopLeftNoTitle.gif\" /></td>");
writer.WriteLine("<td background=\"/images/GBMPanel/TopNoTitle.gif\" ></td>");
writer.WriteLine("<td width=\"2\"><img src=\"/images/GBMPanel/TopRightNoTitle.gif\" /></td>");
ColCount += 3;
}
writer.WriteLine(" </tr>");
writer.WriteLine(" <tr id=\"{0}_trContent\">", cid);
writer.WriteLine(" <td background=\"/images/GBMPanel/left.gif\"></td>");
if (sHeight == String.Empty)
{
writer.WriteLine("<td colspan=\"{0}\"><div constrainHeight=\"0\"><div>", ColCount - 2);
}
else
{
writer.WriteLine("<td colspan=\"{0}\"><div id=\"{2}_divContentHolder\" style=\"height:{1};overflow:auto;border:0px;position:relative;width:inherit;margin:2px;\"><div>", ColCount - 2, this.ContentHeight, cid);
}
this.contentPanel.RenderControl(writer);
writer.WriteLine("</div></div></td>");
writer.WriteLine(" <td background=\"/images/GBMPanel/right.gif\"></td>");
writer.WriteLine(" </tr>");
writer.WriteLine(" <tr height=\"2\">");
writer.WriteLine(" <td><img src=\"/images/GBMPanel/BottomLeft.gif\" /></td>");
writer.WriteLine(" <td background=\"/images/GBMPanel/Bottom.gif\" colspan=\"{0}\"></td>", ColCount - 2);
writer.WriteLine(" <td><img src=\"/images/GBMPanel/BottomRight.gif\" /></td>");
writer.WriteLine(" </tr>");
writer.WriteLine("</table>");
writer.WriteLine("</div>");
writer.WriteLine("</div>");
// this.contentPanel.RenderControl(writer);
// base.Render(writer);
}
}