How can I pevent Gridview.DataBind when a commandField is pressed - asp.net

I have a gridview included "Edit CommandFields Update and Cancel" at the last column of my gridview. Also, I have a search button above my Gridview and everything works properly together. When I am searching for a particular Column the "sqlDataSource" of my Gridview is changing respectively on the query and displays the results.
However, the Edit/Update/Cancel command field buttons refresh the whole Gridview and I am loosing the selected editable row derived from the search results.
How can I prevent grdiview Databing when I click the Edit Command Field ?
Any advice would be appreciated,

You are probably missing the famous is Page.IsPostback check.In vb.net you will have to do some thing like this
If Not Page.IsPostBack
->your binding code goes here
End if
C#
if (!Page.IsPostBack)
{
->your binding code goes here
}
This way your results are not lost when the page reloads.

Related

GridView CommandName = "INSERT" not working

I am using GridView data bound control and trying to insert a record from the footer template, below is snapshot of my code:
The insert command doesn't seem to be working as expected. Insert button click does not do anything. However, if I have a delete button with CommandName="Delete" (with same markup), it works fine.
Does GridView not understand the "Insert" CommandName? Am I missing anything?
Thanks!
GridView in ASP.NET does not support insert operations. Lots of people have added a footer row and a button to that row to do the insert. example: http://geekswithblogs.net/casualjim/archive/2005/08/29/51360.aspx
My personal preference is to have a detailsview linked to the gridview and do inserts through the detailsview. It's not pretty but it works. the footer row solution is much nicer.

New row in ASP.NET GridView Control

I want to do something really simple, I just can't seem to find the EnableClientAddRow property, so I can set it to true. I have a standard GridView control on a web form. I want a button to appear on the web form. When the user clicks the button, an empty row is added to the GridView UI, so the user can enter data in the appropriate fields. The row will of course, have a "Save" button of some type in one of the columns.
I know this functionality must be in the GridView somewhere, I just can't find it. I did find some odd hacks that try to manually implement this. I'm not really interested in footer manipulations or binding tricks, just the standard add row method.
EDIT:
It appears the GridView does not support adding a row as a first-order operation. This appears to be a serious design flaw.
I typically add a new record to the underlying data source as a part of the "add record" button click action. I then re-bind the view in order to show the blank row.
The new record is typically a DataRow if the GridView is bound to a DataTable, or an object if the GridView is bound to a collection of a particular type. Not sure if that is what you consider a binding trick from your question, but it works well and is quite easy to implement.
Edit - more detail to describe the process:
Add the row to the data source, set the EditItemIndex to the newly added row in order for the row to enter edit mode, then bind the data source to the GridView. Your EditItemTemplate would contain a Cancel and a Save button. Cancel would re-bind the GridView to the underlying data source without the empty row and set EditItemIndex to -1, thereby removing the row from the GridView.
How to easily insert row in GridView with SqlDataSource
If you add a new row to the datasource, even if the row has empty values, and you databind the datasource to the Gridview, it should show up as an editable row just like any of the other rows.

Asp Update Panel - Details View Updates Underlying GridView doesn't

I have an Asp.net gridview on a page, when a buttonfield is clicked (instead of select) it opens a popup form (my popup not asp's) which contains the detail of the gridview row displayed in an editable DetailsView. (This approach has been used because the grid contains 20 wideish columns and it is easier to edit / update in the detailsView format) The DetailsView takes any amendmends and writes them back to the table, fine, but the underlying Gridview is never visually updated unless of course the page is completely reloaded (I tried to use the Windows.reload function but I get the annoying winddows is trying to reopen etc, error, so that is useless). I am trying to discover the best way to get the gridview to refresh it's data. I have placed via a Me.ClientScript.RegisterStartupScript(Me.GetType() eyc, an alert box to communicate when the 'update steps' of the detailsview fires so I could interject a gridview.ReBind() but because the detailsview is held within the update panel the, for example, Protected Sub DetailsView2_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles DetailsView2.ItemUpdated functions, and the roqwupdatind and so one, do not appear to fire. (they must do of course else the table would not be updating. MY problem is how do I 'communicate with update events within the panel contained detailsviews so that I can cause a reBind to occur on the main gridview. Any Ideas would be appreciated. Thank you
Detail view have an event called
protected void dvEbook_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
Response.Redirect(Request.RawUrl,false);
}
where dvEbook is my detail view.
It will call a same page.
I hope it will work for you.
Thanks

Grid View View state

I'm Using a GridView in my Application.
My GridView has a checkbox column and when the checkbox is clicked, the page is redirected to an other form.
When I return to previous from, nothing comes and GridView data is lost.
I used session and it's working fine, but I want that checkbox which was clicked to also maintain there state.
Is there better method to do this?
Upon redirection to the next page, Iterate your Gridview to update your Datatable with a checked checkbox column and then set that datatable to a session variable. I am assuming that you are already storing your source Datatable to a session.
Sorry, but no other magic work around !!!

Gridview RowUpdating event not firing

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.
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.
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.
Set the GridView EnableViewState property to true.
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
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.
This fixed the issue for me:
If Not Master.Page.IsPostBackEventControlRegistered Then
'logic to bind data
End If
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.
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.
Find the validators that are not within the edit item template of this gridview row, set them to disabled in page_load.
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.
Profit.
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.
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.

Resources