0

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?

TheHolyTerrah
  • 2,859
  • 3
  • 43
  • 50
  • You realy need to post some code, where does the HasSelection bit gone (data or...). How do you bind the data to the grid etc? – Peter Feb 11 '14 at 14:48
  • No code necessary. I just wanted the WebGrid to reset itself whenever it's repopulated with a new set of data. Seems that should've been one of the first things out of the box the type should do without having to do anything special. – TheHolyTerrah Feb 12 '14 at 12:59

2 Answers2

0

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());
}
LostInComputer
  • 15,188
  • 4
  • 41
  • 49
  • Updating the SelectedIndex there hijacks an actual user's selection. So if the user selects a WebGrid row, the behavior to act on it is bypassed since it's being ignored via the index reset. Attempting to do so afterward throws an exception since the WebGrid is already bound. I've been working on dropping the querystring already. But haven't found a turnkey method of doing so in the server-side code. – TheHolyTerrah Feb 11 '14 at 16:31
  • There is an easy way to not hijack the actual user's selection. See my updated code. – LostInComputer Feb 12 '14 at 00:31
  • Ok. Thanks for the details. Though that's not the answer I ultimately found to fix the issue, I'll give it to you since modifying the SelectedIndex is a bit of an art. ;) – TheHolyTerrah Feb 12 '14 at 13:01
0

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.

TheHolyTerrah
  • 2,859
  • 3
  • 43
  • 50