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

时间:2016-02-01 21:32:06

标签: javascript c# asp.net telerik

我将onClick事件附加到RadGrid上的Add Record按钮。代码如下:

背后的代码

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()");
        }
}

的Javascript

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

1 个答案:

答案 0 :(得分:0)

发布答案,以便每个可能偶然发现的人:

代码背后:

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()");
    }
}

<强>的Javascript

function testClick(){
    // Perform some Client Side Validation
    var validated = false;

    if(!validated){
        radalert('Validation Fail', 100, 100, 'Window', null, null);
                return false;
    }
}