This is a migrated thread and some comments may be shown as answers.

dynamically created TemplateColumns

3 Answers 81 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ras
Top achievements
Rank 1
ras asked on 10 Apr 2013, 11:09 AM
In my grid I have dynamically Created some Template Columns with TextBox as ItemTemplate control. My problem is that i couldnt
find that control When an external Save button is clicked. My code is here. Can anyone tell me what is the problem. Thanks in advance

  private void rgAddColumns()
        {
            int c = rgEmpOverTimeCalculation.Columns.Count;
 
            OvertimeTypeList objlist = new OvertimeTypeList();
            ListParams LP = new ListParams();
            LP.Add("@OvertimeTypeID", "%");
             
            LP.Add("Culture", SecurityUtility.Culture());
            objlist.GetList(LP);
 
            int TypeCount = objlist.Count;
            for (int k = 0; k < TypeCount; k++)
            {
                GridTemplateColumn Column = new GridTemplateColumn();
                
                Column.HeaderText = objlist[k].OvertimeType.ToUpper();
                Column.HeaderStyle.Font.Size =10;
                Column.HeaderStyle.ForeColor =System.Drawing.Color.Maroon;
                Column.HeaderStyle.Font.Bold = true;
                Column.UniqueName = objlist[k].OvertimeType.Replace(" ", String.Empty) + objlist[k].OvertimeTypeID;
                Column.Visible = true;
                 
                Column.ItemTemplate = new MyTemplate(Column.UniqueName);
 
                rgEmpOverTimeCalculation.MasterTableView.Columns.Add(Column);
            }
        }
 
 
public class MyTemplate : ITemplate
{
 
    protected RadNumericTextBox textBox;
    protected CheckBox boolValue;
    private string colname;
    public MyTemplate(string cName)
    {
        colname = cName;
    }
 
    
    public void InstantiateIn(System.Web.UI.Control container)
    {
 
        textBox = new RadNumericTextBox();
        textBox.ID = "txt"+colname;
        container.Controls.Add(textBox);
        
 
    }
 
}
   protected void rbtnSaveOvertime_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in rgEmpOverTimeCalculation.MasterTableView.Items)
            {
                RadNumericTextBox txtOvertimeInHrs = (RadNumericTextBox)item[rgEmpOverTimeCalculation.Columns[5].UniqueName].FindControl("txt" + rgEmpOverTimeCalculation.Columns[5].UniqueName);     //returns null
            }
        }

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 10 Apr 2013, 12:02 PM
Hi,

Try the following code.
c#:
protected void Button1_Click(object sender, EventArgs e)
 {
            foreach (GridDataItem item in RadGrid1.Items)
            {
                RadNumericTextBox txt = (RadNumericTextBox)item.FindControl("textBox");
             }
}

Thanks,
Shinu
0
ras
Top achievements
Rank 1
answered on 11 Apr 2013, 04:35 AM
Thanks Shinu. But It s not working..
0
Shinu
Top achievements
Rank 2
answered on 11 Apr 2013, 07:21 AM
Hi,

Here is the full code that I tried based on your scenario.
c#:
protected void Page_Init(object sender, System.EventArgs e)
{
      NewTemplateColumn templateColumn = new NewTemplateColumn();
        templateColumn.HeaderText = "sample";
   templateColumn.ItemTemplate = new TemplateColumn();
      RadGrid1.MasterTableView.Columns.Add(templateColumn);
}
public partial class NewTemplateColumn : GridTemplateColumn
    {
        public void InstantiateIn(Control container)
        {
         
        }
    }
    public partial class TemplateColumn : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            RadNumericTextBox txt;
            txt = new RadNumericTextBox();
            txt.ID = "txt1";
            container.Controls.Add(txt);
        }
 }
protected void Button1_Click(object sender, EventArgs e)
{
  foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
  {
     RadNumericTextBox txt = (RadNumericTextBox)item.FindControl("txt1");
  }
}

Thanks,
Shinu
Tags
Grid
Asked by
ras
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
ras
Top achievements
Rank 1
Share this question
or