我在asp.net中構建了一個主要的數據錄入應用程序。由於設計窗格從不準確地反映運行時的外觀,因此我在HTML視圖中完成了大部分工作。不幸的是,無休止地添加表格,tr的td標籤和文本框是非常乏味的,我想創建一些複合控件來爲我做很多這樣的工作。創建/呈現簡單複合控件的問題
我的工作的複合來產生一個div,表,tablerow的用4個細胞,作爲
a)細胞縮進下面,
b)中的細胞以含有標記
c)一種細胞包含一個文本框,&
d)包含一個星號標籤(用於指示強制項的細胞)
我的問題是,我不知道該如何讓各個組件正確呈現。
我的代碼如下:
public class ReadyTextBox : WebControl, INamingContainer
{
#region private
TextBox _txt = null;
BaseLabel _lbl = null;
Label _astrx = null;
int _indent = 0;
int _lblwidth = 0;
int _txtwidth = 0;
#endregion
public ReadyTextBox()
{
txt = new TextBox();
lbl = new BaseLabel();
_astrx = new Label();
_astrx.Text = "*";
_astrx.ForeColor = System.Drawing.Color.Red;
}
#region properties
public BaseLabel lbl {
set { _lbl = value; }
get { return _lbl; }
}
public TextBox txt {
set { _txt = value; }
get { return _txt; }
}
public string Caption {
set { _lbl.Text = value + ":"; }
get { return _lbl.Text; }
}
public int Indent {
set { _indent = value; }
get { return _indent; }
}
public int LabelWidth {
set { _lblwidth = value; }
get { return _lblwidth; }
}
public int TextWidth {
set { _txtwidth = value; }
get { return _txtwidth; }
}
public override bool Enabled {
set { if (!(_txt == null)) _txt.Enabled = value; }
get { if (!(_txt == null)) return _txt.Enabled; else return false; }
}
public string LabelClass {
set { if (!(_lbl == null)) _lbl.CssClass = value; }
}
public bool Mandatory {
set { if (!(_txt == null)) _astrx.Visible = value; }
}
public string TextClass {
set { if (!(_txt == null)) _txt.CssClass = value; }
}
public int TextLen {
set { if (!(_txt == null)) _txt.MaxLength = value; }
}
public TextBoxMode TextMode {
set { if (!(_txt == null)) _txt.TextMode = value; }
}
public int TextRows {
set { if (!(_txt == null)) _txt.Rows = value; }
}
#endregion
#region rendering
//build UI
protected override void CreateChildControls()
{
Controls.Clear();
HtmlGenericControl div = new HtmlGenericControl("div");
div.ID = "div_" + this.ID;
div.Attributes.Add("class", "clear");
Table tbl = new Table();
tbl.ApplyStyle(CreateControlStyle());
tbl.CellSpacing = 1;
tbl.CellPadding = 1;
div.Controls.Add(tbl);
TableRow r = new TableRow();
tbl.Rows.Add(r);
if (Indent == 0) Indent = 4;
if (LabelWidth == 0) LabelWidth = 30;
if (TextWidth == 0) TextWidth = 40;
if (Indent + LabelWidth + TextWidth > 99) throw new Exception("Component widths exceed 99%, for control: " + this.ID);
TableCell c = new TableCell();
c.Width = Unit.Percentage(Indent);
r.Cells.Add(c);
c = new TableCell();
c.Width = Unit.Percentage(LabelWidth);
r.Cells.Add(c);
c.Controls.Add(lbl);
c = new TableCell();
c.Width = Unit.Percentage(TextWidth);
r.Cells.Add(c);
c.Controls.Add(txt);
c = new TableCell();
c.Width = Unit.Percentage(100 - (Indent + LabelWidth + TextWidth));
r.Cells.Add(c);
c.Controls.Add(_astrx);
}
//render UI
protected override void Render(HtmlTextWriter writer)
{
//these statements all run, but by themselves, nothing is rendered.
base.EnsureChildControls();
PrepareForRender();
RenderContents(writer);
//this code doesnt work; for some reason controls.count is 0 at this point
//if (this.Controls.Count != 1) return;
//HtmlGenericControl div = (HtmlGenericControl)this.Controls[0];
//div.RenderControl(writer);
//Table t = (Table)div.Controls[0];
//t.RenderControl(writer);
//TableRow r = t.Rows[0];
//r.RenderControl(writer);
//TableCell c = r.Cells[0];
//c.RenderControl(writer);
//c = r.Cells[1];
//c.RenderControl(writer);
//Label l = (c.Controls[0] as Label);
//l.RenderControl(writer);
//c = r.Cells[2];
//c.RenderControl(writer);
//TextBox x = (TextBox)c.Controls[0];
//x.RenderControl(writer);
//c = r.Cells[3];
//c.RenderControl(writer);
//l = (c.Controls[0] as Label);
//l.RenderControl(writer);
//these statements work, but the controls are rendered
//without the indentation & width sizing intended.
lbl.RenderControl(writer);
txt.RenderControl(writer);
_astrx.RenderControl(writer);
}
protected virtual void PrepareForRender()
{
if (Controls.Count != 1) return;
HtmlGenericControl div = (HtmlGenericControl)Controls[0];
Table t = (Table)div.Controls[0];
t.CopyBaseAttributes(this);
if (ControlStyleCreated) t.ApplyStyle(ControlStyle);
TableRow r = t.Rows[0];
BaseLabel b = (r.Cells[1].Controls[0] as BaseLabel);
if (b != null) b.ForeColor = ForeColor;
TextBox tb = (r.Cells[2].Controls[0] as TextBox);
if (tb != null) b.ForeColor = System.Drawing.Color.Black;
Label l = (r.Cells[3].Controls[0] as Label);
if (l != null) l.ForeColor = System.Drawing.Color.Red;
}
#endregion
}
}
任何人都可以指出的問題,或者給我一個解決方案或解決方案片斷?我還沒有設法在網上找到一個有用的例子。 http://www.blog.ingenuitynow.net/Custom+Server+Controls+CreateChildControls+Or+Render.aspx
我已經改變了邏輯不創建HTML控件,然後嘗試呈現:
你能描述一下你的渲染問題嗎?現在發生了什麼,你不想發生? –