views:

3369

answers:

7

I have a Gridview with AutoGenerateColumns="False".
I am using a TemplateField to display my Edit, Update and Cancel 'buttons' in the first column of the GridView within respective ItemTemplate and EditItemTemplate fields.

Within the ItemTemplate I have an ImageButtong with a CommandName of "Edit". This works as expected and I can put a breakpoint in the RowCommand event handler to see the "Event" command name. After it has been clicked the postback places that row in edit mode. All textboxes appear as they are meant to.

At this point in time the above EditItemTemplate is displayed with two ImageButtons within it. One has it's CommandName = "Update" and the other "Cancel".

My problem lies in that the click on the Update ImageButton posts back, but neither the RowCommand nor RowUpdating events get triggered.

I have set the requisite attributes in the GridView tag. (Note, in the gridview the EnableViewState="False" - if I set it to True I get the standard "Failed to load viewstate. The control tree..." etc. error)

One strange thing that I've noticed that makes me think it's a ViewState problem is that if I change the CommandName of the Update button to "Edit" that postback event does get captured in the RowCommand event...

Any suggestions are welcome. Thanks.

A: 

if you change the name of the command to 'update' you will have to handle the update in rowcommand which shouldn't be a problem - right?

Check out this Question I asked. It may help

Added
Something you could do is change the commandname to whatever you'd like and handle it in Rowcommand. Make the database updates/inserts manually on rowcommand.

Eric
Hi Eric - unfortunately the RowCommand is not being triggered when the Update ImageButton has it's CommandName as anything other than "Edit"... I don't think the EditItemTemplate ImageButtons are being created for the postback to register them so their events aren't being raised - either that, or the gridview is being re-bound and they are being lost. Ideally I'm looking for a full working sample where there are customised buttons used for this type of scenario.
Sean
ok. Hang on. I'll help you.
Eric
Thanks Eric - I thought I had a solution (see 'answer' below) but realised I need the TemplateFields instead of the CommandField as I have an insert row in the footer. Please ignore the answer below...
Sean
A: 

Sean,

I understand you have the answer now but for future references you would have to create an addhandler and a delegate to do what you wanted to do. I misunderstood the question at first. But here's what you would do if you chose not to use a command field.

//This is in pageload

If Not IsPostBack Then
        'Create new column for Edit buttons
        'Dim field As New TemplateField
        Dim actionfield As New TemplateField


        actionfield.HeaderText = "Action"
        Dim actioncol As DataControlField = actionfield
        GridView1.Columns.Insert(8, actioncol)//the eight is the column number of where you are adding the column. below you will add the button. You really don't need to add this column programmtically. I normally do though.

    End If

//rowcreated

 If e.Row.RowType <> DataControlRowType.footer Then
            btnedit.ToolTip = "Edits the Current Record"
            btnedit.ImageUrl = "\images\bttnEditMini.gif"
            GridView1.Rows(i).Cells(8).Controls.Add(btnedit)
            btnedit.CommandName = "view"//notice commandname. You can manipulate it.
            btnedit.CommandArgument = GridView1.Rows(i).Cells(0).Text
            AddHandler btnedit.Click, AddressOf btnedit_Click
 end if

//then notice you must create an imageclickeventhandler delegate

 Public Delegate Sub ImageClickEventHandler(ByVal sender As Object, ByVal e As ImageClickEventArgs)
Sub btnedit_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
//whatever actions you need to take.   

end sub
Eric
Cheers for that, Eric. You probably realise by now that I was mistaken in thinking I had the answer :(It's bedtime here - I'll have to get back to your answer tomorrow evening. Thanks for the help so far.Sean
Sean
No problem Sean! Hope this helps!
Eric
A: 

Refer link below

Edit update records in gridview

+1  A: 

I removed the master page and let the page that contained the GridView inherit from Page, and it worked. Something in my inheritance stack (or something in the MS controls) didn't work the way I had it set up.

Sean
A: 

This fixed the issue for me:

   If Not Master.Page.IsPostBackEventControlRegistered Then

        'logic to bind data

   End If
Jim Wild
A: 

Hi all,

I also have the same problem in a Gridview with Edit,update,cancel. Edit and Cancel event get fired but never Update Event. Then finally I change the CauseValidation to false for Update linkbuttion form the Edit Template field. It suprisingly, i work fine. Great.

Hope this will help other who is facing my problem. Regards, Asem Ibohal

Asem Ibohal
A: 

I also had same problem as noted by Asem and same with a Delete Template field with client side confirmation. Changing the CauseValidation flag to False for the Update and Delete LinkButtons resolved the problem. Thank you, Asem, for your answer, although I don't really understand why it works.

Ron Woods