Question

Self explanatory question.

Between posts, the grid I have setup is retaining the HasSelection bit, even if the WebGrid has been re-loaded with new data. Therefore, the functionality I have wired into the physical selection of a WebGrid record runs, even though the user hasn't selected anything on the new resultset yet.

Thoughts?

Was it helpful?

Solution

WebGrid obtains the selected row thru the query string. by default, the query string field is row like http://localhost/grid?row=2

Ideally, you would remove that query string field before posting back like so http://localhost/grid

If it is not possible, set WebGrid.SelectedIndex to -1 instead.

Edit

Here are few ways to set WebGrid.SelectedIndex:

@{
    WebGrid grid = new WebGrid(Model);
    grid.SelectedIndex = ViewBag.SelectedIndex;
    @grid.GetHtml(
        columns: grid.Columns(
            ...
        )
    )
}

public ActionResult Index(int? row)
{
    ViewBag.SelectedIndex = (IsKeepSelection() ? row.GetValueOrDefault() : 0) - 1; //TODO: add bound checking
    return View(People.GetPeople());
}

Or (I prefer the previous one though since it's easier to understand):

@{
    WebGrid grid = new WebGrid(Model);
    if(ViewBag.ClearSelection) {
        grid.SelectedIndex = -1;
    }
    @grid.GetHtml(
        columns: grid.Columns(
            ...
        )
    )
}

public ActionResult Index(int? row)
{
    ViewBag.ClearSelection = IsClearSelection();
    return View(People.GetPeople());
}

OTHER TIPS

The ultimate answer to this issue was to clear out the "action" on the form. Apparently, the WebGrid is tightly coupled to this value and therefore the makers of the WebGrid expected you to futz with the action attribute instead of the WebGrid itself.

I simply reset the querystring with document.forms[0].action = window.location.pathname; in my submission button. Since the form's action resolves to the querystring, this fixed it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top