Jesun Bicar
Jesun Bicar

Reputation: 47

DevExpress MVC Gridview

Good day guys I'm trying to add a extra column on the grid view with this code. But when i run it. I cant click the button on the grid view. Got any idea what i'm doing wrong. Thanks

        settings.Columns.Add(column =>
        {
            column.FieldName = "Unbound";
            column.Caption = "Action";
            column.UnboundType = DevExpress.Data.UnboundColumnType.Object
            column.EditFormSettings.Visible = DevExpress.Utils.DefaultBoolean.True;
            column.ReadOnly = false;
            column.SetDataItemTemplateContent((c) =>
            {

                Html.DevExpress().Button(b =>
                {
                    b.Name = "btnVE" + c.KeyValue;
                    b.Text = "V/E";
                    b.ClientSideEvents.Click =
                    "function(s, e) { document.location='" + DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" })
                    + "?key=' + s.GetRowKey(e.visibleIndex); }";

                }).GetHtml();
            });
        });

        settings.Columns.Add("Id");
        settings.Columns.Add("Code");
        settings.Columns.Add("CompanyId");
        settings.Columns.Add("Description");
        settings.Columns.Add("ContactPerson");
        settings.Columns.Add("TelNo");
        settings.Columns.Add("Notes");

Update: i found the error on web via web developer tools but i dont know how to fix it Error button hover

Upvotes: 2

Views: 1581

Answers (2)

Jesun Bicar
Jesun Bicar

Reputation: 47

Found the problem. Apparently this should be place on the front of the view before the creation of the grid view. But i was thinking this should be a JScript. It should run asynchronous. Ohh well as long as it works. Thanks for the help @tetsuya

 @Html.DevExpress().GetStyleSheets(
        new StyleSheet { ExtensionSuite = ExtensionSuite.NavigationAndLayout },
        new StyleSheet { ExtensionSuite = ExtensionSuite.Editors, ExtensionType = ExtensionType.Button },
        new StyleSheet { ExtensionSuite = ExtensionSuite.GridView }

    )
    @Html.DevExpress().GetScripts(
        new Script { ExtensionSuite = ExtensionSuite.NavigationAndLayout },
        new Script { ExtensionSuite = ExtensionSuite.Editors, ExtensionType = ExtensionType.Button },
        new Script { ExtensionSuite = ExtensionSuite.GridView }
    )

Upvotes: 0

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

Sounds like the problem is originated from s which assigned to button sender on this block instead of GridView row:

Html.DevExpress().Button(b =>
{
    b.Name = "btnVE" + c.KeyValue;
    b.Text = "V/E";
    b.ClientSideEvents.Click =
    "function(s, e) { document.location='" + DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" })
    + "?key=' + s.GetRowKey(e.visibleIndex); }"; // ==> 's' refers to button object as sender
}).GetHtml();

What you should use is GridViewDataItemTemplateContainer object to get KeyValue property for corresponding row, which returns integer value from GridViewBaseRowTemplateContainer:

column.SetDataItemTemplateContent((c) =>
{
    Html.DevExpress().Button(b =>
    {
        b.Name = "btnVE" + c.KeyValue;
        b.Text = "V/E";
        b.UseSubmitBehavior = false; // prevent default submit action
        b.EnableClientSideAPI = true; // add this line if not sure
        b.ClientSideEvents.Click =
        "function(s, e) { window.location = '" + DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" })
        + "?key=" + c.KeyValue.ToString() + "'; }";

    }).GetHtml();
});

Or using string.Format() which easier to read:

b.ClientSideEvents.Click = string.Format("function(s, e) {{ window.location = '{0}?key={1}'; }}", 
                           DevExpressHelper.GetUrl(new { Controller = "ViewPrincipal", Action = "EditRecord" }), 
                           c.KeyValue.ToString());

Notes:

1) If you want to get row index, use c.VisibleIndex.

2) For cross-browser concern, I preferred window.location to document.location as provided here.

Related issue: GridView - How to define Button inside grid

Upvotes: 2

Related Questions