Question

I am dynamically creating a RadGrid and adding GridTemplateColumns to it. Those columns have textbox in them.
After binding datatable to the grid, after user makes changes to the textboxes and on clicking save button, I would like to access the textbox values. But I am stuck at getting hold of the textbox instance. I couldn't even get hold of GridItems!
To add more complexity, my RadGrid is in a UserControl, which is in a (multi)view.
Heres the code.

    protected void Page_Init(object sender, EventArgs e)
    {
        DefineGridStructure();            
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (RadGrid1 != null && RadGrid1.Items.Count > 0)
        {
            string strtxt = ((TextBox)RadGrid1.Items[1]["ProductGroup1"].Controls[0]).Text;//For starters, load one control and check it's state
        }
    }
    private void DefineGridStructure()
    {
        RadGrid1 = new RadGrid();
        RadGrid1.AutoGenerateColumns = false;
        RadGrid1.ShowHeader = true;
        RadGrid1.NeedDataSource += RadGrid1_NeedDataSource;
        foreach(GridColumn qtyColumn in BuildGridQtyColumns(PaxColumnCount))
        {
            RadGrid1.MasterTableView.Columns.Add(qtyColumn);
        }
        //Add grid to page
        phRadGrid.Controls.Add(RadGrid1);
    }
    private List<GridColumn> BuildGridQtyColumns(int count)
    {
        List<GridColumn> qtyColumns = new List<GridColumn>();
        for (int i = 1; i <= count; i++)
        {
            string qtyColumnName = string.Format("ProductGroup{0}", i);
            GridTemplateColumn qtyColumn = new GridTemplateColumn();
            qtyColumn.ItemTemplate = new GridNumberTemplate(qtyColumnName);//Creates a textbox control
            qtyColumn.UniqueName = qtyColumnName;
            qtyColumn.HeaderText = "Qty";
            qtyColumn.HeaderStyle.Width = Unit.Pixel(60);
            qtyColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;

            qtyColumns.Add(qtyColumn);
        }
        return qtyColumns;
    }

Since my control is in view, it's Page_Init is called more than once for each action that involves this view.

Was it helpful?

Solution

For a dynamically generated radgrid, it should be created in page_init method and viewstate for this grid will be restored for us automatically which we can get hold of in page_load method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top