Rad Button Inside a Rad Grid

2 posts, 0 answers
  1. Srikanth
    Srikanth avatar
    1 posts
    Member since:
    Dec 2017

    Posted 06 Sep 2018 Link to this post

    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; }

  2. Marin Bratanov
    Marin Bratanov avatar
    5910 posts

    Posted 10 Sep 2018 Link to this post

    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.

    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.
Back to Top