Telerik ASP.Net RadGrid 添加新记录客户端 JavaScript 防止行为

本文关键字:JavaScript 客户端 新记录 ASP Net RadGrid 添加 Telerik | 更新日期: 2023-09-27 18:32:52

我正在将onClick事件附加到RadGrid上的"添加记录"按钮。代码如下:

代码隐藏

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
        if (e.Item is GridCommandItem)
        {
                LinkButton lnk = (LinkButton)e.Item.FindControl("InitInsertButton");
                lnk.Attributes.Add("onClick", "testClick()");
        }
}

爪哇语

function testClick(){
    // Perform some Client Side Validation
    var validated = false;
    if(!validated){
        radalert('Validation Fail', 100, 100, 'Window', null, null);
        // What to call here to prevent the RadGrid from going into Insert (Edit) mode?? 
//I tried return false; but that did not prevent the grid.
    }
}

因此,在我的JS调用中,在显示警报窗口并关闭它之后,RadGrid仍然进入编辑模式。

我尝试了以下变体:

代码隐藏

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
        if(e.CommandName == RadGrid.InitInsertCommandName)
        {
                // If Validation Failed, prevent Radgrid from going to Edit Mode.
                RadGrid1..MasterTableView.ClearEditItems();
        }
}

Telerik ASP.Net RadGrid 添加新记录客户端 JavaScript 防止行为

发布一个答案,以便为每个可能偶然发现此内容的人:

代码隐藏:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridCommandItem)
    {
        LinkButton lnk = (LinkButton)e.Item.FindControl("InitInsertButton");
        lnk.Attributes.Add("onClick", "return testClick()");
    }
}

爪哇语

function testClick(){
    // Perform some Client Side Validation
    var validated = false;
    if(!validated){
        radalert('Validation Fail', 100, 100, 'Window', null, null);
                return false;
    }
}