|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Datagrid cancel command does not work when adding new recordIf I edit a record then press the cancel button this works fine. If I add a new record, then change my mind before saving and press cancel, the cancel does not work and an invalid record is saved to the dataset. Any ideas on what I need to do to make cancel work with new records? Thanks in advance. Code below: Private Sub UserDataGrid_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles UserDataGrid.CancelCommand UserDataGrid.EditItemIndex = -1 UserDataGrid.DataBind() End Sub Private Sub UserDataGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles UserDataGrid.UpdateCommand Dim UserID, UserName, UserNetwork, UserType As String ' Gets the value of the key field of the row being updated Dim key As String = UserDataGrid.DataKeys(e.Item.ItemIndex).ToString() ' Gets get the value of the controls (textboxes) that the user ' updated. The DataGrid columns are exposed as the Cells collection. ' Each cell has a collection of controls. In this case, there is only one ' control in each cell -- a TextBox control. To get its value, ' you copy the TextBox to a local instance (which requires casting) ' and extract its Text property. ' ' The first column -- Cells(0) -- contains the Update and Cancel buttons. Dim tb As TextBox ' Gets the value of the TextBox control in the columns tb = CType(e.Item.Cells(2).Controls(0), TextBox) UserID = tb.Text tb = CType(e.Item.Cells(3).Controls(0), TextBox) UserName = tb.Text Dim ddl As DropDownList = e.Item.FindControl("cboNetwork") Dim ddlValue As String = ddl.SelectedValue UserNetwork = ddlValue Dim ddl1 As DropDownList = e.Item.FindControl("cboPermissions") Dim ddl1Value As String = ddl1.SelectedValue UserType = ddl1Value ' Finds the row in the dataset table that matches the ' one the user updated in the grid. This example uses a ' special Find method defined for the typed dataset, which ' returns a reference to the row. Dim r As UserDataSet.UsersRow r = UserDataSet1.Users.FindByUserID(key) ' Updates the dataset table. r.UserID = UserID r.UserName = UserName r.UserNetwork = UserNetwork r.UserType = UserType ' Calls a SQL statement to update the database from the dataset UserDataAdapter.Update(UserDataSet1) ' Takes the DataGrid row out of editing mode UserDataGrid.EditItemIndex = -1 ' Refreshes the grid UserDataGrid.DataBind() End Sub Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click 'create a new blank row Dim dr As DataRow = Me.UserDataSet1.Users.NewRow 'fill the fields out with default text dr("UserID") = "(Enter Network ID)" dr("UserName") = "(Enter User's Full Name)" dr("UserNetwork") = "rcuknet" dr("UserType") = "Edit" 'put the row at the top of the dataset Me.UserDataSet1.Users.Rows.InsertAt(dr, 0) 'update the data set Session("UserDataSet1") = UserDataSet1 UserDataGrid.EditItemIndex = 0 UserDataGrid.DataBind() End Sub In the UserDataGrid_CancelCommand call UserDataSet1.RejectChanges() and at
the end of the UserDataGrid_UpdateCommand call UserDataSet1.AcceptChanges() Show quoteHide quote "Julia B" wrote: > I've got a datagrid with a add, update/cancel and edit commands. > > If I edit a record then press the cancel button this works fine. If I add a > new record, then change my mind before saving and press cancel, the cancel > does not work and an invalid record is saved to the dataset. Any ideas on > what I need to do to make cancel work with new records? > > Thanks in advance. > > Code below: > > Private Sub UserDataGrid_CancelCommand(ByVal source As Object, ByVal e As > System.Web.UI.WebControls.DataGridCommandEventArgs) Handles > UserDataGrid.CancelCommand > UserDataGrid.EditItemIndex = -1 > UserDataGrid.DataBind() > End Sub > > Private Sub UserDataGrid_UpdateCommand(ByVal source As Object, ByVal e As > System.Web.UI.WebControls.DataGridCommandEventArgs) Handles > UserDataGrid.UpdateCommand > Dim UserID, UserName, UserNetwork, UserType As String > > ' Gets the value of the key field of the row being updated > Dim key As String = UserDataGrid.DataKeys(e.Item.ItemIndex).ToString() > > ' Gets get the value of the controls (textboxes) that the user > ' updated. The DataGrid columns are exposed as the Cells collection. > ' Each cell has a collection of controls. In this case, there is > only one > ' control in each cell -- a TextBox control. To get its value, > ' you copy the TextBox to a local instance (which requires casting) > ' and extract its Text property. > ' > ' The first column -- Cells(0) -- contains the Update and Cancel > buttons. > Dim tb As TextBox > > ' Gets the value of the TextBox control in the columns > tb = CType(e.Item.Cells(2).Controls(0), TextBox) > UserID = tb.Text > tb = CType(e.Item.Cells(3).Controls(0), TextBox) > UserName = tb.Text > Dim ddl As DropDownList = e.Item.FindControl("cboNetwork") > Dim ddlValue As String = ddl.SelectedValue > UserNetwork = ddlValue > Dim ddl1 As DropDownList = e.Item.FindControl("cboPermissions") > Dim ddl1Value As String = ddl1.SelectedValue > UserType = ddl1Value > > ' Finds the row in the dataset table that matches the > ' one the user updated in the grid. This example uses a > ' special Find method defined for the typed dataset, which > ' returns a reference to the row. > Dim r As UserDataSet.UsersRow > r = UserDataSet1.Users.FindByUserID(key) > > ' Updates the dataset table. > r.UserID = UserID > r.UserName = UserName > r.UserNetwork = UserNetwork > r.UserType = UserType > > ' Calls a SQL statement to update the database from the dataset > UserDataAdapter.Update(UserDataSet1) > > ' Takes the DataGrid row out of editing mode > UserDataGrid.EditItemIndex = -1 > > ' Refreshes the grid > UserDataGrid.DataBind() > > End Sub > > Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles cmdAdd.Click > 'create a new blank row > Dim dr As DataRow = Me.UserDataSet1.Users.NewRow > 'fill the fields out with default text > dr("UserID") = "(Enter Network ID)" > dr("UserName") = "(Enter User's Full Name)" > dr("UserNetwork") = "rcuknet" > dr("UserType") = "Edit" > 'put the row at the top of the dataset > Me.UserDataSet1.Users.Rows.InsertAt(dr, 0) > 'update the data set > Session("UserDataSet1") = UserDataSet1 > UserDataGrid.EditItemIndex = 0 > UserDataGrid.DataBind() > End Sub > Thanks that works perfectly!
Julia Show quoteHide quote "Phillip Williams" wrote: > In the UserDataGrid_CancelCommand call UserDataSet1.RejectChanges() and at > the end of the UserDataGrid_UpdateCommand call UserDataSet1.AcceptChanges() > > -- > HTH, > Phillip Williams > http://www.societopia.net > http://www.webswapp.com > > > "Julia B" wrote: > > > I've got a datagrid with a add, update/cancel and edit commands. > > > > If I edit a record then press the cancel button this works fine. If I add a > > new record, then change my mind before saving and press cancel, the cancel > > does not work and an invalid record is saved to the dataset. Any ideas on > > what I need to do to make cancel work with new records? > > > > Thanks in advance. > > > > Code below: > > > > Private Sub UserDataGrid_CancelCommand(ByVal source As Object, ByVal e As > > System.Web.UI.WebControls.DataGridCommandEventArgs) Handles > > UserDataGrid.CancelCommand > > UserDataGrid.EditItemIndex = -1 > > UserDataGrid.DataBind() > > End Sub > > > > Private Sub UserDataGrid_UpdateCommand(ByVal source As Object, ByVal e As > > System.Web.UI.WebControls.DataGridCommandEventArgs) Handles > > UserDataGrid.UpdateCommand > > Dim UserID, UserName, UserNetwork, UserType As String > > > > ' Gets the value of the key field of the row being updated > > Dim key As String = UserDataGrid.DataKeys(e.Item.ItemIndex).ToString() > > > > ' Gets get the value of the controls (textboxes) that the user > > ' updated. The DataGrid columns are exposed as the Cells collection. > > ' Each cell has a collection of controls. In this case, there is > > only one > > ' control in each cell -- a TextBox control. To get its value, > > ' you copy the TextBox to a local instance (which requires casting) > > ' and extract its Text property. > > ' > > ' The first column -- Cells(0) -- contains the Update and Cancel > > buttons. > > Dim tb As TextBox > > > > ' Gets the value of the TextBox control in the columns > > tb = CType(e.Item.Cells(2).Controls(0), TextBox) > > UserID = tb.Text > > tb = CType(e.Item.Cells(3).Controls(0), TextBox) > > UserName = tb.Text > > Dim ddl As DropDownList = e.Item.FindControl("cboNetwork") > > Dim ddlValue As String = ddl.SelectedValue > > UserNetwork = ddlValue > > Dim ddl1 As DropDownList = e.Item.FindControl("cboPermissions") > > Dim ddl1Value As String = ddl1.SelectedValue > > UserType = ddl1Value > > > > ' Finds the row in the dataset table that matches the > > ' one the user updated in the grid. This example uses a > > ' special Find method defined for the typed dataset, which > > ' returns a reference to the row. > > Dim r As UserDataSet.UsersRow > > r = UserDataSet1.Users.FindByUserID(key) > > > > ' Updates the dataset table. > > r.UserID = UserID > > r.UserName = UserName > > r.UserNetwork = UserNetwork > > r.UserType = UserType > > > > ' Calls a SQL statement to update the database from the dataset > > UserDataAdapter.Update(UserDataSet1) > > > > ' Takes the DataGrid row out of editing mode > > UserDataGrid.EditItemIndex = -1 > > > > ' Refreshes the grid > > UserDataGrid.DataBind() > > > > End Sub > > > > Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As > > System.EventArgs) Handles cmdAdd.Click > > 'create a new blank row > > Dim dr As DataRow = Me.UserDataSet1.Users.NewRow > > 'fill the fields out with default text > > dr("UserID") = "(Enter Network ID)" > > dr("UserName") = "(Enter User's Full Name)" > > dr("UserNetwork") = "rcuknet" > > dr("UserType") = "Edit" > > 'put the row at the top of the dataset > > Me.UserDataSet1.Users.Rows.InsertAt(dr, 0) > > 'update the data set > > Session("UserDataSet1") = UserDataSet1 > > UserDataGrid.EditItemIndex = 0 > > UserDataGrid.DataBind() > > End Sub > >
return url value
Display a record - several questions Show ***** only if have a password How do I debug apsx pages? Validation inside composite controls for ASP .NET 2.0 problem traping event in child control How to set default values for fields in detailsview style question must click button twice for event to fire Webcontrol Alignment problem |
|||||||||||||||||||||||