4
votes

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.

10
Has anyone got a sample where they have substituted the CommandField (built in buttons for the GridView functions) with their own TemplateFields for editing?Sean
Blast! I've just remembered that I need the TemplateField solution as I also have an Insert line in the footer, and require the FooterTemplate to contain an ImageButton... pulling my hair out here!! Please ignore the self-answer below if it hasn't been deleted.Sean
Be sure to use the CommandName Update when using custom TemplateField.DreamTeK

10 Answers

8
votes

As noted by Asem and Ron, adding the CausesValidation="false" attribute to the CommandField resolved the problem. The reason was that I had some other validation controls on the page and on the GridView update the page was firing the other validation controls, so I think its better to set a ValidationSummary property.

6
votes

I had this same situation where my "Edit" button was causing other validations (albeit hidden popups) to execute behind the scene.

From reading the solutions I was looking for the standard CausesValidation="false" property to fix the issue. I was not able to locate that field because it seems as if I was using AutoGenerateEditButton="True" to add my edit buttons to the gridview.

My SOLUTION was as follow and I hope this help you save some valuable time as well.

  1. Set AutoGenerateEditButton="False" this way you can add this field via ASP Code.

  2. Use the code below to add the "Edit" button field to your gridView code as follow.

    <asp:commandfield showeditbutton="true" causesvalidation="false" headertext="Edit"/> 
    

If validation was the problem, you would now be able to see your Updating module firing as axpected.

2
votes

Set the GridView EnableViewState property to true.

1
votes

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
1
votes

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.

1
votes

This fixed the issue for me:

   If Not Master.Page.IsPostBackEventControlRegistered Then

        'logic to bind data

   End If
0
votes

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.

0
votes

It has been mentioned on this page in other answers that the error is caused by validation. The reason is that there are validators on the page, which quite possibly do not apply to the particular row being edited, and for whatever reason they are not being met. If you want validators to work for your currently editing row, you will not want to set CasuesValidation to false... Instead, you will want to disable any irrelevant validators except those that you care about for the purpose of editing this row.

  1. Find the validators that are not within the edit item template of this gridview row, set them to disabled in page_load.
  2. Let 'CausesValidation' stay true on the template command fields (by default it is true so you can just elide it.) so that anything in the row that you want to validate still gets validated.
  3. Profit.
0
votes

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 surprisingly works fine.

0
votes

If you using any function for retrieving(Binding) the grid view from database and calling it in page_Load() event then it may cause this issue. try to call this function in page_LoadComplete() event and it will work.