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

Telerik Radgrid How to retrieve textbox values from dynamically generated RadGrid columns?

2 Answers 812 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sridhar
Top achievements
Rank 1
Sridhar asked on 08 Mar 2012, 11:04 PM
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.
Since my control is in view, it's Page_Init is called more than once for each action that involves this view.

Once the gridstructure is defined, it has no need to change, but I think I should be calling it on Page Init or Load every time..

I have column paging instead of row paging, so there is custom code to handle that.
I would like to get some advice on the best place to call NeedDataSource, DefineGridStructure and where to get Postback values.
I have read this article, but it has sparse information for my need. http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html
Heres my 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;
    }

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 09 Mar 2012, 05:47 AM
Hello,

Try the following code.
C#:
void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        TextBox txtbx = (TextBox)item.FindControl("TextBox1");
    }
}

-Shinu.
0
Sridhar
Top achievements
Rank 1
answered on 12 Mar 2012, 12:17 AM
My grid is data bound on every 'postback'.
ItemDataBound is called when the dataitem is bound to control.

I am looking for the event method that lets me get values in the grid's textboxes once viewstate is restored. It can be done in OnLoad. I think I am at the verge of finding a solution.
Tags
Grid
Asked by
Sridhar
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Sridhar
Top achievements
Rank 1
Share this question
or