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

Find RadGrid in a PlaceHolder control

4 Answers 278 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeffrey
Top achievements
Rank 1
Jeffrey asked on 18 Nov 2010, 10:15 PM
Hi,

I created a RadGrid programmatically and added the RadGrid to a PlaceHolder control. The PlaceHolderControl is in a RadPageView.
In the code-behind, how can I programmatically find the RadGrid control when I click on a button?

I have tried the following code but to no avail.
 

 

 

protected void GetVotingsBtn_Click(object sender, EventArgs e)

 

{

 

 

  RadGrid vGrid = null;

 

 

 

 

  PlaceHolder GridPlaceHolder = (PlaceHolder)this.VotingRadPageView.FindControl("GridPlaceHolder");

 

 

 

  if (GridPlaceHolder != null) { vGrid = (RadGrid)this.GridPlaceHolder.FindControl("VRadGrid"); }

 

 

}

 


The value of the vGrid is always null. Please, help.

Thanks in advance.

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 19 Nov 2010, 07:52 AM
Hello Jeffery,

I believe your code will work if you use the exact ID of the control while accessing it. Have you missed setting the ID property for the grid when creating it?

C#:
protected void Page_Init(object source, System.EventArgs e)
   {
     RadGrid RadGrid1 = new RadGrid();
     RadGrid1.ID = "RadGrid1";
     RadGrid1.DataSourceID = "SqlDataSource1";
 
   }

And when accessing use the same ID as shown below:

C#:
protected void Button1_Click(object sender, EventArgs e)
   {
       RadGrid Grid1 = (RadGrid)PlaceHolder1.FindControl("RadGrid1");
   }

Could you paste the code that you tried for adding the grid control, if that is different from this?

Thanks,
Princy.
0
Jeffrey
Top achievements
Rank 1
answered on 19 Nov 2010, 01:03 PM
Hi Princy, thanks for helping out. I think the issue here might be coming from my form doing a postback. Let me explain further. So at the moment, I have a RadTabStrip Wizard with the following steps.
1) In the first RadPageView, I have two listbox. I populate the left listbox with items from the database. I then select items from the left listbox and drop them in the right listbox. The selected items in the right listbox will be used as columns when dynamically building my RadGrid.

2) In this step, I will click on a "Next" button and upon doing so, I call a "DefineGridStructure" method that dynamically builds my RadGrid and displays the Grid in the second RadPageView. Each column in this RadGrid contains RadNumericTextBox that I will need to find and obtain any data entered into them. Now, in this second RadPageView, I also have a button such that when I click on this button (GetValues), I want to be able to find controls in my dynamically built grid and obtain their values. So I think what happens here is that once I click on the "GetValues" button, there is a postback and the dynamically built Grid is gone.

So I am wondering if I build this Grid in a UserControl, then insert the UserControl in the PlaceHolder control found in the second RadPageView will help in anyway? Any other approach here will be helpful. By the way below is the code I use to build the Grid.

private

 

 

void DefineGridStructure()

 

 

 

{

 

 

RadGrid VotingRadGrid = new RadGrid();

 

VotingRadGrid.DataSourceID =

 

 

"ImpactCatDataSource";

 

VotingRadGrid.MasterTableView.DataKeyNames =

 

 

new string[] {"ImpactCategoryId"};

 

VotingRadGrid.Skin =

 

 

"Web20"; // "Office2007";

 

 

VotingRadGrid.GridLines =

 

GridLines.Both;

 

VotingRadGrid.ID =

 

 

"VRadGrid";

 

VotingRadGrid.AutoGenerateColumns = 

 

false;

 

 

 

 

/Add columns

 

 

 

 

 

 

this.AddImpactCategoryToGrid(VotingRadGrid);//Impact Category Column

 

 

 

 

this.AddRiskToGrid(VotingRadGrid);//Risk Columns

 

 

 

 

this.AddComponentToGridStructure(VotingRadGrid);//Add columns with RadNumericTextBox

 

 

 

 

this.GridPlaceHolder.Controls.Add(VotingRadGrid);

 

 

}

 

Please, let me know if you have any other questions.

Thanks
Jeff
0
Jeffrey
Top achievements
Rank 1
answered on 19 Nov 2010, 01:10 PM
Now, I am definitely sure that "Postback" is the issue here. I need to rebuild the grid each time I attempt to retrieve data from the grid. However, if I re-build the grid, I lose any data entered by a user. So I think either I find another design solution to build the grid at design time or I crack my brains to figure out how to retrieve user data entered in the dynamic grid.

Has anyone been able to find a solution on how to capture data in controls found in a dynamically created RadGrid or Datagrid?


Thanks in advance.
Jeff.
0
Jeffrey
Top achievements
Rank 1
answered on 23 Nov 2010, 05:40 PM
Folks, I finally figured out my issue. I should have build my dynamic RadGrid in the Page_Load Event. If do this, everthing works fine.

Merci!
Jeff
Tags
Grid
Asked by
Jeffrey
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jeffrey
Top achievements
Rank 1
Share this question
or