|
动态生成模板列:
/// <summary>
/// 创建TextBox类型的Grid列
/// </summary>
public class TemplateTextBox : ITemplate
{
public string ControlID = "", FieldName = "";
public TemplateTextBox()
{
}
public TemplateTextBox(string ControlID, string FieldName)
{
this.ControlID = ControlID;
this.FieldName = FieldName;
}
public void InstantiateIn(System.Web.UI.Control container)
{
AspNet.TextBox labGender = new AspNet.TextBox();
labGender.ID = this.ControlID;
labGender.BorderStyle = AspNet.BorderStyle.None;
labGender.DataBinding += new EventHandler(TemplateTextBox_DataBinding);
container.Controls.Add(labGender);
}
private void TemplateTextBox_DataBinding(object sender, EventArgs e)
{
AspNet.TextBox labGender = (AspNet.TextBox)sender;
IDataItemContainer dataItemContainer = (IDataItemContainer)labGender.NamingContainer;
labGender.Text = Convert.ToString(((DataRowView)dataItemContainer.DataItem)[this.FieldName]);
}
}
protected void Page_Init(object sender, EventArgs e)
{
tf = new FineUI.TemplateField();
tf.HeaderText = "角色名称";
tf.Width = Unit.Pixel(150);
tf.ItemTemplate = new FYMat.Interface.TemplateTextBox("RoleName", "RoleName");
Grid1.Columns.Add(tf);
Grid1.DataKeyNames = new string[] { "RoleName" };
}
在按钮中提取ROleName列的值,语句如下:
AspNet.TextBox TextBoxRoleName = (AspNet.TextBox)Grid1.Rows[i].FindControl("RoleName");
string RoleName = TextBoxRoleName.Text;
报错信息为:未将对象引用设置到对象的实例;
调试发现:TextBoxRoleName取出为null
怎么办呢?
|
|