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

Rad Button Inside a Rad Grid

1 Answer 45 Views
This is a migrated thread and some comments may be shown as answers.
Srikanth
Top achievements
Rank 1
Srikanth asked on 06 Sep 2018, 12:05 PM

I have a Telerik Grid in which I have declared Radbutton which is populated in itemdatabound event. I have disabled it with condition from database.

But When ever the page index is Changed at the same item index the button is disabled.

 

Please kindly suggest a better way to do so.

 

Here is the code inside the ItemDataBound event

 

if (e.Item is GridDataItem)
                {
                    GridDataItem dataItem = e.Item as GridDataItem;
                    if ((rddlLogLevel.SelectedValue).Contains("ERROR") == true)
                    {
                        DataSet ds = (DataSet)Session["LogErrors"];
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            if (ds.Tables[0].Rows[e.Item.ItemIndex]["PSRNumber"].ToString() == "" && ds.Tables[0].Rows[e.Item.ItemIndex]["LeretaPSRLogID"].ToString() == "")
                            { (dataItem["Action"].FindControl("rbtnCreatePSR") as RadButton).Enabled = true; }
                            else if (ds.Tables[0].Rows[e.Item.ItemIndex]["PSRNumber"].ToString() == "" && ds.Tables[0].Rows[e.Item.ItemIndex]["LeretaPSRLogID"].ToString() != "")
                            { (dataItem["Action"].FindControl("rbtnCreatePSR") as RadButton).Enabled = false; }
                            else { (dataItem["Action"].FindControl("hylnk_PSR") as HyperLink).Visible = true; }
                        }
                    }
                    else { (dataItem["Action"].FindControl("rbtnCreatePSR") as RadButton).Enabled = false; }

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 10 Sep 2018, 12:01 PM
Hi Srikanth,

When the page index changes the grid items are re-crated and re-bound to the new data. This means that the ItemCreted and ItemDataBound events will fire for each item. Since these are the same items in the grid, only their data changes, their indexes remain the same. So, such behavior would be expected.

Perhaps you can consider getting some flag from the bound row instead of its index: https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-raw-field-data-and-key-values.
For example, dataItem.GetDataKeyValue("someField") will give you an object that is that field's contents. Just make sure to add that field to the DataKeyValues collection of the MasterTableView.
Once you extract that flag from the row, you can replace e.Item.ItemIndex with it in the logic that disables/enables buttons so it only fires for the rows you want, not for all rows.


Regards,
Marin Bratanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Srikanth
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or