I have a telerik RadGrid to which I am adding columns dynamically on click of a button. The Code is as follows :
private void AddColumns()
{
try
{
DataTable dtParMaintenanceListHeaderData = (DataTable)Session["ParMaintenanceListHeaderData"];
foreach (DataColumn dc in dtParMaintenanceListHeaderData.Columns)
{
dc.ReadOnly = false;
}
if (!dtParMaintenanceListHeaderData.Columns.Contains("parComboDataField"))
dtParMaintenanceListHeaderData.Columns.Add("parComboDataField", typeof(string));
dtParMaintenanceListHeaderData.AcceptChanges();
foreach (DataRow drEachRow in dtParMaintenanceListHeaderData.Rows)
{
string cusCombo = drEachRow["cusfstnam"].ToString() + " - " + drEachRow["cuslstnam"].ToString();
string parCombo = drEachRow["parcod"].ToString() + " - " + drEachRow["pardsc"].ToString();
drEachRow["parComboDataField"] = cusCombo + " " + parCombo;
GridTemplateColumn templateColumn = new GridTemplateColumn();
string templateColumnName = cusCombo + " - " + parCombo;
templateColumn.ItemTemplate = new RadGridTemplate(templateColumnName);
templateColumn.HeaderText = templateColumnName;
templateColumn.DataField = "parqty";
templateColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
templateColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
templateColumn.HeaderStyle.Width = Unit.Pixel(60);
templateColumn.HeaderStyle.Wrap = true;
this.radgridParListItems.MasterTableView.Columns.Add(templateColumn);
}
}
catch (Exception Ex)
{
}
}
The above method is called on click of a button.
In each o开发者_如何学编程f the columns added I am adding a textbox as follows :
#region Namespaces used
using System;
using System.Web;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
#endregion
/// <summary>
/// Summary description for RadGridTemplate
/// </summary>
public class RadGridTemplate : ITemplate
{
protected LiteralControl lControl;
protected TextBox textBox;
private string colname;
public RadGridTemplate(string cName)
{
colname = cName;
}
public void InstantiateIn(System.Web.UI.Control container)
{
textBox = new TextBox();
textBox.ID = container.ID + "txtParQty";
textBox.Width = Unit.Pixel(50);
container.Controls.Add(textBox);
textBox.Text = HttpContext.Current.Request.Form[textBox.UniqueID];
}
}
Now there is another button,"Save" which when clicked upon will have to fetch the data from all the textboxes in the dynamically added controls. The trouble is :
On post back all the controls are cleared and re-created so their values are null.
How do I capture the values of all the textboxes in a row.
If you instantiate the template columns on init as explained in the telerik docs here, you should be able to keep their state steady on posbacks and fetch the data from the textboxes looping through the grid rows.
I am adding the solution for the use of other users who are all facing same problem.
To avoid recreation of dynamic controls in radgrid we have to call the set EnableColumnsViewState="false" in MasterTableView of the grid. It worked for me.
Regards,
Nathiya Rajendran.
精彩评论