全编辑WebGrid控件LrcGrid(6)——控件呈现
创建子控件
重写CreateChildControls()过程,调用创建子控件的方法ReBuild()
每当 ASP.NET 页框架需要创建控件树时就会调用CreateChildControls()方法,且该方法调用并不限于控件生命周期的特定阶段。例如,可以在加载页时、在绑定数据过程中或者在呈现过程中调用CreateChildControls
protected override void CreateChildControls()
{
this.ReBuild();
}
重写OnPreRender(EventArgs e)过程,注册客户端脚本块,此脚本可使用户回车时进行焦点跳转,注释掉的部分为使用资源文件的方法.
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
if(this._isTabChg)
{
if(!Page.IsClientScriptBlockRegistered("lrc_chgTab"))
{
string str_chgTab = @"<script language='javascript' event='onkeydown' for='document'>
<!--
if(event.keyCode==13 && event.srcElement.type!='button' && event.srcElement.type!='submit' && event.srcElement.type!='reset' && event.srcElement.type!='')
event.keyCode=9;
-->
</script>";
Page.RegisterClientScriptBlock("lrc_chgTab",str_chgTab);
}
}
/*
*
ResourceManager manager = new ResourceManager(this.GetType());
ResourceSet resources = manager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture,true,true);
if(!Page.IsClientScriptBlockRegistered("LiuRuiCai.LrcGrid.Script"))
{
string script = resources.GetString("ClientScript");
this.Page.RegisterClientScriptBlock("LiuRuiCai.LrcGrid.Script",script);
string style = resources.GetString("ClientStyle");
this.Page.RegisterClientScriptBlock("LiuRuiCai.LrcGrid.Style",style);
}
*/
}
ReBuild()方法,当在运行时改变了控件属性,可调用此方法重新构造控件,它调用一系列的内部过程:管理视图状态,清空子控件,重新初始化数据集,创建标题行、创建数据行、创建操作行等:
/// <summary>
/// 重新构造控件
/// </summary>
public void ReBuild()
{
if(this.CurrentPageIndex == -1)
this.CurrentPageIndex = 0;
this.Controls.Clear();
this.ClearChildViewState();
this.Attributes.Add("isdel",this._isDel.ToString());
this.Attributes.Add("isadd",this._isAdd.ToString());
this.DbSet = null;
base.CreateChildControls ();
//创建标题行
buildTitle();
initDataSet();
if(this.DbSet != null)
{
if(this.DbSet.Tables.Count > 0)
{
if(this.DbSet.Tables[0].Rows.Count > 0)
{
//创建数据行
buildCol();
}
}
}
//创建操作行
buildOper();
}
创建标题行buildTitle()方法,标题行也就的表头,根据设定标题内容属性colsA,这里我把每列的表头文字设成可触发服务器端排序事件的LinkButton,并在每一个可编辑的列头添加一个HtmlAnchor ▼ ,这个HtmlAnchor 调用客户端脚本chgEdit()函数,(脚本内容请参见《全编辑WebGrid控件LrcGrid(4)—— 脚本库和样式表》)将表格中的一列在正常和编辑模式间切换,如果属性设置允许删除,将在标题行添加"全部删除"的LinkButton
表头效果如下图所示:
private void buildTitle()
{
TableRow tr =new TableRow();
tr.BackColor = _titColor;
TableCell tc = new TableCell();
LinkButton lk = new LinkButton();
HtmlAnchor lkE = new HtmlAnchor();
if(colsA != null)
{
for(int i=0;i<colsA.Length;i++)
{
tc = new TableCell();
//tc.BackColor = _titColor;
//排序
if(_isSort)
{
//全编辑
lk = new LinkButton();
lk.ID = "LrcLk_" + i;
lk.Text = colsA[i];
lk.Command += new CommandEventHandler(lk_Command);
lk.CommandArgument = colsA[i];
tc.Controls.Add(lk);
}
else
{
tc.Controls.Add(new LiteralControl(colsA[i]));
}
if(isEditCol(i))
{
lkE = new HtmlAnchor();
lkE.ID = "lrcLkE_" + i;
lkE.InnerText = "▼";
lkE.HRef = "javascript:chgEdit(" + i + "," + this.ClientID + ");";
tc.Controls.Add(lkE);
}
tr.Cells.Add(tc);
}
if(_isDel)
{
tc = new TableCell();
lk = new LinkButton();
lk.ID = "LrcDelAll";
lk.Text = "全部删除";
lk.Command += new CommandEventHandler(lkDel_Command);
tc.Controls.Add(lk);
tr.Cells.Add(tc);
}
this.Rows.Add(tr);
}
}
创建数据行buildCol()方法,该方法遍历数据集中表的所有项,将数据源中的记录和字段显示成表单的行和列,根据设定的属性,在表格项中添加不同的控件,并将数据源中的值绑定到这些控件
不可编辑列:直接将数据项值写入TableCell.Text
非外键的编辑列:添加文本框,并绑定数据项
是外键的编辑列:添加下拉列表,将下拉列表绑定为外键表,并根据数据项的值指定下拉列表的选择项.
根据属性设置,在最后一列添加"
删除"
LinkButton
如图所示:
private void buildCol()
{
for(int i=this.Rows.Count;i>1;i--)
{
this.Rows.RemoveAt(i-1);
}
TableRow tr;
TableCell tc;
TextBox tb;
int rowNum = 0;
foreach(DataRow dr in DbSet.Tables[0].Rows)
{
string forColor;
if(this.BackColor.IsKnownColor)
forColor = this.BackColor.Name;
else
forColor = "#" + this.BackColor.Name;
this.BackColor.B.ToString();
tr = new TableRow();
tr.Attributes.Add("onmouseover","this.bgColor='#C1D2EE'");
tr.Attributes.Add("onmouseout","this.bgColor='" + forColor + "'");
int rowNumData = rowNum + 1;
tr.Attributes.Add("ondblclick","chgEditRow(" + rowNumData.ToString() + "," + this.ClientID + ");");
for(int i=0;i<dr.ItemArray.Length;i++)
{
tc = new TableCell();
if(isEditCol(i))
{
if(isFkCol(i))
{
if(this.FkList(i,dr[i].ToString()) != null)
{
DropDownList drl = this.FkList(i,dr[i].ToString());
drl.ID = "LrcDrl_" + rowNum.ToString() + "_" + colsA[i];
drl.Enabled = false;
tc.Controls.Add(drl);
}
else
{
tc.Text = dr[i].ToString();
}
}
else
{
tb = new TextBox();
tb.ID = "LrcTb_" + rowNum.ToString() + "_" + colsA[i];
tb.Text = dr[i].ToString();
tb.Attributes.Add("onfocus","this.className=\"lrc_txt_edit\";this.parentNode.parentNode.bgColor='#C1D2EE';");
tb.Attributes.Add("onblur","this.className=\"lrc_txt_normal\";this.parentNode.parentNode.bgColor='#FFFFFF';");
//tb.Attributes.Add("ondblclick","this.className=\"lrc_txt_edit\";");
tb.Enabled = false;
//tb.Attributes.Add("style","border:0;background:url(images/txt_back.gif);width:100;height:20;");
tb.CssClass = "lrc_txt_hid";
tc.Controls.Add(tb);
}
}
else
{
tc = new TableCell();
tc.Text = dr[i].ToString();
}
tr.Cells.Add(tc);
}
if(_isDel)
{
tc = new TableCell();
LinkButton lkDelOne = new LinkButton();
lkDelOne.ID = "lkDel_" + rowNum;
lkDelOne.Text = "删除";
lkDelOne.CommandArgument = rowNum.ToString();
lkDelOne.Command += new CommandEventHandler(lkDelOne_Command);
tc.Controls.Add(lkDelOne);
tr.Cells.Add(tc);
}
base.Rows.Add(tr);
rowNum++;
}
}
本文地址:http://com.8s8s.com/it/it7418.htm