Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - Dynamic Bound Columns OnSortCommand not called (Solution)

 
Thread Tools Search this Thread
Old 02-22-2005, 10:06 PM   #1
Default Dynamic Bound Columns OnSortCommand not called (Solution)


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
  Reply With Quote
Old 02-28-2005, 04:13 PM   #2
=?Utf-8?B?RGlyazQx?=
 
Posts: n/a
Default RE: Dynamic Bound Columns OnSortCommand not called (Solution)
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?=
  Reply With Quote
Old 02-06-2008, 06:04 PM   #3
slickuser
Junior Member
 
Join Date: Feb 2008
Posts: 1
Default little late
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
slickuser is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46