T
Turgeson
thought this would be relatively simple but apparently it is not. I'm
trying to create a Custom Server Control class that will automatically
build a grid that conforms with a style consistent in my application. I
have no problems referencing the class in my pages. I can even step
through and check my property values Everything works fine until the
Render method is called. Then all derived properties are nulled.
// Control Class
using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
namespace customcontrols
{
/// <summary>
/// Summary description for XGrid.
/// </summary>
[toolboxdata("<{0}:xgrid runat=server></{0}:XGrid>")]
public class XGrid : System.Web.UI.WebControls.DataGrid
{
private string _sCaption = "";
private bool _bBuildScript = false;
private bool _bBandGrid = true;
private StringBuilder sbScript = new StringBuilder();
public string Caption
{
get
{
return _sCaption;
}
set
{
_sCaption = value;
_bBuildScript = true;
}
}
public bool BandGrid
{
get
{
return _bBandGrid;
}
set
{
_bBandGrid = value;
_bBuildScript = true;
}
}
private void BuildScript()
{
sbscript.append("<script>\n");
if(_sCaption.Length > 0)
{
// include a caption
sbScript.AppendFormat("sCap=document.getElementById('{0}').createCaption()\n"
+
"sCap.innerHTML = '{1}';\n", this.ID,
_sCaption);
}
if(_bBandGrid)
{
// band table
sbScript.AppendFormat("setTableBands('{0}');\n", this.ID);
}
sbscript.append("</script>");
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
base.Render(output);
if(_bBuildScript)
{
BuildScript();
base.Page.Response.Write(sbScript.ToString());
}
}
}
}
// Aspx
<%@ Page language="c#" Codebehind="frmCourseDetails.aspx.cs"
AutoEventWireup="false"
Inherits="sdb.student_information.frmCourseDetails" %>
<%@ Register TagPrefix="sdb" Namespace="sdb.usercontrols"
Assembly="sdb"%>
<html>
<body>
<form id=Form1 method=post runat="server">
<sdb:XGrid id=dgDetails style="LEFT: -1px; POSITION: absolute; TOP:
-1px" runat="server" Width="477px" AutoGenerateColumns="False"
ShowHeader="False" CellPadding="2" BorderWidth="0" BandGrid=true>
<Columns>
<asp:BoundColumn DataField="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="Internal"></asp:BoundColumn>
<asp:BoundColumn
DataField="Description"></asp:BoundColumn>
<asp:BoundColumn DataField="Max"
ItemStyle-HorizontalAlign=Center></asp:BoundColumn>
<asp:BoundColumn DataField="P/F"
ItemStyle-HorizontalAlign=Center></asp:BoundColumn>
<asp:BoundColumn DataField="Weight"
ItemStyle-HorizontalAlign=Right></asp:BoundColumn>
<asp:BoundColumn DataField="Order"
ItemStyle-HorizontalAlign=Center></asp:BoundColumn>
</Columns>
</sdb:wcSDBGrid>
</FORM>
</body>
</HTML>
Any ideas?? I've tried everything I can think of. The control will write
the javascript, but it acts like an empty Dataset (which it is not).
trying to create a Custom Server Control class that will automatically
build a grid that conforms with a style consistent in my application. I
have no problems referencing the class in my pages. I can even step
through and check my property values Everything works fine until the
Render method is called. Then all derived properties are nulled.
// Control Class
using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
namespace customcontrols
{
/// <summary>
/// Summary description for XGrid.
/// </summary>
[toolboxdata("<{0}:xgrid runat=server></{0}:XGrid>")]
public class XGrid : System.Web.UI.WebControls.DataGrid
{
private string _sCaption = "";
private bool _bBuildScript = false;
private bool _bBandGrid = true;
private StringBuilder sbScript = new StringBuilder();
public string Caption
{
get
{
return _sCaption;
}
set
{
_sCaption = value;
_bBuildScript = true;
}
}
public bool BandGrid
{
get
{
return _bBandGrid;
}
set
{
_bBandGrid = value;
_bBuildScript = true;
}
}
private void BuildScript()
{
sbscript.append("<script>\n");
if(_sCaption.Length > 0)
{
// include a caption
sbScript.AppendFormat("sCap=document.getElementById('{0}').createCaption()\n"
+
"sCap.innerHTML = '{1}';\n", this.ID,
_sCaption);
}
if(_bBandGrid)
{
// band table
sbScript.AppendFormat("setTableBands('{0}');\n", this.ID);
}
sbscript.append("</script>");
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
base.Render(output);
if(_bBuildScript)
{
BuildScript();
base.Page.Response.Write(sbScript.ToString());
}
}
}
}
// Aspx
<%@ Page language="c#" Codebehind="frmCourseDetails.aspx.cs"
AutoEventWireup="false"
Inherits="sdb.student_information.frmCourseDetails" %>
<%@ Register TagPrefix="sdb" Namespace="sdb.usercontrols"
Assembly="sdb"%>
<html>
<body>
<form id=Form1 method=post runat="server">
<sdb:XGrid id=dgDetails style="LEFT: -1px; POSITION: absolute; TOP:
-1px" runat="server" Width="477px" AutoGenerateColumns="False"
ShowHeader="False" CellPadding="2" BorderWidth="0" BandGrid=true>
<Columns>
<asp:BoundColumn DataField="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="Internal"></asp:BoundColumn>
<asp:BoundColumn
DataField="Description"></asp:BoundColumn>
<asp:BoundColumn DataField="Max"
ItemStyle-HorizontalAlign=Center></asp:BoundColumn>
<asp:BoundColumn DataField="P/F"
ItemStyle-HorizontalAlign=Center></asp:BoundColumn>
<asp:BoundColumn DataField="Weight"
ItemStyle-HorizontalAlign=Right></asp:BoundColumn>
<asp:BoundColumn DataField="Order"
ItemStyle-HorizontalAlign=Center></asp:BoundColumn>
</Columns>
</sdb:wcSDBGrid>
</FORM>
</body>
</HTML>
Any ideas?? I've tried everything I can think of. The control will write
the javascript, but it acts like an empty Dataset (which it is not).