欢迎光临
我们一直在努力

全编辑WebGrid控件LrcGrid(6)——控件呈现-ASP教程,数据库相关

建站超值云服务器,限时71元/月

全编辑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++;

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 全编辑WebGrid控件LrcGrid(6)——控件呈现-ASP教程,数据库相关
分享到: 更多 (0)

相关推荐

  • 暂无文章