客户端模板按钮事件未触发

client template button event not firing

本文关键字:事件 按钮 客户端      更新时间:2023-09-26

当用户单击按钮时,我正在尝试显示一个弹出窗口,但我似乎无法让它工作。我可以在使用自定义命令时触发事件,但我需要在同一列中彼此相邻的文本框和按钮。

任何想法为什么这不起作用?

@(Html.Kendo().Grid(Model)
.Name("gridPopup")
.Columns(columns =>
{
    columns.Bound(p => p.ProductName).Width(120);
    columns.Template(@<text></text>).Title("").Width(110).ClientTemplate(
        Html.Kendo().TextBox()
        .Name("search_textfield")
        .Value("").ToClientTemplate().ToString()
        + " " +
        Html.Kendo().Button()
        .Name("search_button")
        .HtmlAttributes(new { type = "button", @class = "k-primary" })
        .Events(e =>
            e.Click("SearchButton")
        )
        .Content("...").ToClientTemplate().ToString()
    );
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Scrollable()
.HtmlAttributes(new { style = "height:320px;" })
.DataSource(dataSource => dataSource
    .Ajax()
    .Batch(true)
    .PageSize(20)
    .ServerOperation(false)
    .Events(events => events.Error("errorHandler"))
    .Model(model =>
    {
        model.Id(p => p.ProductID);
        model.Field(p => p.ProductID).Editable(true);
        model.Field(p => p.CategoryID).DefaultValue(1);
    })
    .Read(read => read.Action("ForeignKeyColumn_Read", "Home"))
    .Update(update => update.Action("ForeignKeyColumn_Update", "Home"))
    .Create(create => create.Action("ForeignKeyColumn_Create", "Home"))
    .Destroy(destroy => destroy.Action("ForeignKeyColumn_Destroy", "Home"))
)
)
<script type="text/javascript">

    function errorHandler(e) {
        if (e.errors) {
            var message = "Errors:'n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "'n";
                    });
                }
            });
            alert(message);
        }
    }
    function SearchButton(e)
    {
        alert("Search Button Clicked");
    }

</script>

奇怪! 如果在 ClientTemplate() 中使用,.Events(e => e.Click("SearchButton"))不会生成 onclick 属性

试试这个,它会为你工作,但我会等待有人解释它:)

.HtmlAttributes(new { type = "button", @class = "k-primary", onclick = "SearchButton()" })

编辑:我找到的解释在这里:如何在网格客户端列模板中使用剑道 UI 小部件?它说script tags are not automatically evaluated inside a Grid client column template, so any included widgets will not be initialized.

因此,初始化 .Events(e => e.DataBound("onDatabound")) 中的嵌套控件

function onDatabound()
{
    jQuery("#gridPopup tbody [class='k-primary']").each(function () {
        $(this).kendoButton({ "click": SearchButton });
    })
}