GridView如何赋值,访问自定模板中的textbox控件的值
前台页面如下:<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ></asp:GridView>
后台代码如下:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using zp_Biz;
using System.Data;
public partial class DataGridTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BasicTableBiz btbiz = new BasicTableBiz();
//DataSet ds = btbiz.GetInterviewerByComIDAll("137");
DataSet ds = btbiz.GetInterviewerByComIDNoAccount("137");
DataSet ds1 = btbiz.GetInterviewerByRel_ID("86","0");
BoundField mCol_1 = new BoundField();
mCol_1.DataField = "";
mCol_1.HeaderText = "序号";
GridView1.Columns.Add(mCol_1);
BoundField mCol_2 = new BoundField();
mCol_2.DataField = "ub_name";
mCol_2.HeaderStyle.Width = 80;
mCol_2.HeaderText = "名称";
mCol_2.ControlStyle.Width=80;
GridView1.Columns.Add(mCol_2);
foreach (DataRow perRow in ds.Tables[0].Rows)
{
TemplateField tft = new TemplateField();
//表头信息
string studentName = perRow["ier_Name"].ToString();
tft.HeaderText = studentName;
MyTemplate mtt = new MyTemplate();
string str = "ierid" + btbiz.GetInerIDByNameComID(studentName, "137");
mtt.ProName = str;//数据源字段
tft.ItemTemplate = mtt;
this.GridView1.Columns.Add(tft);
}
GridView1.Visible = true; //是否隐藏
GridView1.DataSource = ds1.Tables["zp_ResumeAnalysis"];
GridView1.DataKeyNames = new string[] { "puser_id" };
GridView1.DataBind();
if (GridView1.Rows.Count <= 1)
{
return;
}
for (int i =1; i <5; i++)
{
TableCell oldtc = GridView1.Rows[0].Cells[i];
for (int j = 1; j < GridView1.Rows.Count; j++)
{
TableCell newtc = GridView1.Rows[j].Cells[i];
if (newtc.Text == oldtc.Text)
{
newtc.Visible = false;
if (oldtc.RowSpan == 0)
{
oldtc.RowSpan = 1;
oldtc.RowSpan = oldtc.RowSpan + 1;
oldtc.VerticalAlign = VerticalAlign.Middle;
}
else
{
oldtc = newtc;
}
}
}
}
}
public class MyTemplate : System.Web.UI.ITemplate
{
private string proName;
public MyTemplate()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public string ProName//要绑定的数据源字段名称
{
set { proName = value; }
get { return proName; }
}
public void InstantiateIn(Control container)//关键实现这个方法
{
TextBox hi = new TextBox();
hi.DataBinding += new EventHandler(hi_DataBinding);//创建数据绑定事件
container.Controls.Add(hi);
}
void hi_DataBinding(object sender, EventArgs e)
{
TextBox hi = (TextBox)sender;
GridViewRow container = (GridViewRow)hi.NamingContainer;
//关键位置
//使用DataBinder.Eval绑定数据
//ProName,MyTemplate的属性.在创建MyTemplate实例时,为此属性赋值(数据源字段)
hi.Attributes.Add("Text", "'<%#Eval(" + DataBinder.Eval(container.DataItem, ProName).ToString() + ") %>'");
}
}
}
就是赋值到自定义模板中,然后再取出来这个值来,就是搞不定
如GridView1.Rows[0].Cells[1].Control[0],就是不行,郁闷中,在线等在线结贴 --------------------编程问答-------------------- 建一个table,把textbox的值给table.然后绑定. --------------------编程问答--------------------
补充:.NET技术 , C#