![]() |
|
|
|||||||
![]() |
ASP Net - Dynamic Bound Columns OnSortCommand not called (Solution) |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
Alright, I am posting my solution to a problem I've been having. I have
finally found the answer, and it took me a while so I thought I'd provide the final solution here, since it may be benificial to others and because I had to search for the solution (not the problem) in order to find a working one. I had a datagrid that wasn't sorting, the onsortcommand seemed as though it wasn't being called, and I had dynamically bound columns in the page_load sub. That was the big mistake. Apparently, in order for the sortcommand to work correctly, you need to bound your columns in a different sub, the page_init event sub. Once you successfully do this, all is well. After doing a few very specific searches I found this answer (in a rather vague form) on usenet. The page_init sub runs before the page_load sub is run, and is only run once when the page is first loaded, not on every page load. ------------ The code should look like: Sub Page_Init(Sender As Object, E As EventArgs) 'Don't forget to open a sql connection here, and fill the dataset. objDataTable = objDataSet.Tables(0) For i = 0 To objDataTable.Columns.Count - 1 objCol = New BoundColumn() objCol.HeaderText = objDataTable.Columns(i).ColumnName objCol.DataField = objDataTable.Columns(i).ColumnName objCol.SortExpression = objDataTable.Columns(i).ColumnName DataGrid1.Columns.Add(objCol) Next End Sub Some settings in the datagrid: AutoGenerateColumns="False" OnSortCommand="SQLDataGrid_SortCommand" AllowSorting="True" ------------ Hope this helps anyone out there that is having this problem. Chad Devine |
|
|
|
|
#2 |
|
Posts: n/a
|
Or you could do this:
private page_load() { if ( !Page.IsPostBack) { //Build Dynamic columns here } } You are only supposed to build (bind) your columns once, not everytime the page_load event is called. ENJOY ! "Chad Devine" wrote: > Alright, I am posting my solution to a problem I've been having. I have > finally found the answer, and it took me a while so I thought I'd > provide the final solution here, since it may be benificial to others > and because I had to search for the solution (not the problem) in order > to find a working one. > > I had a datagrid that wasn't sorting, the onsortcommand seemed as > though it wasn't being called, and I had dynamically bound columns in > the page_load sub. That was the big mistake. > > Apparently, in order for the sortcommand to work correctly, you need to > bound your columns in a different sub, the page_init event sub. Once > you successfully do this, all is well. After doing a few very specific > searches I found this answer (in a rather vague form) on usenet. > > The page_init sub runs before the page_load sub is run, and is only run > once when the page is first loaded, not on every page load. > > ------------ > The code should look like: > Sub Page_Init(Sender As Object, E As EventArgs) > 'Don't forget to open a sql connection here, and fill the dataset. > > objDataTable = objDataSet.Tables(0) > For i = 0 To objDataTable.Columns.Count - 1 > objCol = New BoundColumn() > objCol.HeaderText = objDataTable.Columns(i).ColumnName > objCol.DataField = objDataTable.Columns(i).ColumnName > objCol.SortExpression = objDataTable.Columns(i).ColumnName > DataGrid1.Columns.Add(objCol) > Next > > End Sub > > Some settings in the datagrid: > AutoGenerateColumns="False" > OnSortCommand="SQLDataGrid_SortCommand" > AllowSorting="True" > ------------ > > Hope this helps anyone out there that is having this problem. > > =?Utf-8?B?RGlyazQx?= |
|
|
|
#3 |
|
Junior Member
Join Date: Feb 2008
Posts: 1
|
I know this forum has had no activity, but I found it while searching for a solution to the same problem the original poster had. I also followed everything to make my sortcommand event work. Every time i would try to sort, the datagrid would disappear. When using the debugger, it was clear that the sorting event was never called.
After setting up the grid in the OnInit, everything was fine. I am not sure if it was an ordering issue of when events are registered or what, but this solution worked for me. Thank you. =?Utf-8?B?RGlyazQx?= : I was checking if the page was a post back. When you clicked to sort, the page would always call Page_Load and determine it was a post back, then do nothing (I had no code otherwise). So, if all else fails, give this solution a try. slickuser |
|
|
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 'Dynamic Full' vs '2/8' means what? | Dogger the Filmgoblin | DVD Video | 2 | 06-30-2004 09:03 AM |