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.
 
>>As a temporray hack, if I reload the data on postback into the dataset and re-bind to the DataGrid - it then works fine.

the click event works, hmm it seems like EnableViewState type of problem at first glance but since u have enabled that too my only other doubt is - is javascript enabled???

Known is handfull, Unknown is worldfull
 
maybe post your ItemDatabound event?

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 

vbkris - from what I can tell JavaScript is enabled.

Here's the ItemDataBound event:

Private Sub dgAdvancedAnswers_ItemDataBound(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgAdvancedAnswers.ItemDataBound
Try
Dim objLinkBtn As LinkButton

If (e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem) Then

If e.Item.Cells(4).Text.ToUpper = "IMAGE" Then
objLinkBtn = New LinkButton
objLinkBtn.Text = "View Image"
objLinkBtn.CommandName = "SelectImage"
e.Item.Cells(0).Controls.Add(objLinkBtn)
ElseIf e.Item.Cells(4).Text.ToUpper = "SIGNATURE" Then
objLinkBtn = New LinkButton
objLinkBtn.Text = "View Signature"
objLinkBtn.CommandName = "SelectSignature"
e.Item.Cells(0).Controls.Add(objLinkBtn)
Else
e.Item.Cells(0).Text = "No additional action"
End If
End If

Catch ex As Exception
EventLog.WriteEntry("Proj1", "Answers:dgAdvancedAnswers_ItemDataBound:error=" + ex.Message, EventLogEntryType.Error)
End Try
End Sub

The idea is that this fires when the DataGrid is been built, and depending on a particaular column (ie. cell of the datagrid), I build a new control which allows them to link to another page. Otherwise the text "No Additonal Action" is displayed.

Everything displays as it should - but the ItemCommand event does not fire unless I build the page again on PostBack (ie. issue the query again and re-bind it to the datagrid).

Thanks.
 
I believe that the problem would be these lines
Code:
objLinkBtn.CommandName = "SelectSignature"

the ItemCommand is looking for a command name of "Select" I believe...if the use of this, is just to send to a different page...then you could add javascript onclick's to the button in the itemdatabound event and not even use the ItemCommand...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 

Thanks for that.

I have tried changing the CommandName to just "Select" and this makes no difference unfortunately - the event is still not firing as expected.

It is used to navigate to a different page but unfortunately I need to run some server side code first.
 
>> Private Sub dgAdvancedAnswers_ItemDataBound(

if i am right this event is raised only when the DataBind() method is called.

therefore it was NOT called during post back.

on post back i guess u have to write ur own function to read through the rows...

Known is handfull, Unknown is worldfull
 

True, this event is called for every row in the datagrid as it is being built ie. the databinding of each row. I am using the event to programmatically add a new control dependent on data values at the time.

The ItemCommand should fire but isn't unless I re-bind on PostBack (not good!)

Just wondering why I have to re-bind for the ItemCommand to fire and if there is a way around it.

Thanks.
 
>>Just wondering why I have to re-bind for the ItemCommand to fire and if there is a way around it.

write ur own function to read through the rows of the datagrid, its pretty easy...

Known is handfull, Unknown is worldfull
 

Quite happy to run some server side code if the event would fire in the first place !!!
 
>>Quite happy to run some server side code if the event would fire in the first place !!!

that event WILL never fire.

ur CodeBehind looks something like this right?

if not ispostback then
'BIDNING CODE HERE
else
CallMe()
end if

function CallMe()
'BROWSE Through the datagrid here using normal loops
end function

Known is handfull, Unknown is worldfull
 

Thanks vbkrid, I appreciate your help here.

Your are correct, my Page_Load routine looks like:

If Not IsPostBack then LoadData()

In this scenario the ItemCommand event does not fire. If I just call LoadData in the Page_Load sub regardless, the event does fire.

I'm not sure how having this CallMe function will help me - it will get fired on every postback to the form - including clicking column headers to re-sort, paging, etc. And how will I know which item was clicked in the datagrid to be able to perform any additional processing on that item?

Thanks again for your help here.
 
hmm,

just give me some time, there must be some kind of bubling event for datagrids, i will check it and come back to u...

Known is handfull, Unknown is worldfull
 
ok,

>> - the datagrid.ItemCommand is not firing.

thats the bubbling event for datagrid control. can i have the code for this sub too???

Known is handfull, Unknown is worldfull
 

Thanks vbkris, I appreciate this. Here is the code for the ItemCommand event:

Code:
    Private Sub dgAdvancedAnswers_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgAdvancedAnswers.ItemCommand
        Try
            Dim strRecordKey, strNavigateTo, strHeight, strWidth As String

            Select Case e.CommandName
                Case "SelectImage"
                    strNavigateTo = "Image.aspx"
                    strWidth = "700" : strHeight = "700"
                Case "SelectSignature"
                    strNavigateTo = "Signature.aspx"
                    strWidth = "600" : strHeight = "400"
                Case Else
                    Exit Sub
            End Select

            Session.Item("QuestionnaireID") = e.Item.Cells(5).Text
            Session.Item("QuestionSequence") = e.Item.Cells(6).Text
            Session.Item("RepeatedFirstAnswer") = e.Item.Cells(7).Text
            Session.Item("Answer") = e.Item.Cells(2).Text

            Response.Write("<script language='javascript'>window.open('../Common/" + strNavigateTo + "', 'AdditionalData', 'width=" + strWidth + ",height=" + strHeight + ",left=30,top=30');</script>")

        Catch ex As Exception
            EventLog.WriteEntry("Proj1", "Answers:dgAdvancedAnswers_ItemCommand:error=" + ex.Message, EventLogEntryType.Error)
        End Try
    End Sub

Thanks.
 
i dont see an error on the face of it, lets start debugging it.

try giving a response.write() in the first line of the item command sub to see if it is firing atleast...

Known is handfull, Unknown is worldfull
 
I think that you should look at the fact that you're adding the link buttons at runtime. That means they don't exist until you databind. That's why the event doesn't get fired.

Dynamic controls must be re-added in the init of the page for the event to get fired. Has to do with page life cycle. If the controls aren't there they can't fire events.

Is there anyway that you can use a template column with the linkbutton in it at design time. Then just configure each linkbutton at runtime.


 
>>That means they don't exist until you databind

correct but wont that problem happen only when u try to access the control? in IemCommand u dont access the control rather u access the "e.CommandName"

correct me if i am wrong...

Known is handfull, Unknown is worldfull
 
Has to do with the event firing.
WHen the page generates it compares controls when it sent it to the browser and after it comes back (some user action). It then decides which events to fire off.

This article may help out a bit:

I think your angle of problem solving is a good one, but this is another possibility.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top