![](/img/trans.png)
[英]Dynamically added controls in gridview (checkbox) disappear after postback in asp.net
[英]ASP.NET WebForms: Adding controls dynamically to a TableCell of a GridView and recovering them on the PostBack
我有一个绑定到DataSource
的GridView
。 它的最后一列具有文字控件,该控件用逗号分隔的数字字符串。 该字符串必须被拆分,并且这些数字必须在它们自己的TextBox
(我不知道事先有多少个):
这是最后一个列定义:
<asp:TemplateField HeaderText="Cantidades">
<ItemTemplate>
<asp:Literal ID="litCantidades" runat="server" Text='<%# Eval("CANTIDADES") %>'></asp:Literal>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
之后,在RowDataBound
事件中,我执行以下操作:
private void CreaControlesCeldaCantidades(GridViewRow gridRow)
{
//Array of numbers got from the bound literal control in the cell.
int[] cantidades = GetCantidadesFromRow(gridRow);
TableCell cantidadesCell = gridRow.Cells[9];
//I don't want the literal control anymore.
cantidadesCell.Controls.Clear();
int i = 0;
//I create one TextBox per each number to put it
//inside, so the user can edit it.
foreach (var cantidad in cantidades)
{
var cantidadTextBox = new TextBox();
cantidadTextBox.ID = "txtCantidad" + i++;
cantidadTextBox.Text = cantidad.ToString();
cantidadTextBox.Width = Unit.Pixel(10);
cantidadTextBox.CssClass = "txtCantidad";
cantidadesCell.Controls.Add(cantidadTextBox);
}
}
到现在为止还挺好。 问题出在PostBack
。 我尝试恢复用户编辑的数字,但是当我尝试恢复时,所有创建的TextBox
都消失了:
private void ValidarEntradasNumericas()
{
foreach (GridViewRow row in grvMaterialesTareas.Rows)
{
var chkSistematico = (CheckBox)row.FindControl("chkSistematico");
var txtSustitucion = (TextBox)row.FindControl("txtSustitucion");
var sustitucion = 0;
if (chkSistematico.Checked)
{
txtSustitucion.Text = "100";
}
else if (!int.TryParse(txtSustitucion.Text, out sustitucion))
{
throw new Exception("Uno de los códigos de sustitución indicados no es numérico. Por favor, corrija su valor.");
}
var cantidad = 0;
var cantidadesCell = row.Cells[9];
//Fails miserably and I don't know why :'(
foreach (TextBox txtCantidad in cantidadesCell.Controls)
{
if (!int.TryParse(txtCantidad.Text, out cantidad))
{
throw new Exception("Una de las cantidades indicadas no es numérica. Por favor, corrija su valor.");
}
}
}
}
我想知道为什么该解决方案不起作用以及如何使它起作用。
提前致谢 :)
动态创建的控件必须在每个Page Load上重新创建,并且其中包括PostBack。 因此,如果在IsPostBack
内具有GridView的绑定(通常需IsPostBack
),请将其删除。
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = source;
GridView1.DataBind();
}
其次,在GridView中不需要Literal即可在RowDataBound事件中获取数据
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView row = e.Row.DataItem as DataRowView;
TableCell cantidadesCell = e.Row.Cells[0];
if (e.Row.RowType == DataControlRowType.DataRow)
{
string[] cantidades = row["CANTIDADES"].ToString().Split(',');
for (int i = 0; i < cantidades.Length; i++)
{
var cantidadTextBox = new TextBox();
cantidadTextBox.ID = "txtCantidad" + i;
cantidadTextBox.Text = cantidades[i];
cantidadTextBox.Width = 50;
cantidadTextBox.CssClass = "txtCantidad";
cantidadesCell.Controls.Add(cantidadTextBox);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.