在使用ASP.net的DataGrid控件时,跟本无法更新数据项.
代码如下:
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
tb=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
DataGrid1.EditItemIndex=e.Item.ItemIndex;
DataGrid1.DataBind();
Label2.Text=tb;
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string ta=((TextBox)e.Item.Cells[3].Controls[0]).Text; //不能得到输入后的数据
Label1.Text=ta;
a.UpdateNews(tb,ta);
DataGrid1.DataSource=a.NewsItem();
DataGrid1.EditItemIndex=-1;
DataGrid1.DataBind();
}表现:
在取得修改状态时的TextBox中的键入记录的操作是无效的,ta还是为以前状态的值,无法达到更新的目的.
代码如下:
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
tb=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
DataGrid1.EditItemIndex=e.Item.ItemIndex;
DataGrid1.DataBind();
Label2.Text=tb;
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string ta=((TextBox)e.Item.Cells[3].Controls[0]).Text; //不能得到输入后的数据
Label1.Text=ta;
a.UpdateNews(tb,ta);
DataGrid1.DataSource=a.NewsItem();
DataGrid1.EditItemIndex=-1;
DataGrid1.DataBind();
}表现:
在取得修改状态时的TextBox中的键入记录的操作是无效的,ta还是为以前状态的值,无法达到更新的目的.
这句改成:
TextBox tb = (TextBox)e.Item.Cells[3].FindControl("NewName");
string ta;
if(tb != null)
ta = tb.Text;
if(!Page.IsPostBack)
{
//初次绑定你的控件
}
这句改成:
TextBox tb = (TextBox)e.Item.FindControl("NewName");
string ta;
if(tb != null)
ta = tb.Text;
这句话找不到文本框假如没有那个条件判断
运行到string t=tb.text时就出错了.
//以下是类成员
//string strTmp=string.Empty;
void ListCtrl(Control[] ctrls)
{
strTmp+=" ";
Response.Write
foreach(Control ctrl in ctrls)
{
Response.Write(string.Formate("{0}Name:{1},Type:{2}<br>",strTmp,ctrl.Name,ctrl.Type));
if(ctrl.Controls.Length>0)
ListCtrl(ctrl.Controls);
}
strTmp=strTmp.Substring(12);
}
我同事也碰到过这问题
好几个控件都可以取到值 但是其中一个就是拿不到值
控件是可以找到
就是得不到新值
估计是页面状态管理出了些问题你在程序中断 检查下e.Item.Cells[3].Controls.Count
再一个个试((TextBox)e.Item.Cells[3].Controls[0 -- Count-1]).Text
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox txtResult;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.button3 = new System.Windows.Forms.Button();
this.textBox3 = new System.Windows.Forms.TextBox();
this.txtResult = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.panel1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 24);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(144, 24);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "checkBox1";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Location = new System.Drawing.Point(24, 80);
this.groupBox1.Name = "groupBox1";
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 24);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(112, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// panel1
//
this.panel1.Controls.Add(this.groupBox2);
this.panel1.Location = new System.Drawing.Point(32, 208);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(648, 208);
this.panel1.TabIndex = 3;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.groupBox3);
this.groupBox2.Controls.Add(this.button2);
this.groupBox2.Controls.Add(this.textBox2);
this.groupBox2.Location = new System.Drawing.Point(16, 16);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(552, 128);
this.groupBox2.TabIndex = 3;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "groupBox2";
//
// button2
//
this.button2.Location = new System.Drawing.Point(112, 48);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "button1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(24, 24);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 0;
this.textBox2.Text = "textBox1";
//
// groupBox3
//
this.groupBox3.Controls.Add(this.button3);
this.groupBox3.Controls.Add(this.textBox3);
this.groupBox3.Location = new System.Drawing.Point(240, 16);
this.groupBox3.Name = "groupBox3";
this.groupBox3.TabIndex = 3;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "groupBox3";
//
// button3
//
this.button3.Location = new System.Drawing.Point(112, 48);
this.button3.Name = "button3";
this.button3.TabIndex = 1;
this.button3.Text = "button1";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(24, 24);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 0;
this.textBox3.Text = "textBox1";
//
// txtResult
//
this.txtResult.Location = new System.Drawing.Point(256, 24);
this.txtResult.Multiline = true;
this.txtResult.Name = "txtResult";
this.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtResult.Size = new System.Drawing.Size(400, 152);
this.txtResult.TabIndex = 4;
this.txtResult.Text = "";
this.txtResult.WordWrap = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(696, 461);
this.Controls.Add(this.txtResult);
this.Controls.Add(this.panel1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.panel1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
this.txtResult.Text="";
this.ListCtrl(this.Controls);
}
string strTmp=string.Empty; void ListCtrl(Control.ControlCollection ctrls)
{
strTmp+="\t";
foreach(Control ctrl in ctrls)
{
this.txtResult.AppendText(string.Format("{0}Name:{1},Type:{2}\r\n",strTmp,ctrl.Name,ctrl.GetType()));
if(ctrl.Controls.Count>0)
ListCtrl(ctrl.Controls);
}
strTmp=strTmp.Substring(1);
}
}
}
((TextBox)e.Item.Cells[1].FindControl("TextBox1")).Text="aaaa";
title=((TextBox)e.Item.Cells[1].FindControl("TextBox1")).Text;能把"aaaa"写入到数据库,但是把第一句注释了,是无伦如何也得不到文本框里面的数据