views:

1300

answers:

3

To start I am using a MultiView control to step users through searching. The first page in the MultiView is just a search box with a button to preform the search.

The second page has the GridView, but I would like to keep the search box and button for the user to search again if they didn't find the user they were looking for.

When you search from page one and move to page 2 the GridView shows the correct results. But when it is on the second page with both the GridView and the search the GridView doesn't update. Below is the code I'm using.

//GridView = SearchResults  
//SqlDataSource = AddPlayerDataSource  
//MultiView = PlayerSearchView

protected void PlayerSearch_Click(object sender, ImageClickEventArgs e)
{
    string userId = User.Identity.Name.ToString();

    if (SearchText.Text != "" && !userId.Equals(""))
    {
        GridView SearchResults = (GridView)PlayerSearchView.FindControl("SearchResults");

        string SqlSelect = "SELECT [id], [username] FROM [users] WHERE [username] LIKE '%" + SearchText.Text + "%'";
        AddPlayerDataSource.SelectCommand = SqlSelect;
        SearchResults.DataBind();

         if (PlayerSearchView.ActiveViewIndex != 1)
             PlayerSearchView.ActiveViewIndex = 1;
    }
}
A: 

Perhaps the ViewState of GridView is being loaded after you databind? Try turning off the ViewState (probably better, as you don't seem to really need that) or do the databinding in the OnPreRender().

Moron
viewstate is loaded before the page load and the click handler is executed after that so I don't think it's a view state issue. I agree that the viewstate is not needed though.
JohnIdol
A: 

Instead of declaring a new gridview "SearchResults" and then calling databind on that shadow copy of your gridview, just databind the gridview in the view itself.

Just call

((GridView)PlayerSearchView.FindControl("SearchResults")).DataBind()

instead of

SearchResults.DataBind();

Then you can get rid of

GridView SearchResults = (GridView)PlayerSearchView.FindControl("SearchResults");

That way your SqlDataSource control will execute the select() when databinding the existing gridview, not the copy.

Drell
I did try this out and it did not work. With C# the assignment of objects is not making a "shadow" copy. It is referring to the original control object.
jWoose
+1  A: 

Do you need to set the DataSource property? You have that set in comments above the code, but it's unclear when that is happening. Put a breakpoint on SearchResults.DataBind() and make sure all the properties are set correctly here.

Incidentally... these kinds of problems are why it's infinitely preferable to use a proper business logic layer. If you were controlling the code that actually does the DB query you'd either not have this problem or know exactly where it was.

GridView SearchResults = (GridView)PlayerSearchView.FindControl("SearchResults");
SearchResults.DataSource = Data.Players.LoadAll(); //Loads a DataTable with your data
SearchResults.DataBind();

EDIT: Does everything work the way you expect it to if you remove the MultiView? Let's narrow down the problem.

Bryan
The DataSource property is set in the .aspx main page. As I've said when the ViewState.ActivePageIndex changes from 0 to 1 it works fine. But when I'm already on ActivePageIndex = 1 the GridView does not refresh. I'm not sure there is a benefit to assigning the DataSource that way.
jWoose
The benefit is that the LoadAll() is actually making the SQL/data layer call. Exactly the part you are missing. Can you force AddPlayerDataSource to reload by manually calling Select()? I am not too familiar with this object.
Bryan
Thank you. I had been looking at the problem for too long. The MultiView was hiding the real problem from me. I was having issues with the search text box.
jWoose
To be more specific I thought the search text box and button were outside of the MultiView, but I had put them inside the MultiView. The end result was 2 different text boxes. Careless mistake thanks to everyone for the help.
jWoose