日期:2014-05-19  浏览次数:20976 次

请问如何实现
我想把下面的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);
*/
}

------解决方案--------------------
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
}
}
------解决方案--------------------
在调用实现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