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.
Related
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.
I know there are similar questions about this issue (for example, here and here) but no one results helpful for my problem.
I have a ListView control showing all the users registered in the database and there is a CheckBox for each user shown if the user is approved or not, and I wanna save the changes directly when the CheckBox's Checked property changes.
I know it's not correct to add the event handler on the ListView_ItemDataBound, because after the CheckBox's AutoPostback there is not a new binding, thus the event handler get lost. On the other hand, I can't append the method directly on the ASPX file because this way I can't know which user is affected by the change (at least, I think I can't).
Any sugestion?
Thanks a lot
You have a couple of options.
It's possible that the ListView.ItemCommand event might fire. I'm not sure, though, because the documentation just specifies buttons. You might want to experiment.
The other option would be to harness the ListView.ItemCreated command. I believe that this always runs, regardless of whether the ListView is bound or not, because the item always has to be created, even if it's from ViewState. What you would do in the event handler for that event would be to attach an event handler to the CheckBox Click or CheckChanged event (I forget what the server-side name is for a CheckBox state change event).
So i ran into the same problem. i'm curious, on your page load are you checking if its a postback?
if(!Post.IsPostBack){
//normal page load
}
If you don't have that check, it will call your page load logic, in my case it was resetting the checkbox everytime with my data object.
On the aspx on the checkbox OnCheckedChanged="ckbNameOfCheckbox_CheckedChanged" AutoPostBack="true"
In the code behind
Protected Sub ckbNameOfCheckbox_CheckedChanged(sender As Object, e As EventArgs)
Dim chkBox As CheckBox = CType(sender, CheckBox)
' Gets the item that contains the CheckBox object.
Dim item As ListViewDataItem = CType(chkBox.Parent, ListViewDataItem)
NameOfTheListView.UpdateItem(item.DisplayIndex, sender.Checked)
End Sub
This is probably something fundamental and stupid that I've missed, but the various workarounds are causing me such huge headaches that really, I need to understand the basic problem here in order to find the best solution.
I have a usercontrol which contains a drop-down list.
<asp:DropDownList ID="ddlOrderStatus" AutoPostBack="true" runat="server" CssClass="textbox">
<asp:ListItem value="8">Pending</asp:ListItem>
<asp:ListItem value="9">On Hold</asp:ListItem>
<asp:ListItem Value="11">Complete</asp:ListItem>
<asp:ListItem Value="12">Cancelled</asp:ListItem>
</asp:DropDownList>
It does nothing in its Page_Load event.
This is in turn contained in a page which, in it's Page_Load event does some databinding on some repeater controls on the page but doesn't touch the control containing the ddl. There is no ajax on the page.
The dropdownlist is - clearly - set to autopostback = true. The postback code looks like this:
Private Sub ddlOrderStatus_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOrderStatus.SelectedIndexChanged
Dim ddl As DropDownList = DirectCast(sender, DropDownList)
Dim currentsorting As String = Request.QueryString("sort")
Dim currentpaging As String = Request.QueryString("page")
Response.Redirect(String.Format("~/admin/orders/Manage/0?sort={0}&page={1}&status={2}", currentsorting, currentpaging, ddl.SelectedValue))
End Sub
So when the ddl is changed, the page actually does a double postback. I see no way to avoid this as I need to do a postback to get the value in the ddl and then another postback to get some new data from the server based on that value.
Anyway, the long and the short of it is that when you do this the dropdownlist fails to maintain its state. So you select a value, the code does the job it's supposed to - gets the selected value then posts back again returning the new expected set of data - and then on loading again the dropdownlist is back in its default form.
This is a pernicious problem for me because of that double-postback: if I try and set the value of the ddl to equal what's in the querystring I have, effecitvely, set it to that value permanently because Page_Load - where the value is set - occurs before events are processed and so the value stays unchanged. I tried moving the code which changed the selectedvalue of the ddl into Render, but it seemed that although the selected value had changed as far as the user could see, the selected value was still the default value during processing and was treated as such.
I also tried setting the selectedValue in session and then clearing it as soon as it was populated. That worked for some scenarios, but unfortunately this page also contains some javascript that can cause it to post back - and you can't set session from client-side javascript. So that idea had to go out the window.
I'm at a loss here - this apparently simple problem has eaten a whole day of my time. Can anyone either tell me why state isn't being maintained on this control and/or suggest a way I get it to show the right value after postback without turning the selection into a permanent one?
Cheers,
Matt
In your page load:
// Only set the selected order status when the page first loads, but not on postbacks.
if( !Page.IsPostback )
{
ddlOrderStatus.SelectedValue = Request.QueryString("status");
}
Footnote:
I see no way to avoid this as I need
to do a postback to get the value in
the ddl and then another postback to
get some new data from the server
based on that value.
You could do it from javascript
I'll preface this by saying that I do my ASP.NET programming in C# and use GridViews, but the following should apply to your situation regardless:
Is it necessary for you to use the query string to hold this data? If you've declared your paging and sorting controls on your ASPX page, you can access them and their values directly in your codebehind (as Greg demonstrated in the above code by not performing a FindControl call). Has the user control setup prevented you from being able to u this? Are you instantiating the user control containing this drop-down list on a page, and then trying to access the value of the drop-down list from the page's codebehind itself, rather than the user control's code behind?
I am trying to set a default button in my ASPX page. I have a master page so the form is there. I have a panel in the content page that holds a table that organizes a number of textboxes, dropdowns and other inputs. The last row of the table holds some buttons, one of which I want to be the default button. After doing some research, I have tried the following with no success.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
pnlHolder.DefaultButton = cmdSearchJob.ClientID
I have also tried
pnlHolder.DefaultButton = cmdSearchJob.UniqueID
and
Dim cmdDef As Button = pnlHolder.FindControl("cmdSearchJob")
pnlHolder.DefaultButton = cmdDef.UniqueID
but both throw the exception "The DefaultButton of 'pnlHolder' must be the ID of a control of type IButtonControl.".
I have seen some Javascript solutions, but was hoping to just be able to set the defaultButton for the panel.
Try setting the DefaultButton of the parent Form.
C#:
this.Page.Form.DefaultButton = cmdSearchJob.UniqueID;
VB?:
me.Page.Form.DefaultButton = cmdSearchJob.UniqueID
Similar issue here: Allow Enter key to login in asp.net?
Try setting it to:
cmdSearchJob.ID
The panel will call FindControl to get the Client ID itself
Can you set this inside the control on the front-end?
<asp:Panel id="pnlHolder" DefaultButton="cmdSearchJob">
<asp:Button id="cmdSearchJob" runat="server" Text="Search" />
</asp:Panel>
Also, this may worth knowing, what type of object is cmdSearchJob? Is it a standard asp.net button control?
Finally found what it was. Tried adding another button with no CSS, etc. that just popped up a javacsript alert() for testing. Was able to set that as the default button when it was outside the table. As the submit button is one of a series of buttons (not a very pretty UI, but user requirements and all that) in the table, I used this additional button and set its style to display:none and have it call the same subroutine in the code behind.
So, short answer, it wasn't seeing it in the table.
Thanks everyone for your input.
I still have no idea why it wouldn't set the button.
I'm looking to be able to click on a gridview row in order to select a row rather than use the select link.
I have the code below which make the row clickable and act like a hyperlink.....
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.DataRow) Then
e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';this.style.textDecoration='underline';")
e.Row.Attributes.Add("onmouseout", "this.style.textDecoration='none';")
e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" + e.Row.RowIndex.ToString()))
End If
End Sub
....but then I get the error message:
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Anyone know how to overcome this?
Thanks,
Thanks for help had found the answer another way...rather than use the visibility property
I set the display property to none and everything worked as is...
<asp:CommandField ShowSelectButton="True" ItemStyle-CssClass="HiddenColumn" HeaderStyle-CssClass="HiddenColumn"/>
.HiddenColumn{display:none;}
Adrian Godong's comment is correct. The easiest way to correct this is to set the GridView to still generate the Select LinkButton, but set its Visible property to false. Finally, set the onclick event to fire a virtual click on the Select LinkButton. This way, the ASP.NET event will come directly from the Select button and you will therefore not be caught be an invalid postback security check.
You could also use "RegisterForEventValidation".
Check this:
RegisterForEventValidation .net 3.5 gridview row, how to?