Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid ItemCommand not firing 2

Status
Not open for further replies.

skip2004

Programmer
Nov 9, 2004
37
GB
Some help with this would be appreciated.

I am programmatically adding a LinkButton to my DataGrid at run time (in the datagrid.ItemDataBound event).

All works well and is displayed as expected until I click the LinkButton - the datagrid.ItemCommand is not firing.

As a temporray hack, if I reload the data on postback into the dataset and re-bind to the DataGrid - it then works fine.

As the query is potentially big I would rather not have to issue the query again.

I have EnableViewState turned on so data is retained - but it seems the events are losing their pointers, and it seems the only way to re-link them is to re-bind each time.

Is there any way I can avoid having to re-bind on each postback?

Any help greatly appreciated. Thanks.
 
>>WHen the page generates it compares controls when it sent it to the browser and after it comes back (some user action).

agreed, but from what i understand every event of a child control is always trapped by the datagrid (a good example is pagination where even before u page to the next page the page number is available, even though the numbers are controls)...

Known is handfull, Unknown is worldfull
 
Good point.
Still the LinkButton Start the whole thing and if isn't there to start it, then there's no event for the datagrid to trap.
 
then wont that make the entire ItemCommand useless? cause one of the main reasons to maintain veiw sate is to save round trips to the database, but if that is going to stop normal events (like IteCommand) from firing then wont that be a imporper conctualisation? let me try to do some research on this...

Known is handfull, Unknown is worldfull
 
You confused me.

Veiwstate is there to persist state across postbacks. It doesn't work in this example because dynamic controls are added after the page load its viewstate. Therefore the linkbutton event doesn't fired

If you want to save trips to the DB then store the results in session, application, cache, text file or some other persistable medium. Viewstate is not the place for this.
 
nope, i was not talking about the use of view state i was rather talking about the use of this line:

if not IsPostBack


that saves a lot of trips to the database (like u need not rebind the grid for the current page)

it is this data thats saved in view state, and it is with this data that the grid for the page is rebuilt...

Known is handfull, Unknown is worldfull
 
You right viewstate is the data that the grid for the page is rebuilt. But the Linkbutton control is not added to the control hierarchy until after the load view state happens so it can not be restored from view state.

The load view state phase of the page life cycle recursively loads view state data that had been saved from the previous page into the control hierarchy of the page.

I would think that if the original poster only went to the DB on first trip to the page rebound the datagrid on every postback.

This would work. Not an optimal solution though

Something like
dim ds as dataset

If not ispostback then
//Get data and store in session
ds = GetData()
Session("ds") = ds
else
//Get data out of session
ds = ctype(Session("ds"),dataset )
end if

//BindDataGrid to ds everytime
 
Here is full example. This would only go to the datasource once.

1) Start Web Project
2) Add datagrid to web page
3) Put this code in code behind

Private _ds As DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
'Get some data
_ds = New DataSet
_ds.Tables.Add(New DataTable("TestData"))
_ds.Tables(0).Columns.Add(New DataColumn("col1"))
_ds.Tables(0).Columns.Add(New DataColumn("col2"))
_ds.Tables(0).Rows.Add(New Object() {"Val1,1", "Val1,2"})
_ds.Tables(0).Rows.Add(New Object() {"Val2,1", "Val2,2"})

Session("ds") = _ds
Else
_ds = DirectCast(Session("ds"), DataSet)
End If
DataGrid1.DataSource = _ds
DataGrid1.DataBind()
End Sub

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
Dim objLinkBtn As LinkButton
If (e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem) Then
If e.Item.ItemIndex Mod 2 = 0 Then
objLinkBtn = New LinkButton
objLinkBtn.Text = "View Image"
objLinkBtn.CommandName = "SelectImage"
e.Item.Cells(0).Controls.Add(objLinkBtn)
Else
e.Item.Cells(0).Text = "No additional action"
End If
End If
End Sub

Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand
Response.Write("Yo in the Item Command")
End Sub
 
nice one.

but isnt storing all the data in a session heavy on the server???

Known is handfull, Unknown is worldfull
 
Really depends.
If your site has 200K concurrent users then probable and the object is large then probable.

ASP.Net session and Old ASP session are different. MS has made many improvements and using session isn't as taboo as it used to be. Responsible used it's OK.

skip2004, did you ever get this working?
 
yeah, what happened???

Known is handfull, Unknown is worldfull
 

Hi guys - I appreciate your help and advice.

I've not had chance to implement your suggestions yet - hopefully will have time in the next couple of days.

I'll let you know soon! Thanks once again.
 

OK, I've had chance to implement your suggestions. It didn't quite work so had to make a few tweaks, but the idea of storing the DataSet in a session variable is the solution.

My code looks like:

If Not IsPostBack Then
LoadData()
Else
DsAdvancedAnswer1 = CType(Session("DSAA"), Proj1.dsAdvancedAnswer)
dgAdvancedAnswers.DataBind()
End If

The change from your suggestion is you had set the DsAdvancedAnswer1.DataSource to the session variable - however, it should be the the entire dataset object (DsAdvancedAnswer1) which should be set.

Anyway, apart from that small issue, the code works fine and my event now fires as expected.

Many thanks for your help!

Cheers,
skip2004
 
Sorry, typo in there...

Should read that you had the dgAdvancedAnswers.DataSource set to the session vaiable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top