【发布时间】:2011-05-07 10:11:58
【问题描述】:
我有问题,我无法控制我在 DataGrid 中添加的内容。我将它添加到 OnRowDataBound 事件中,例如:
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
//int cindex = 0;
//for (cindex = 0; cindex < e.Row.Controls.Count; cindex++)
foreach (Control ctl in e.Row.Controls)
{
DataControlFieldCell dcctl = (DataControlFieldCell)ctl;
TableCell tcell = (TableCell)dcctl;
Label lblComment = new Label();
TextBox txtComment = new TextBox();
lblComment.Text = "<br>Comment: ";
dcctl.Controls.Add(lblComment);
dcctl.Controls.Add(txtComment);
//tcell.Controls.Add(lblComment);
//tcell.Controls.Add(txtComment);
//e.Row.Cells[cindex].Controls.Add(lblComment);
//e.Row.Cells[cindex].Controls.Add(txtComment);
这里发生了什么:默认情况下,TableCell 中已经存在一个 TextBox,我想再添加一个 TextBox 和 Label。边界后我可以看到 2 个文本框,我可以在两者中输入数据,但是当我单击更新按钮时,会引发 OnRowUpdating 事件,我无法获取我的文本框!
protected void RowUpdating(object sender, GridViewUpdateEventArgs e)
{
grdView.EditIndex = -1;
int counter = 0;
for (counter = 0; counter < grdView.Rows[e.RowIndex].Cells.Count; counter++)
{
foreach (Control ctl in grdView.Rows[e.RowIndex].Cells[counter].Controls)
{
在这里,我将只获得一个默认的 TextBox(及其值)。但是我的文本框消失了! :(
你能建议我在这里做什么?
附:我不能在 aspx 文件中使用预定义列,例如 asp:TemplateField,因为我的表每次都有不同数量的行。它是动态的
【问题讨论】: