Reputation: 385
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.
Upvotes: 5
Views: 32175
Reputation: 1709
I was changing the name of the parent table row at runtime:
currentRow.ID = $"dashboard-row-{e.Row.RowIndex}";
#before
<input type="submit" name="ctl00$MainContent$gvDashboard$ctl05$btnEdit" value="Edit" id="btnEdit" class="btn edit-button" style="font-family:Poppins;font-size:12pt;font-weight:normal;">
#after
<input type="submit" name="ctl00$MainContent$gvDashboard$dashboard-row-4$btnEdit" value="Edit" id="btnEdit" class="btn edit-button" style="font-family:Poppins;font-size:12pt;font-weight:normal;">
Has something to do w/ the naming container. I'm guessing the runtime somehow picked up on the existing name attribute prior to me changing the ID of the parent table row.
When I commented out this change, everything worked (I should instead set ID of some element within the table row...only reason I'm doing this is to scroll to that row after postback since postback resets scroll offset of page and user has to manually scroll back down to find target row they were attempting to edit).
Upvotes: 0
Reputation: 71
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.
Set AutoGenerateEditButton="False"
this way you can add this field via ASP Code.
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.
Upvotes: 7
Reputation: 1
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.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 115
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.
Upvotes: 0
Reputation: 101
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.
Upvotes: 9
Reputation:
This fixed the issue for me:
If Not Master.Page.IsPostBackEventControlRegistered Then
'logic to bind data
End If
Upvotes: 1
Reputation: 385
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.
Upvotes: 1
Reputation: 8078
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
Upvotes: 1
Reputation: 8078
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.
Upvotes: 0