在GridView DevExpress中添加动作

时间:2016-01-01 18:35:50

标签: javascript c# asp.net gridview

我想添加一个动作来删除,编辑和添加按钮到我的函数javascript

这是我的GridView内容的代码,包括DeleteButton,EditButton和AddButton:

@Html.DevExpress().GridView(settings =>
       {
           settings.Name = "GridView";
           settings.KeyFieldName = "Id";
           settings.SettingsBehavior.AllowSelectByRowClick = true;
           settings.SettingsBehavior.AllowFocusedRow = true;

           settings.SettingsBehavior.AllowSelectSingleRowOnly = true;

           settings.ClientSideEvents.RowClick = "function(s, e){rowSelected(s, e)}";

           settings.Columns.Add("CodeClient");
           settings.Columns.Add("Nom");
           settings.Columns.Add("Prenom");
           settings.Columns.Add("DateNaissance");
           settings.CommandColumn.Visible = true;
           settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Client", Action = "" };
           settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Client", Action = "" };
           settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Client", Action = "" };
           // Show the command column, and enable the "Edit" and "Delete" buttons within the command column. 
           settings.CommandColumn.Visible = true;
           settings.CommandColumn.ShowEditButton = true;
           settings.CommandColumn.ShowDeleteButton = true;
           // Show the "New" button within the column header. 
           settings.CommandColumn.ShowNewButtonInHeader = true;
           // Change the "New" button settings. 

           ///DeleteButton.Image.Url = "~/content/delete.png";
           settings.SettingsCommandButton.NewButton.Image.IconID = IconID.ActionsAdditem16x16;
           settings.SettingsCommandButton.EditButton.Image.IconID = IconID.ActionsEditname16x16;
           settings.SettingsCommandButton.DeleteButton.Image.IconID = IconID.ActionsDelete16x16gray;





       }).Bind(Model).GetHtml()

这是我想在这三个按钮之一中的clic时执行的方法javascript

<script>
  function AjouterClient(s, e) {

        var _id = 0;
        $.ajax
            ({
                url: "/Client/ajouter",
                type: "POST",
                dataType: "text",
                success: function (response) {

                    location.replace("/Client/ajouter");

                }
            })
    }
    function ModifierClient(s, e) {
        debugger;
        var _id = idClient;
        $.ajax
            ({
                url: "/Client/ModifierClient?id=" + _id,
                type: "POST",
                dataType: "text",
                success: function (response) {

                    location.replace("/Client/ModifierClient?id=" + _id);

                }
            })
    }



    function Delete(s, e) {
        debugger;

        var _id = idClient;
        $.ajax
            ({
                url: "/Client/delete?id=" + _id,
                type: "POST",
                dataType: "text",
                success: function (response) {
                    debugger;
                    if (response == "True")
                        location.replace("/Client/Listeclients");
                    else
                        alert("ERROR");
                }
            })
    }



</script>

有人可以帮助将这三个按钮与这个javascript方法联系起来,谢谢。

1 个答案:

答案 0 :(得分:0)

要调用JS函数,您可以尝试这样的

settings.Columns.Add(column =>
    {
        column.Settings.AllowSort = DevExpress.Utils.DefaultBoolean.False;
        column.SetDataItemTemplateContent(c =>
        {               
            ViewContext.Writer.Write(string.Format("<button class=\"btn btn-primary btn-xs additem\" title=\"Add\" data-id=\"{0}\"><i class=\"fa fa-edit\"></i></button>", DataBinder.Eval(c.DataItem, "Id")));
            ViewContext.Writer.Write(string.Format("<button class=\"btn btn-danger btn-xs deleteitem\" title=\"Delete\" data-id=\"{0}\"><i class=\"fa fa-trash\"></i></button>", DataBinder.Eval(c.DataItem, "Id")));
            ViewContext.Writer.Write(string.Format("<button class=\"btn btn-primary btn-xs updateitem\" title=\"Update\" data-id=\"{0}\"><i class=\"fa fa-edit\"></i></button>", DataBinder.Eval(c.DataItem, "Id")));
        });
    });

页面加载

$(function () {

        $(document).delegate(".additem", "click", function () { AjouterClient($(this).data('id')); });
        $(document).delegate(".deleteitem", "click", function () { Delete($(this).data('id')); });
        $(document).delegate(".updateitem", "click", function () { ModifierClient($(this).data('id')); });
    });

你的js方法

    function AjouterClient(id){
        ...
    }
    function Delete(id){
        ...
    }
    function ModifierClient(id){
        ...
    }