I have a web form with a text box, button and a data grid. when clicked the button will populate the data grid with records that match the text box value. This part works.
The column headers are sortable so the user can put the records in whatever order they want. However when I click a header to sort by the records disappear. The code executes without error, but no records are returned. I have researched Page.IsPostBack and View States to see if that would solve the problem, but it hasn't yet.
Below is my code to populate the data grid and sort the table. I found the code for sorting in the help files. The prerender code is from ASP.Net for Dummies. They say it is good practice to store all your variables after the page loads.
The other piece that is confusing me is I have a data grid on another page that works fine. That data grid has no parameters passed to it though.Code:Dim UserName As String Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If IsPostBack Then UserName = CStr(ViewState("UName")) End If End Sub Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender ViewState("UName") = UserName End Sub Private Sub btnFillGrids_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFillGrids.Click If fvUserName.IsValid Then UserName = txtUserName.Text cmdAssigned.Parameters.Item(0).Value = UserName daAssigned.SelectCommand.Connection.Open() daAssigned.Fill(dsAssigned) daAssigned.SelectCommand.Connection.Close() dgAssigned.DataBind() cnnCWO2.Dispose() cmdAssigned.Dispose() cnnCWO2 = Nothing cmdAssigned = Nothing End If End Sub Private Sub dgAssigned_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgAssigned.SortCommand dvAssigned.Sort = e.SortExpression dgAssigned.DataBind() End Sub
Thank you in advance.




Reply With Quote