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

How to find a control in radgrid hosted by radpanel

2 Answers 89 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bala
Top achievements
Rank 1
Bala asked on 30 Dec 2009, 08:55 PM
Hi experts,

we are facing challenges in converting an exisitng asp.net gridview to radgrid hosted by a radpanel.  below is the existing asp.net gridview code which we are trying to convert to radgrid.   Issue is we are not able to get the reference to the controls hosted by the radgrid for eg. Label)e.Row.FindControl("lblRole"), is always returning null. Please note that container for the radgrid is radpanel.  Tried looping the controls collection and finding the control by using ID (lblrole) but still noluck. any help is appreciated

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
        {  
            string strPrimary = string.Empty;  
            string sCopy = string.Empty;  
 
            if (e.Row.RowType == DataControlRowType.DataRow)  
            {  
                Label lblRole = (Label)e.Row.FindControl("lblRole");  
                RadioButtonList rbroles = (RadioButtonList)e.Row.FindControl("rbroles");  
                Label lbldelete = (Label)e.Row.FindControl("lbldelete");  
 
                rbroles.SelectedValue = lblRole.Text;  
                CheckBox chk = (CheckBox)e.Row.FindControl("chkUser1");  
 
                if (lbldelete != null)  
                {  
                    if (string.Compare(lbldelete.Text, "true", true) == 0)  
                    //if (lbldelete.Text.Equals("true",StringComparison.InvariantCultureIgnoreCase)  
                    {  
                        chk.Checked = true;  
                    }  
                }  
 
                if (hdnCurrenUserRole.Value.Length > 0)  
                {  
                    currentUserRole = Int32.Parse(hdnCurrenUserRole.Value);  
                }  
                if (currentUserRole == (int)Role.Owner || IsAdmin)  
                {  
                    rbroles.Enabled = true;  
                    chk.Enabled = true;  
                }  
                else  
                {  
                    rbroles.Enabled = false;  
                    chk.Enabled = false;  
                }  
            }  
        } 

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 31 Dec 2009, 05:04 AM
Hello Bala,

You can try out the following code while converting from GridView to RadGrid:
c#:
protected void RadGrid1_ItemDataBound(object sender, GridViewRowEventArgs e)   
        {   
            string strPrimary = string.Empty;   
            string sCopy = string.Empty;   
  
            if (e.Item is GridDataItem)   
            {   
                GridDataItem dataItem = (GridDataItem)e.Item; 
                Label lblRole = (Label)dataItem.FindControl("lblRole");   
                RadioButtonList rbroles = (RadioButtonList)dataItem.FindControl("rbroles");   
                Label lbldelete = (Label)dataItem.FindControl("lbldelete");   
  
                rbroles.SelectedValue = lblRole.Text;   
                CheckBox chk = (CheckBox)dataItem.FindControl("chkUser1");   
  
                if (lbldelete != null)   
                {   
                    if (string.Compare(lbldelete.Text, "true"true) == 0)   
                    //if (lbldelete.Text.Equals("true",StringComparison.InvariantCultureIgnoreCase)   
                    {   
                        chk.Checked = true;   
                    }   
                }   
  
                if (hdnCurrenUserRole.Value.Length > 0)   
                {   
                    currentUserRole = Int32.Parse(hdnCurrenUserRole.Value);   
                }   
                if (currentUserRole == (int)Role.Owner || IsAdmin)   
                {   
                    rbroles.Enabled = true;   
                    chk.Enabled = true;   
                }   
                else   
                {   
                    rbroles.Enabled = false;   
                    chk.Enabled = false;   
                }   
            }   
        }  

You can also refer to the following help document to understand better how to access rows and cells in RadGrid:
Accessing cells and rows

Thanks
Princy.
0
Sharada Murali
Top achievements
Rank 1
answered on 31 Dec 2009, 01:19 PM
Hi Princy,


I modified the code as you have suggested:

 

protected void radgrdUsers_ItemDataBound(object sender, GridItemEventArgs e)

 

{

 

string strPrimary = string.Empty;

 

 

string sCopy = string.Empty;

 

 

if(e.Item is GridDataItem)

 

{

 

GridDataItem dataItem = (GridDataItem)e.Item;

 

 

Label lblRole = (Label)dataItem.FindControl("lblRole");

 

 

RadioButtonList rbroles = (RadioButtonList)dataItem.FindControl("rbroles");

 

 

Label lbldelete = (Label)dataItem.FindControl("lbldelete");

 

rbroles.SelectedValue = lblRole.Text;

 

CheckBox chk = (CheckBox)dataItem.FindControl("chkUser1");

 

 

if (lbldelete != null)

 

{

 

if (string.Compare(lbldelete.Text, "true", true) == 0)

 

{

chk.Checked =

true;

 

}

}

 

//disable based on the roles

 

 

if (hdnCurrenUserRole.Value.Length > 0)

 

{

currentUserRole =

Int32.Parse(hdnCurrenUserRole.Value);

 

}

 

if (currentUserRole == (int)Role.Owner || IsRoadmapAdmin)

 

{

rbroles.Enabled =

true;

 

chk.Enabled =

true;

 

}

 

else

 

{

rbroles.Enabled =

false;

 

chk.Enabled =

false;

 

}

}

}

But still the value of the control is "null". 

I am copying the aspx code of this radgrid below:

 

 

<telerik:RadGrid ID="radgrdUsers" runat="server" CellPadding="3" AutoGenerateColumns="false"

 

 

 

 

 

 

AllowPaging="false" AllowSorting="false" Width="100%" OnNeedDataSource="radgrdUsers_NeedDataSource"

 

 

 

 

 

 

OnItemDataBound="radgrdUsers_ItemDataBound" >

 

 

<HeaderStyle CssClass="gridhs" />

 

 

<FooterStyle CssClass="gridfs" />

 

 

<PagerStyle HorizontalAlign="Right" />

 

 

<MasterTableView>

 

 

<Columns>

 

 

<telerik:GridBoundColumn HeaderText="Alias" UniqueName="Alias">

 

 

<ItemStyle Width="300px" />

 

 

<FilterTemplate>

 

 

<ui:BaseLabel runat="server" ID="lblname1" Text='<%#Eval("FullName") %>'></ui:BaseLabel><br />

 

 

<ui:BaseLabel runat="server" ID="lblalias1" Text='<%#Eval("DomainUserName") %>'></ui:BaseLabel><br />

 

 

<ui:BaseLabel runat="server" ID="lbltitle1" Text='<%#Eval("Title") %>'></ui:BaseLabel>

 

 

</FilterTemplate>

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn HeaderText="Role" UniqueName="Role">

 

 

<ItemStyle Width="150px" />

 

 

<FilterTemplate>

 

 

<asp:RadioButtonList ID="rbroles" runat="server" >

 

 

<asp:ListItem Value="2" >User</asp:ListItem>

 

 

<asp:ListItem Value="4">Owner</asp:ListItem>

 

 

</asp:RadioButtonList>

 

 

<ui:BaseLabel runat="server" ID="lblRole" Visible="false" Text='<%#Eval ("Role") %>' />

 

 

</FilterTemplate>

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn HeaderText="Delete" UniqueName="Delete">

 

 

<ItemStyle Width="50px" HorizontalAlign="Center" />

 

 

<FilterTemplate>

 

 

<ui:BaseCheckBox ID="chkUser1" runat="server" ToolTipId="DeleteUserHelp" />

 

 

<ui:BaseLabel runat="server" ID="lbldelete" Text='<%#Eval("Delete") %>' Visible="false" />

 

 

</FilterTemplate>

 

 

</telerik:GridBoundColumn>

 

 

</Columns>

 

 

</MasterTableView>

 

 

</telerik:RadGrid>

 

 

Can you suggest what could be the issue?

Thanks,
Sharada

Tags
Grid
Asked by
Bala
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Sharada Murali
Top achievements
Rank 1
Share this question
or