-1

My page has a comboBox which filters grid values. Im trying to disable grid's "add new record" button, when comboBox is empty, and enable the button when a value is selected and subconsequently, grid is loaded.

I have the following JavaScript function, which disables the button on pageLoad, but i cant enable the button later. What should i do?

function pageLoad() {
                       var grid = $find("<%=grid1.ClientID %>");
                       Button1 = $telerik.findControl(grid.get_element(), "AddNewRecordButton");
                       Button1.set_visible(false);
                   }

I tried to enable the button on the comboBox "SelectedChangeIndex", after trying in the PreRender method, with any results.

        if (radcombobox1.SelectedValue != null)
{
    GridCommandItem cmditem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
    Telerik.Web.UI.RadButton addbtn = (Telerik.Web.UI.RadButton)cmditem.FindControl("AddNewRecordButton");
    addbtn.Visible = true;
}

else
{
    // alert
} 
rlhf
  • 1
  • 5
  • Are you enabling the button in server-side code and then immediately disabling it in client-side code? Why two different approaches? Why not do both server-side or both client-side? – David Nov 10 '21 at 12:57
  • I would rather do it server side, but wasn't getting any results. In client side, i don't know how to do it, but if there's a good approach i try to follow it – rlhf Nov 10 '21 at 13:06
  • That depends on the overall UX that's happening here. What operations are causing a post-back? What operations *need* to cause a post-back? If *all you're doing* is "disabling" and "enabling" (either by actual disabling or hiding or some UI change of some kind) an element then that certainly can and should be entirely client-side. But if mixed in there you also have some operations that *require* a post-back then that would change things. – David Nov 10 '21 at 13:10
  • Yes, it is just enable/disable operations,, however the comboBox/grid loads need postbacks, so i really don't know what is the best way to manage the situation – rlhf Nov 10 '21 at 14:31

2 Answers2

0

I solved this by doing the work on the server-side, using the grid_ItemDataBound event, disabling the button:

if (RadComboBox1.SelectedItem == null) 
{

    GridCommandItem cmditem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
    Telerik.Web.UI.RadButton addbtn = (Telerik.Web.UI.RadButton)cmditem.FindControl("AddNewRecordButton");
    addbtn.Visible = false; //Enabled
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
rlhf
  • 1
  • 5
0

In your page load do you call the GridBind? After you call the Grid Bind do a RowCount check if its 0 then turn off the button. Don't put button the inside the Grid just add a button outside of it that way you can easily reference it to turn it off. Something like:

Grid.Bind();
if(Grid.Rows.Count == 0)
{
   buttonID.Visible = false;
}
else
{
   buttonID.Visible = true;
}
JobesK
  • 347
  • 1
  • 2
  • 6