I'm having trouble with the DataGrid Web Control. I'm
manually adding BoundColumns to the DataGrid and finally
adding an "Edit" column and then calling the DataBind()
method.
This all works fine, with all the data correctly
displayed, and one "Edit" hyper link in each row. Clicking
on the link, however, doesn't fire the ItemCommand or
EditCommand events of the DataGrid.
If I use the property pages and manually add the "Edit"
column using the designer, the ItemCommand fires ok.
Here's some sample code for setting up the DataGrid and
columns:
===========================================================
With grdClientList
.AutoGenerateColumns = False
.DataSource = objDataSet '<- previously populated.
'Add all the required columns to the grid.
'-[ Client Name ]-
objDataGridColumn = New BoundColumn()
With objDataGridColumn
.DataField = "ClientName"
.HeaderText = "Client Name"
End With
.Columns.Add(objDataGridColumn)
'Add the edit command column.
Dim objTemp As New ButtonColumn()
With objTemp
.ButtonType = ButtonColumnType.LinkButton
.CommandName = "Edit"
.Text = "Custom Edit Button"
End With
.Columns.Add(objTemp)
'Bind the data source to the grid.
.DataBind()
End With
===========================================================
(The AutoEventWireup page property of the ASPX page is set
to "false". Setting this to true causes the page to load
twice each time you navigate to it.)
This must must be done using a CodeBehind page, not inline
scripting on the client.
Does anyone know what is going wrong here and how to fix
it?
Thanks.
Tom Cammarano.
"Tom Cammarano" <to...@trilogy.com.au> wrote in message
news:afe101c29069$6c2d7740$89f82ecf@TK2MSFTNGXA01...
If you want the ItemCommand (or any other event) to fire
for a custom added button column, you have to make sure
that the grid is re-bound in the Page_Load event to a
state where it is identical to when the page was first
loaded.
I achieve this by storing the dataset that I bind the grid
to (after all the columns have been added) in the Session
object by doing a simple Session.Add
("SessionVariableName", objDataSet).
Then when the Page_Load event is fired I add all the
columns to the grid again. If it is a PostBack I simply
use the dataset that is in the session variable to re-bind
to the grid, otherwise I load a new dataset from the
database. The dataset's state is kept this way just in
case someone else added or changed a record before the
post back was called.
This behaviour is merely mimicking what the DataGrid is
supposed to do automatically (I believe), namely store its
state between post backs.
The drawback to this approach is that this requires more
server resources (specifically memory) to store a dataset
of varying proportions for EVERY single client that is
browsing that page.
I remove the dataset from the session when the user
navigates to another page.
I hope this helps.
Regards,
Tom Cammarano.
Analyst/Programmer
Trilogy Resources Pty. Ltd.
(www.trilogy.com.au)
>.
>