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

Disable RadButton with condition in Grid

1 Answer 501 Views
Grid
This is a migrated thread and some comments may be shown as answers.
rizky
Top achievements
Rank 1
rizky asked on 08 Dec 2014, 10:49 AM
Hello guys,

Can't anyone help me?, I'm new in ASP.NET and Telerik.

I do have problem, I can't disable the button in my grid.

I put RadButton in GridTemplateColumn in my grid. This is the hierarchy that I mean :

RadGrid
    MasterTableView
        Column
            GridTemplateColumn
                     ItemTemplate
                       --------->RadButton
                      /ItemTemplate
            /GridTemplateColumn

            GridBoundColumn
            /GridBoundColumn
        /Column

I use RadGrid_NeedDataSource for the data source. I want to disable RadButton if the value in other GridBoundColumn "yes", and if  the value is "No" RadButton will be able.

Do you get it what I meant?

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 10 Dec 2014, 01:05 PM
Hello Rizky,

For accomplishing the desired result you can handle the server-side OnItemDataBound event of the grid, retrieve the value that you will use for the condition and depending on the value, set the RadButton Enabled property to true or false.

Following is a simple example demonstrating such approach:
<telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
    <MasterTableView AutoGenerateColumns="false">
        <Columns>
            <telerik:GridBoundColumn DataField="ID"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="BoolValue"></telerik:GridBoundColumn>
            <telerik:GridTemplateColumn UniqueName="ButtonColumn1">
                <ItemTemplate>
                    <telerik:RadButton runat="server" ID="RadButton1" Text="Test button"></telerik:RadButton>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

And the code-behind:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("BoolValue", typeof(string));
    for (int i = 0; i < 5; i++)
    {
        table.Rows.Add(i, i % 2 == 0 ? "yes" : "no");
    }
 
    (sender as RadGrid).DataSource = table;
}
 
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        if (item["BoolValue"].Text == "yes")
        {
            (item["ButtonColumn1"].FindControl("RadButton1") as RadButton).Enabled = false;
        }
    }
}

Hope this helps.


Regards,
Konstantin Dikov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
rizky
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or