(5)重写CreateChildControls方法,该类在Control类上定义,此方法用于创建控件层次,以便为回发和呈现做准备。在该方法中调用了自定义CreateControlHierarchy方法以创建控件层次:
protected override void CreateChildControls()
{
base.CreateChildControls();
CreateControlHierarchy();
}
{
base.CreateChildControls();
CreateControlHierarchy();
}
(6)在CreateControlHierarchy方法中创建一个一行两列的表格,并分别调用CreateComment方法和CreateStarts方法创建注释和星形图按:
protected virtual void CreateControlHierarchy()
{
Table table = new Table();
TableRow row = new TableRow();
table.Rows.Add(row);
TableCell comment = new TableCell();
CreateComment(comment);
row.Cells.Add(comment);
TableCell stars = new TableCell();
CreateStars(stars);
row.Cells.Add(stars);
this.Controls.Add(table);
}
{
Table table = new Table();
TableRow row = new TableRow();
table.Rows.Add(row);
TableCell comment = new TableCell();
CreateComment(comment);
row.Cells.Add(comment);
TableCell stars = new TableCell();
CreateStars(stars);
row.Cells.Add(stars);
this.Controls.Add(table);
}
(7)实现CreateComment方法,该方法接收一个TableCell参数当做显示文本的容器,直接设置单元格的文本为注释字符串:
private void CreateComment(TableCell cell)
{
cell.Text = Comment;
}
{
cell.Text = Comment;
}
(8)实现CreateStarts方法,该方法亦接收一个TableCell当做参数,首先取得图片资源文件的路径,并在单元格里创建嵌套的层(在服务器端控件中Panel类与div相对应)并根据得分设设置其相应属性:
private void CreateStars(TableCell cell)
{
string starPath = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ControlLibrary.Image.stars.gif");
Panel panBg = new Panel();
panBg.Style.Add(HtmlTextWriterStyle.Width, "80px");
panBg.Style.Add(HtmlTextWriterStyle.Height, "16px");
panBg.Style.Add(HtmlTextWriterStyle.TextAlign, "left");
panBg.Style.Add(HtmlTextWriterStyle.Overflow, "hidden");
panBg.Style.Add(HtmlTextWriterStyle.BackgroundImage, starPath);
panBg.Style.Add("background-position", "0px -32px");
panBg.Style.Add("background-repeat", "repeat-x");
cell.Controls.Add(panBg);
Panel panCur = new Panel();
string width=Score*16+"px";
panCur.Style.Add(HtmlTextWriterStyle.Width, width);
panCur.Style.Add(HtmlTextWriterStyle.Height, "16px");
panCur.Style.Add(HtmlTextWriterStyle.BackgroundImage, starPath);
panCur.Style.Add("background-position", "0px 0px");
panCur.Style.Add("background-repeat", "repeat-x");
panBg.Controls.Add(panCur);
}
{
string starPath = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ControlLibrary.Image.stars.gif");
Panel panBg = new Panel();
panBg.Style.Add(HtmlTextWriterStyle.Width, "80px");
panBg.Style.Add(HtmlTextWriterStyle.Height, "16px");
panBg.Style.Add(HtmlTextWriterStyle.TextAlign, "left");
panBg.Style.Add(HtmlTextWriterStyle.Overflow, "hidden");
panBg.Style.Add(HtmlTextWriterStyle.BackgroundImage, starPath);
panBg.Style.Add("background-position", "0px -32px");
panBg.Style.Add("background-repeat", "repeat-x");
cell.Controls.Add(panBg);
Panel panCur = new Panel();
string width=Score*16+"px";
panCur.Style.Add(HtmlTextWriterStyle.Width, width);
panCur.Style.Add(HtmlTextWriterStyle.Height, "16px");
panCur.Style.Add(HtmlTextWriterStyle.BackgroundImage, starPath);
panCur.Style.Add("background-position", "0px 0px");
panCur.Style.Add("background-repeat", "repeat-x");
panBg.Controls.Add(panCur);
}