kengkit
asked on
problem when i using edit / delete button in datagrid
hello guys, i was using visual studio.net 2005 to develop asp.net application
My datagrid embedded with the [edit] and [delete] button in order to help me update my database!
but the error msg come up no matter i clicked either [edit] or [delete] button in my datagrid. WHY?? it's super urgent.. pls help me to solve tis problem
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- --------
my datagrid code
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- --------
<asp: DataGrid ID="MyDataGrid" runat="server" AutoGenerateColumns="False " BackColor="LightSalmon"
OnUpdateCommand="MyDataGri d_Update" OnCancelCommand="MyDataGri d_Cancel"
OnEditCommand="MyDataGrid_ Edit" OnDeleteCommand="Delete_It em"
OnPageIndexChanged="MyData Grid_PageI ndexChange d"
DataKeyField="UID" AllowSorting="True">
<Columns>
<asp: EditCommandColumn ButtonType="PushButton" CancelText="Cancel" EditText="Edit"
UpdateText="Update"></asp: EditComman dColumn>
<EditItemTemplate>
<asp: DropDownList runat="server" id="edit_status">
<asp: ListItem>ACTIVE</asp: ListItem>
<asp: ListItem>PENDING</asp: ListItem>
</asp: DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn ButtonType="PushButton" CommandName="Delete" Text="Delete"></asp:Button Column>
</Columns>
</asp: DataGrid>
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- --------
Error msg while i pressed [edit] or [delete] button in datagrid.
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- --------
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="tru e"/> in configuration or <%@ Page EnableEventValidation="tru e" %> 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.Regist erForEvent Validation method in order to register the postback or callback data for validation.
My datagrid embedded with the [edit] and [delete] button in order to help me update my database!
but the error msg come up no matter i clicked either [edit] or [delete] button in my datagrid. WHY?? it's super urgent.. pls help me to solve tis problem
--------------------------
my datagrid code
--------------------------
<asp: DataGrid ID="MyDataGrid" runat="server" AutoGenerateColumns="False
OnUpdateCommand="MyDataGri
OnEditCommand="MyDataGrid_
OnPageIndexChanged="MyData
DataKeyField="UID" AllowSorting="True">
<Columns>
<asp: EditCommandColumn ButtonType="PushButton" CancelText="Cancel" EditText="Edit"
UpdateText="Update"></asp:
<EditItemTemplate>
<asp: DropDownList runat="server" id="edit_status">
<asp: ListItem>ACTIVE</asp: ListItem>
<asp: ListItem>PENDING</asp: ListItem>
</asp: DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn ButtonType="PushButton" CommandName="Delete" Text="Delete"></asp:Button
</Columns>
</asp: DataGrid>
--------------------------
Error msg while i pressed [edit] or [delete] button in datagrid.
--------------------------
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="tru
Can you post your sub routine for delete and Edit command?
ASKER
Sub MyDataGrid_Edit(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)
BindGrid()
End Sub
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -----
Sub MyDataGrid_Delete(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
Dim UID As String
Dim success As Boolean
UID = MyDataGrid.DataKeys(e.Item .ItemIndex )
If UID = "U100001" Then
log.MessageBox("Access Denied! This is administrator account!")
Exit Sub
End If
success = log.delete_member(UID)
If success = True Then
log.MessageBox("Record Deleted")
Else
log.MessageBox("Error")
End If
BindGrid()
End Sub
MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)
BindGrid()
End Sub
--------------------------
Sub MyDataGrid_Delete(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
Dim UID As String
Dim success As Boolean
UID = MyDataGrid.DataKeys(e.Item
If UID = "U100001" Then
log.MessageBox("Access Denied! This is administrator account!")
Exit Sub
End If
success = log.delete_member(UID)
If success = True Then
log.MessageBox("Record Deleted")
Else
log.MessageBox("Error")
End If
BindGrid()
End Sub
in this line in Edit : MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex) is your item Numeric String? or integer?
for this line
MyDataGrid.DataKeys(e.Item .ItemIndex ) change it to myDataGrid.DataKeys.Item( e.Item.ItemIndex )
and about your BindGrid(), it seems that your data contains something which is invalid for textboxes
set the EnableEventValidation="fal se" to false and add it to your page directive
I mean the problem appear when you select data from database to fillout your textboxes by your data in edit command
this is a very easy sample which works with sql
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql Client" %>
<Script Runat="Server">
Sub Page_Load( s As Object, e As EventArgs )
If Not isPostBack Then
BindData
End If
End Sub
Sub BindData
Dim myConnection As SQLConnection
Dim myCommand As SQLCommand
myConnection = New SQLConnection( "Server=Localhost;uid=sa;p wd=;Databa se=Northwi nd" )
myCommand = New SQLCommand( _
"Select * from Products", myConnection )
myConnection.Open()
myDataGrid.DataSource = myCommand.ExecuteReader()
myDataGrid.DataBind()
myConnection.Close()
End Sub
Sub editProduct( s As Object, e As DataGridCommandEventArgs )
myDataGrid.EditItemIndex = e.Item.ItemIndex
BindData
End Sub
Sub cancelEdit( s As Object, e As DataGridCommandEventArgs )
myDataGrid.EditItemIndex = -1
BindData
End Sub
Sub updateProduct( s As Object, e As DataGridCommandEventArgs )
' Get New Values
Dim productName As TextBox
productName = e.Item.Cells( 2 ).Controls( 0 )
Dim unitPrice As TextBox
unitPrice = e.Item.Cells( 3 ).Controls( 0 )
' Update Database
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim sqlString As String
myConnection = New SqlConnection( "Server=Localhost;uid=sa;p wd=hosnieh 60;Databas e=Northwin d" )
sqlString = "Update Products Set ProductName = " & _
"@productName, UnitPrice=@unitPrice Where ProductID= @productID"
myCommand = New SQLCommand( sqlString, myConnection )
myCommand.Parameters.Add( _
New SQLParameter( "@ProductName", SqlDbType.NVarChar, 80 ))
myCommand.Parameters( "@ProductName" ).Value = productName.Text
myCommand.Parameters.Add( _
New SQLParameter( "@unitprice", SqlDbType.Money ))
myCommand.Parameters( "@unitPrice" ).Value = _
cType( unitPrice.Text, Decimal )
myCommand.Parameters.Add(N ew SQLParameter( "@productID", SqlDbType.INT ))
myCommand.Parameters( "@productID" ).Value = myDataGrid.DataKeys.Item( e.Item.ItemIndex )
myConnection.Open()
myCommand.ExecuteNonQuery
myConnection.Close()
myDataGrid.EditItemIndex = -1
BindData
End Sub
</Script>
<html>
<head><title>DataGrid</tit le></head>
<body>
<form Runat="Server">
<asp:DataGrid
id="myDataGrid"
cellpadding=3
AutoGenerateColumns="False "
DataKeyField="ProductID"
OnEditCommand="editProduct "
OnCancelCommand="cancelEdi t"
OnUpdateCommand="updatePro duct"
Runat="Server">
<Columns>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update" />
<asp:BoundColumn HeaderText="Product ID"
DataField="ProductID" ReadOnly="True"/>
<asp:BoundColumn HeaderText="Product Name"
DataField="ProductName" />
<asp:BoundColumn HeaderText="Price"
DataField="UnitPrice" DataFormatString="{0:c}" />
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
for this line
MyDataGrid.DataKeys(e.Item
and about your BindGrid(), it seems that your data contains something which is invalid for textboxes
set the EnableEventValidation="fal
I mean the problem appear when you select data from database to fillout your textboxes by your data in edit command
this is a very easy sample which works with sql
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql
<Script Runat="Server">
Sub Page_Load( s As Object, e As EventArgs )
If Not isPostBack Then
BindData
End If
End Sub
Sub BindData
Dim myConnection As SQLConnection
Dim myCommand As SQLCommand
myConnection = New SQLConnection( "Server=Localhost;uid=sa;p
myCommand = New SQLCommand( _
"Select * from Products", myConnection )
myConnection.Open()
myDataGrid.DataSource = myCommand.ExecuteReader()
myDataGrid.DataBind()
myConnection.Close()
End Sub
Sub editProduct( s As Object, e As DataGridCommandEventArgs )
myDataGrid.EditItemIndex = e.Item.ItemIndex
BindData
End Sub
Sub cancelEdit( s As Object, e As DataGridCommandEventArgs )
myDataGrid.EditItemIndex = -1
BindData
End Sub
Sub updateProduct( s As Object, e As DataGridCommandEventArgs )
' Get New Values
Dim productName As TextBox
productName = e.Item.Cells( 2 ).Controls( 0 )
Dim unitPrice As TextBox
unitPrice = e.Item.Cells( 3 ).Controls( 0 )
' Update Database
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim sqlString As String
myConnection = New SqlConnection( "Server=Localhost;uid=sa;p
sqlString = "Update Products Set ProductName = " & _
"@productName, UnitPrice=@unitPrice Where ProductID= @productID"
myCommand = New SQLCommand( sqlString, myConnection )
myCommand.Parameters.Add( _
New SQLParameter( "@ProductName", SqlDbType.NVarChar, 80 ))
myCommand.Parameters( "@ProductName" ).Value = productName.Text
myCommand.Parameters.Add( _
New SQLParameter( "@unitprice", SqlDbType.Money ))
myCommand.Parameters( "@unitPrice" ).Value = _
cType( unitPrice.Text, Decimal )
myCommand.Parameters.Add(N
myCommand.Parameters( "@productID" ).Value = myDataGrid.DataKeys.Item( e.Item.ItemIndex )
myConnection.Open()
myCommand.ExecuteNonQuery
myConnection.Close()
myDataGrid.EditItemIndex = -1
BindData
End Sub
</Script>
<html>
<head><title>DataGrid</tit
<body>
<form Runat="Server">
<asp:DataGrid
id="myDataGrid"
cellpadding=3
AutoGenerateColumns="False
DataKeyField="ProductID"
OnEditCommand="editProduct
OnCancelCommand="cancelEdi
OnUpdateCommand="updatePro
Runat="Server">
<Columns>
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update" />
<asp:BoundColumn HeaderText="Product ID"
DataField="ProductID" ReadOnly="True"/>
<asp:BoundColumn HeaderText="Product Name"
DataField="ProductName" />
<asp:BoundColumn HeaderText="Price"
DataField="UnitPrice" DataFormatString="{0:c}" />
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
ASKER
1) There is nothing happen when i press [edit button] if i add [EnableEventValidation="fa lse"]
2) Please note tat everything go fine, display properly in my datagrid until i press [edit] or [delete] button
3) Please note tat the edit button is use for update value of [dropdownlist] in my datagrid, and the value of [dropdownlist] is return by [databind]
<%@ Page Language="VB" EnableEventValidation="fal se" AutoEventWireup="false" CodeFile="member_control.a spx.vb" Inherits="member_control" %>
<html>
<head>
<title>Search Member</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="MyDataGrid" runat="server"
AutoGenerateColumns="false "
OnUpdateCommand="MyDataGri d_Update"
OnCancelCommand="MyDataGri d_Cancel"
OnEditCommand="MyDataGrid_ Edit"
OnPageIndexChanged="MyData Grid_PageI ndexChange d"
OnDeleteCommand="MyDataGri d_Delete"
DataKeyField="UID"
CellPadding="3">
<HeaderStyle BackColor="InactiveCaption Text" Font-Bold="True" />
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel" EditText="Edit" UpdateText="Update"></asp: EditComman dColumn>
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Container.DataIte m("status" )%>' ID="Label1" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" id="edit_status">
<asp:ListItem>ACTIVE</asp: ListItem>
<asp:ListItem>PENDING</asp :ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn ButtonType="PushButton" CommandName="Delete" Text="Delete"></asp:Button Column>
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>
2) Please note tat everything go fine, display properly in my datagrid until i press [edit] or [delete] button
3) Please note tat the edit button is use for update value of [dropdownlist] in my datagrid, and the value of [dropdownlist] is return by [databind]
<%@ Page Language="VB" EnableEventValidation="fal
<html>
<head>
<title>Search Member</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="MyDataGrid" runat="server"
AutoGenerateColumns="false
OnUpdateCommand="MyDataGri
OnCancelCommand="MyDataGri
OnEditCommand="MyDataGrid_
OnPageIndexChanged="MyData
OnDeleteCommand="MyDataGri
DataKeyField="UID"
CellPadding="3">
<HeaderStyle BackColor="InactiveCaption
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel" EditText="Edit" UpdateText="Update"></asp:
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Container.DataIte
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" id="edit_status">
<asp:ListItem>ACTIVE</asp:
<asp:ListItem>PENDING</asp
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn ButtonType="PushButton" CommandName="Delete" Text="Delete"></asp:Button
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>
ASKER
for your convenience, I have uploaded my source code to:
http://www.aams.com.my/source.zip
pls help me to solve tis problem.. very urgent...
http://www.aams.com.my/source.zip
pls help me to solve tis problem.. very urgent...
instead of EnableEventValidation="fal se" : ValidateRequest="false"
then , just maybe when you call BidGrid() because in sub routine it seems there is no problem.
what is your version of asp.net , is it 1.1 or 2.0 ?
then , just maybe when you call BidGrid() because in sub routine it seems there is no problem.
what is your version of asp.net , is it 1.1 or 2.0 ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.