请问如何实现

ipodo 2007-04-25 03:59:13
我想把下面的aspx代码:

<asp:TemplateColumn HeaderText="钩选">
<ItemTemplate>
<asp:CheckBox id="Check" runat="server" />
<asp:Label id="DLID" runat="server" Visible=False Text='<%# DataBinder.Eval(Container.DataItem, "DLID") %>' />
</ItemTemplate>
</asp:TemplateColumn>
写到aspx.cs的void Page_Load函数下
protected System.Web.UI.WebControls.DataGrid MyList;
private void Page_Load(object sender, System.EventArgs e)
{

/*
System.Web.UI.WebControls.TemplateColumn TemplateColumnCheckbox = new TemplateColumn();
TemplateColumnCheckbox.HeaderText="钩选";
//TemplateColumnCheckbox.ItemTemplate=
System.Web.UI.ITemplate Itemplate = new ITemplate();
System.Web.UI.WebControls.Label LabelTxt =new Label();
CheckBox checkBox_check =new CheckBox();
checkBox_check.ID="Check";
LabelTxt.Text=DataBinder.Eval(Container.DataItem, "DLID");
TemplateColumnCheckbox.ItemTemplate.
MyList.Columns.AddAt(MyList.Columns.Count,TemplateColumnCheckbox);
*/
}
...全文
269 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ipodo 2007-04-26
  • 打赏
  • 举报
回复
能不能具体点
public class ColumnTemplate1 : ITemplate
{

public void InstantiateIn(Control container)
{
//add checkbox and label
}
public void OnDataBinding(object sender, EventArgs e)
{
//handle binding to DLID field
}
}
ipodo 2007-04-26
  • 打赏
  • 举报
回复
((DataRowView)container.DataItem)
行 78: ["DLID"].ToString();
总报错,高手在吗?
hqbj08 2007-04-26
  • 打赏
  • 举报
回复
只要调试成功马上给
hqbj08 2007-04-26
  • 打赏
  • 举报
回复
在调用实现ITemplate接口处错请高人帮我看看

TemplateColumn s=new TemplateColumn();
s.HeaderText="ID";
LabelTemplate s1 =new LabelTemplate ();
s.ItemTemplate= s1;
MyList.Columns.AddAt(MyList.Columns.Count,s);



public class LabelTemplate : ITemplate
{
// Instantiate the elements of the template in the given
// container. In this case, a DataGridItem element.
public void InstantiateIn(Control container)
{
container.Controls.Add(new LiteralControl("<b>"));
Label lblID = new Label();
lblID.DataBinding += new EventHandler(this.BindID);
container.Controls.Add(lblID);
container.Controls.Add(new LiteralControl("</b>, "));
}
// Handler of the OnDataBinding event for the Label element
// that renders the ID column in the template.
private void BindID(Object sender, EventArgs e)
{
Label l = (Label) sender;
DataGridItem container = (DataGridItem)
l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)["DLID"].ToString();
}
}

报错的信息
指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidCastException: 指定的转换无效。

源错误:


行 75: Label l = (Label) sender;
行 76: DataGridItem container = (DataGridItem) l.NamingContainer;
行 77: l.Text = ((DataRowView)container.DataItem)
行 78: ["DLID"].ToString();
行 79: }


源文件: c:\inetpub\wwwroot\webadex\logiclayer\business.cs 行: 77
zouqiang122 2007-04-26
  • 打赏
  • 举报
回复
up...
ipodo 2007-04-26
  • 打赏
  • 举报
回复
自己写的还请高手帮我看看
public class ColumnTemplate1 : WebControl,INamingContainer
{
Root theroot=new Root();
public String _field;
public String field
{
get { return _field;}
set { _field = value;}
}
public void InstantiateIn(Control container)
{
//add checkbox and label
CheckBox checkBox_check =new CheckBox();
checkBox_check.ID="Check";
container.Controls.Add(checkBox_check);
Label LabelTxt = new Label();
LabelTxt.DataBinding += new EventHandler(this.OnDataBinding);
LabelTxt.Visible= false;
container.Controls.Add(LabelTxt);
}
public void OnDataBinding(object sender, EventArgs e)
{
//handle binding to DLID field
Label LabelTxt = (Label) sender;//发送绑定请求
DataGridItem container = (DataGridItem) LabelTxt.NamingContainer;
LabelTxt.Text = theroot.getstring(((DataRowView)container.DataItem)[field].ToString(),50);//绑定字段
LabelTxt.ToolTip =((DataRowView)container.DataItem)[field].ToString();

}
}
ipodo 2007-04-25
  • 打赏
  • 举报
回复
我是想在aspx中只显示
<column>
</colum>
把这些写到.cs中

<asp:TemplateColumn HeaderText="钩选">
<ItemTemplate>
<asp:CheckBox id="Check" runat="server" />
<asp:Label id="DLID" runat="server" Visible=False Text='<%# DataBinder.Eval(Container.DataItem, "DLID") %>' />
</ItemTemplate>
</asp:TemplateColumn>
scow 2007-04-25
  • 打赏
  • 举报
回复
public class ColumnTemplate1 : ITemplate
{

public void InstantiateIn(Control container)
{
//add checkbox and label
}
public void OnDataBinding(object sender, EventArgs e)
{
//handle binding to DLID field
}
}
honkerhero 2007-04-25
  • 打赏
  • 举报
回复
使用DATAGRID的CoLUMNTemplate类和TemplateCOLUMN来做
zhqs1000 2007-04-25
  • 打赏
  • 举报
回复
要不在aspx.cs的void Page_Load函数下
写table
ufoteam 2007-04-25
  • 打赏
  • 举报
回复
??你想显示什么
ipodo 2007-04-25
  • 打赏
  • 举报
回复
请问如何实现

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧