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

unable to access controls in programmatically created grid

4 Answers 142 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Maksim
Top achievements
Rank 1
Maksim asked on 28 Jan 2013, 03:22 PM
I have created grid programmatically using ITemplate following your tutorial http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html#Section4
Now, I keep getting message 'Object reference not set to an instance of an object' indicating that there is no such control in that item, when I try to access control within 'MyTemplate : ITemplate' in some method. 

For example:

var someString = ((LiteralControl)radGrid1.Items[index].FindControl("SomeControl")).Text;

Grid itself has correct number of items, but no programmatically created controls are there.
Please, help.

Thanks.

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 28 Jan 2013, 06:06 PM
Hello,

Try to access those control after ItemCreated and Page_Load Event.

http://www.telerik.com/help/aspnet-ajax/grid-event-sequence.html
http://www.telerik.com/help/aspnet-ajax/grid-control-lifecycle.html

Thanks,
Jayesh Goyani
0
Shinu
Top achievements
Rank 2
answered on 29 Jan 2013, 05:50 AM
Hi,

Try accessing the control as shown below.

C#:
protected void Page_Load(object sender, EventArgs e)
{
   //your code
 
   Radgrid1.ItemCommand += new GridCommandEventHandler(Radgrid1_ItemCommand);
    OR
   Radgrid1.ItemCreated += new GridItemEventHandler(Radgrid1_ItemCreated);
 
   //your code
}
void Radgrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
    if (e.Item is GridDataItem)
    {
        GridDataItem ditem = (GridDataItem)e.Item;
        //access the Literal control here
    }
}
 
  OR
 
void Radgrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem ditem = (GridDataItem)e.Item;
        //access the Literal control here
    }
}

Please elaborate your scenario if it doesn't help.

Thanks,
Shinu.
0
Maksim
Top achievements
Rank 1
answered on 29 Jan 2013, 11:33 AM
Hi Shinu,
Thanks for reply.
I have several buttons on the page that are supoused to process selected (sometimes not) items in the grid in different way.
I can find control in 'ItemDataBound' (forgot to mention that), but there is a lot of code separated among other 'ordinary' methods, handling button click events.
So, what I really need is to know if there is way to access item controls out there?
Everything was working fine while grid was hardcoded, but I had to make grid dynamic, and that's when the problem occured.

Thanks,
Maksim
0
Shinu
Top achievements
Rank 2
answered on 30 Jan 2013, 06:02 AM
Hi,

I guess you want to access the Controls in programmatically created RadGrid  on an external button click. Please check the following code snippet.

C#:
protected void button1_Click(object sender, EventArgs e)
{
    RadGrid radgrid = (RadGrid)PlaceHolder1.FindControl("RadGrid1");
    foreach (GridDataItem ditem in radgrid.Items)
    {
        //access the controls here..
    }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Maksim
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Maksim
Top achievements
Rank 1
Share this question
or