页面加载时禁用 "Add New Record" 按钮

Disable "Add New Record" Button when page loads

我的页面有一个过滤网格值的组合框。我试图在 comboBox 为空时禁用网格的“添加新记录”按钮,并在选择值时启用该按钮,随后加载网格。

我有以下 JavaScript 功能,它禁用了 pageLoad 上的按钮,但我以后无法启用该按钮。我该怎么办?

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

我尝试在 PreRender 方法中尝试启用组合框“SelectedChangeIndex”上的按钮,但没有任何结果。

        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
} 

我通过在服务器端解决这个问题,使用 grid_ItemDataBound 事件,禁用按钮:

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
}

在您的页面加载中,您是否调用了 GridBind?调用 Grid Bind 后,执行 RowCount 检查是否为 0,然后关闭按钮。不要将按钮放在 Grid 内部,只需在其外部添加一个按钮,这样您就可以轻松引用它以将其关闭。类似于:

Grid.Bind();
if(Grid.Rows.Count == 0)
{
   buttonID.Visible = false;
}
else
{
   buttonID.Visible = true;
}