我动态生成了GridView
。它具有使用文本框模板创建的列。用于创建文本框模板的类如下:
public class GridViewTextboxTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnName;
private string dataType;
frmPracticalMarksEntry _PME;
public GridViewTextboxTemplate(DataControlRowType type,
string colname, string DataType, frmPracticalMarksEntry PME)
{
templateType = type;
columnName = colname;
dataType = DataType;
_PME = PME;
}
public void InstantiateIn(System.Web.UI.Control container)
{
DataControlFieldCell hc = null;
switch (templateType)
{
case DataControlRowType.Header:
// build the header for this column
Literal lc = new Literal();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
// build one row in this column
TextBox l = new TextBox();
// register an event handler to perform the data binding
l.DataBinding += new EventHandler(this.txt_DataBinding);
l.TextChanged += new EventHandler(this.txt_TextChanged);
l.AutoPostBack = true;
container.Controls.Add(l);
break;
default:
break;
}
}
private void txt_DataBinding(Object sender, EventArgs e)
{
// get the control that raised this event
TextBox l = (TextBox)sender;
// get the containing row
GridViewRow row = (GridViewRow)l.NamingContainer;
// get the raw data value and make it pretty
string RawValue = DataBinder.Eval(row.DataItem, columnName).ToString();
l.Text = RawValue;
}
private void txt_TextChanged(Object sender, EventArgs e)
{
_PME.lblEr.Text = (sender as TextBox).Text;
}
}
现在,只要文本框值发生变化,我就需要处理TextChanged
事件。
因此,我添加了事件处理程序txt_TextChanged
来处理文本更改事件。
如果我在回发期间发生don't rebind the GridView again in Page_Load
事件,则会发生doesn't retain its state
。
所以我需要再次绑定它。
现在,txt_TextChanged
事件将触发已更改值的每个文本框。但是应该仅针对其值刚刚更改的文本框触发。因此,它返回网格中最后一个非空白文本框的值。