欢迎光临
我们一直在努力

为DataGrid 写一个 DropDownListColumn-ASP教程,ASP应用

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

net下有一个类datagridcolumn

作为 datagrid 控件的不同列类型的基类。

有关此类型所有成员的列表,请参阅 datagridcolumn 成员。

system.object

system.web.ui.webcontrols.datagridcolumn

system.web.ui.webcontrols.boundcolumn

system.web.ui.webcontrols.buttoncolumn

system.web.ui.webcontrols.editcommandcolumn

system.web.ui.webcontrols.hyperlinkcolumn

system.web.ui.webcontrols.templatecolumn

我们完成可以写一个继承datagirdcolumn的类,这样就成了。dropdownlistcolumn,checkboxlistcolumn等。

控件的代码如下。

using system;

using system.web.ui;

using system.web.ui.webcontrols ;

using system.collections ;

using system.data ;

namespace customcolumn

{

/// <summary>

/// datagridcustomcolumn 的摘要说明。

/// </summary>

public class dropdownlistcolumn :system.web.ui.webcontrols.datagridcolumn

{

//数据源

public datatable datasource ;

//显示的文本字段

public string datatextfield ;

//

public string datavaluefield ;

//datagrid中要绑定的字段

public string datafield ;

public string datashowfield;

public dropdownlistcolumn()

{

//

// todo: 在此处添加构造函数逻辑

//

}

public override void initializecell(tablecell cell, int columnindex, listitemtype itemtype)

{

base.initializecell (cell, columnindex, itemtype);

switch(itemtype)

{

case listitemtype.header :

cell.text = this.headertext ;

break;

case listitemtype.item:case listitemtype.alternatingitem :

cell.databinding +=new eventhandler(cell_itemdatabinding);

break;

case listitemtype.edititem :

cell.databinding +=new eventhandler(cell_edititemdatabinding);

//========================

dropdownlist drp = new dropdownlist();

cell.controls.add(drp);

break;

}

}

private void cell_itemdatabinding(object sender, eventargs e)

{

tablecell cell =(tablecell)sender ;

datagriditem dgi =(datagriditem)cell.namingcontainer ;

try

{

cell.text = (databinder.eval(dgi.dataitem,datashowfield)).tostring();

}

catch

{

throw new exception("请设置字段");

}

}

private void cell_edititemdatabinding(object sender,eventargs e)

{

tablecell cell =(tablecell)sender ;

dropdownlist drp =(dropdownlist)cell.controls[0]; ;

listitem item ;

datagriditem dgi ;

try

{

//================

drp.items.add(new listitem("请选择","-1"));

//

for(int k=0;k<=datasource.rows.count -1;k++)

{

datarow dr = datasource.rows[k];

item = new listitem();

item.text = dr[datatextfield].tostring();

item.value = dr[datavaluefield].tostring();

drp.items.add(item);

}

dgi =(datagriditem)cell.namingcontainer ;

string value = databinder.eval(dgi.dataitem,datafield).tostring();

item = drp.items.findbyvalue(value);

if(item!=null)

{

item.selected = true;

}

}

catch

{

throw new exception("数据源不对。");

}

}

}

}

aspx页面调用。

先注册:

<%@ register tagprefix="drpcolumn" namespace="customcolumn" assembly="dropdownlistcolumn" %>

调用

<asp:datagrid id="dgteacher" runat="server" width="100%" autogeneratecolumns="false" allowpaging="true"

cssclass="datagrid" bordercolor="#333333" borderstyle="solid">

<columns>

<drpcolumn:dropdownlistcolumn headertext="导师"></drpcolumn:dropdownlistcolumn>

<asp:boundcolumn datafield="specname2" readonly="true" headertext="主修专业"></asp:boundcolumn>

<drpcolumn:dropdownlistcolumn datashowfield="specname" datafield="specguid" headertext="兼岗专业"></drpcolumn:dropdownlistcolumn>

<asp:boundcolumn datafield="createddt" readonly="true" headertext="创建日期" dataformatstring="{0:d}"></asp:boundcolumn>

<asp:boundcolumn datafield="createdby" readonly="true" headertext="创建者"></asp:boundcolumn>

<asp:boundcolumn datafield="srcip" readonly="true" headertext="来源ip"></asp:boundcolumn>

<asp:boundcolumn datafield="updatedby" readonly="true" headertext="修改者"></asp:boundcolumn>

<asp:editcommandcolumn buttontype="linkbutton" updatetext="更新" headertext="操作" canceltext="取消" edittext="编辑"></asp:editcommandcolumn>

<asp:buttoncolumn text="&lt;span onclick=return confirmdel();&gt;删除&lt;/span&gt;" headertext="操作"

commandname="delete"></asp:buttoncolumn>

</columns>

<pagerstyle mode="numericpages"></pagerstyle>

</asp:datagrid>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 为DataGrid 写一个 DropDownListColumn-ASP教程,ASP应用
分享到: 更多 (0)

相关推荐

  • 暂无文章