Actually I did post this problem before. I want to do the sorting, and it is working, but got the ERROR for all buttons - delete, update, add, select. The errors -Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> 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.
The Sorting:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'If Not IsPostBack Then
BindGridView()
'End If
End Sub
Private Sub BindGridView()
' Code to retrieve records from database and fill it in a DataTable and Bind it to GridView
Dim dt As DataTable = New DataTable()
Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")
Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Sysdep", conn)
conn.Open()
da.Fill(dt)
da.Dispose()
conn.Close()
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
Dim dtSortTable As DataTable = TryCast(GridView1.DataSource, DataTable)
If dtSortTable IsNot Nothing Then
Dim dvSortedView As New DataView(dtSortTable)
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection)
GridView1.DataSource = dvSortedView
GridView1.DataBind()
'BindGridView()
End If
End Sub
Private Function getSortDirectionString(ByVal sortDireciton As SortDirection) As String
Dim newSortDirection As String = [String].Empty
If sortDireciton = SortDirection.Ascending Then
newSortDirection = "ASC"
Else
newSortDirection = "DESC"
End If
Return newSortDirection
End Function
But if I do this on the Load: all buttons are working, no errors, but sorting is not working.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridView()
End If
End Sub
This original:
Imports System.Data
Imports System.Data.OleDb
Partial Class SystemManager
Inherits System.Web.UI.Page
Dim con1 As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'If Not IsPostBack Then
BindGridView()
'End If
End Sub
Private Sub BindGridView()
' Code to retrieve records from database and fill it in a DataTable and Bind it to GridView
Dim dt As DataTable = New DataTable()
Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Users\user\Desktop\honor.mdb")
Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Sysdep", conn)
conn.Open()
da.Fill(dt)
da.Dispose()
conn.Close()
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
BindGridView()
End Sub
Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
BindGridView()
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
BindGridView()
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim sqlupdate As String
sqlupdate = "UPDATE sysdep SET sysaccount= @sysaccount, syspw= @syspw WHERE id= @id"
Dim cmd1 As New OleDbCommand(sqlupdate, con1)
cmd1.Parameters.Add(New OleDbParameter("@sysaccount", CType(GridView1.Rows(e.RowIndex).FindControl("TextBox1"), TextBox).Text.ToString()))
cmd1.Parameters.Add(New OleDbParameter("@syspw", CType(GridView1.Rows(e.RowIndex).FindControl("TextBox2"), TextBox).Text.ToString()))
cmd1.Parameters.Add(New OleDbParameter("@id", GridView1.DataKeys(e.RowIndex).Value.ToString()))
Try
con1.Open()
cmd1.ExecuteNonQuery()
Catch ex As OleDbException
MsgBox(ex.Message, MsgBoxStyle.Critical, "Web")
Catch ex As InvalidOperationException
MsgBox(ex.Message, MsgBoxStyle.Critical, "Web")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Web")
End Try
con1.Close()
GridView1.EditIndex = -1
BindGridView()
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim sqldelete As String
sqldelete = "DELETE FROM sysdep WHERE id=" & GridView1.DataKeys(e.RowIndex).Value.ToString()
Dim cmd2 As New OleDbCommand(sqldelete, con1)
Try
con1.Open()
cmd2.ExecuteNonQuery()
Catch ex As OleDbException
MsgBox(ex.Message, MsgBoxStyle.Critical, "Web")
Catch ex As InvalidOperationException
MsgBox(ex.Message, MsgBoxStyle.Critical, "Web")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Web")
End Try
con1.Close()
BindGridView()
End Sub
Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
Dim dtSortTable As DataTable = TryCast(GridView1.DataSource, DataTable)
If dtSortTable IsNot Nothing Then
Dim dvSortedView As New DataView(dtSortTable)
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection)
GridView1.DataSource = dvSortedView
GridView1.DataBind()
End If
End Sub
Private Function getSortDirectionString(ByVal sortDireciton As SortDirection) As String
Dim newSortDirection As String = [String].Empty
If sortDireciton = SortDirection.Ascending Then
newSortDirection = "ASC"
Else
newSortDirection = "DESC"
End If
Return newSortDirection
End Function
Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim sqlinsert As String
sqlinsert = "INSERT INTO sysdep (sysaccount,syspw)" & _
"VALUES(@sysaccount, @syspw)"
Dim cmd As New OleDbCommand(sqlinsert, con1)
cmd.Parameters.Add(New OleDbParameter("@sysaccount", TextBox1.Text))
cmd.Parameters.Add(New OleDbParameter("@syspw", TextBox2.Text))
Try
con1.Open()
cmd.ExecuteNonQuery()
Catch ex As OleDbException
MsgBox(ex.Message, MsgBoxStyle.Critical, "Web")
Catch ex As InvalidOperationException
MsgBox(ex.Message, MsgBoxStyle.Critical, "Web")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Web")
End Try
con1.Close()
BindGridView()
End Sub
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.Text = ""
TextBox1.Focus()
TextBox1.Text = ""
End Sub
Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
End Class
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataKeyNames="id" GridLines="Vertical" Width="739px" >
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<Columns>
<asp:TemplateField ShowHeader="False" HeaderText="Edit">
<EditItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:Button ID="Button2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" CausesValidation="False"
CommandName="Select" Text="Select" />
<asp:Button ID="Button1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
<asp:Button ID="Button3" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure to Delete the record?')"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id"
Visible="False">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="System Manager" SortExpression="sysaccount">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("sysaccount") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("sysaccount") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" SortExpression="syspw">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("syspw") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("syspw") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Log/Time" SortExpression="log">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("log") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("log") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#DCDCDC" />
</asp:GridView>
<br />
<asp:Label ID="Label2" runat="server"
style="font-family: Times New Roman; font-size: large; font-weight: 700" Text="System Manager"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="Button4" runat="server"
style="font-family: Time New Roman; font-size: large; font-weight: 700" Text="Add" />
<asp:Button ID="Button5" runat="server"
style="font-family: Time New Roman; font-size: large; font-weight: 700" Text="Cancel" />
<asp:Button ID="Button6" runat="server"
style="font-family: Time New Roman; font-size: large; font-weight: 700" Text="Save" />
</form>
Really don't know how to fix?