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

[Solved] How can i access a RadGrid in code behind wehn I only know the ID

5 Answers 576 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Christian
Top achievements
Rank 1
Christian asked on 08 Apr 2009, 06:56 AM
Hello

how can i access a RadGrid in code behind when I only know the ID of the RadGrid?

thanks

Christian

5 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 08 Apr 2009, 07:10 AM
Hello Christian,

I'm not clear as to where you have placed your grid. But if you want to access the grid when you kow its ID then you can use the FindControl method:

C#:
protected void Page_Load(object sender, EventArgs e) 
    { 
        RadGrid grid = (RadGrid)Page.FindControl("RadGridID"); 
    } 

Thanks
Princy.
0
Christian
Top achievements
Rank 1
answered on 08 Apr 2009, 08:46 AM
I've tried this function but I get back grid = nothing ?!

Christian
0
Shinu
Top achievements
Rank 2
answered on 08 Apr 2009, 10:13 AM
Hi Christian,

The above given code will work fine if you have placed the Grid in a page with its ID as RadGrid1. Check whether you have placed the Grid normally in a page or in a usercontrol and also check whether you are accessing the Grid using its correct ID. If this does not help consider sending the entire aspx code.

Thanks
Shinu
0
Christian
Top achievements
Rank 1
answered on 15 Apr 2009, 07:18 AM
We use the RadGrid in a Usercontrol:



<

 

asp:HiddenField ID="ViewState_RG0" runat="server" Value="" />

 


    <div id="D_0" runat="server" class="" style="width: 110px; margin-top: 2px; overflow: hidden;  
        display: inline; text-align: right"> 
        <rc:HeaderContainer ID="D_01" runat="server" HeaderText=" " Width="100%" ControlsDir="~/rcControls/HeaderContainer">  
            <asp:PlaceHolder ID="PlaceHolder0" runat="server" /> 
        </rc:HeaderContainer> 
    </div> 

The RadGrid itsself is created with codr behind:
Protected Sub Page_Init(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Init  
    CreateGrid()  
End Sub 
 
Private Sub CreateGrid(ByVal Index As Integer)  
dim g as RadGrid  
' build RadGrid  
' do something amazing  
 
    Me.PlaceHolder0.Controls.Add(g)  
    Me.ViewState_RG0.Value = g.ClientID  
        RadGrid0_ClientID = g.ClientID  
End Sub 
 
Public Property RadGrid0_ClientID() As String 
        Get 
            Return CStr(Me.ViewState("RadGrid0_ClientID"))  
        End Get 
        Set(ByVal value As String)  
            Me.ViewState("RadGrid0_ClientID") = value  
        End Set 
End Property 

It doesn't work with:

g = new RadGrid();  
g = DirectCast(FindControl(RadGrid0_ClientID), com.regiocom.Web.UI.WebControls.TelerikGrid)  
g = DirectCast(FindControl(Me.ViewState_RG0.Value), com.regiocom.Web.UI.WebControls.TelerikGrid) 

Anny Idea?

Thanks

Christian
0
Shinu
Top achievements
Rank 2
answered on 15 Apr 2009, 11:13 AM
Hi Christian,

Since the RadGrid is inside the UserControl try accessing the Grid from the UserControl as shown below.

VB:
 
//try accessing the webusercontrol using the correct ID
Dim Grid As RadGrid = DirectCast(WebUserControl1.FindControl("RadGrid1"), RadGrid) 


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