Home > Software engineering >  Disable "Add New Record" Button when page loads
Disable "Add New Record" Button when page loads

Time:11-11

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
} 

CodePudding user response:

EDIT: SOLVED

I did it server side, using 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
        }

CodePudding user response:

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