Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am getting below error.

C#
"Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request."


When i move to next page in the gridview, i am selecting some row in the row.

Based on that, i am selecting dropdown values for editing..?

But, when i select the dropdown i am getting the above error...?

What is the error.???

How to maintain the values in the second page gridview...??

See my code
-----------
ASPX Code
---------
ASP.NET
<asp:GridView ID="GridView1" runat="server" align="center" AllowPaging="true" DataKeyNames="PrNo,RevisionNo"
                                AutoGenerateColumns="False" class="tabulardata"
                                HorizontalAlign="Center" Width="100%">
                                <columns>
                                    <asp:TemplateField HeaderText="Sl.No" ItemStyle-HorizontalAlign="Center"
                                        ItemStyle-Width="4%">
                                        <itemtemplate>
                                          <%# Container.DataItemIndex + 1%>
                                        </itemtemplate>

                                    <%-- <asp:TemplateField HeaderText="CheckAll">
                                       <HeaderTemplate>
                                           <asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelectAll_CheckedChanged"/>
                                       </HeaderTemplate>
                                       <itemtemplate>
                                               <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged"/>
                                     </itemtemplate>--%>
                                  <%-- --%>
                                    <asp:BoundField HeaderText="PrNo" Visible="true" DataField="PrNo"
                                        ItemStyle-HorizontalAlign="center" ReadOnly="true" SortExpression="PrNo">
                                        <itemstyle horizontalalign="Center" width="15%" />

                                    <asp:TemplateField HeaderText="PrNo" Visible="false" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="10%">
                                       <itemtemplate>
                                           <asp:Label ID="lblPrNo" runat="server" Text='<%# Eval("PrNo") %>'>
                                       </itemtemplate>
                                        <HeaderStyle HorizontalAlign="Center" />

                                    <asp:BoundField HeaderText="RevisionNo" Visible="true" DataField="RevisionNo"
                                        ItemStyle-HorizontalAlign="center" ReadOnly="true" SortExpression="RevisionNo">
                                        <itemstyle horizontalalign="Center" width="10%" />

                                    <asp:TemplateField HeaderText="RevisionNo" Visible="false" HeaderStyle-HorizontalAlign="Center" >
                                       <itemtemplate>
                                           <asp:Label ID="lblRevisionNo" runat="server" Text='<%# Eval("RevisionNo") %>'>
                                       </itemtemplate>
                                        <HeaderStyle HorizontalAlign="Center" />

                                    <asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center">
                                        <itemtemplate>
                                            <asp:HyperLink ID="hyperlink1"  onclick="" BorderStyle="None" runat="server" Height="15"
                                                ImageUrl="~/images/EditImage.png"
                                                Width="50%" >
                                        </itemtemplate>
                                        <itemstyle horizontalalign="Center" width="6%" />

                                    <asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center">
                                        <itemtemplate>
                                            <asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete" Height="20"
                                                ImageUrl="~/images/DeleteImage.png" OnClientClick="return confirm('Are you sure you want to delete this PR Number.?');"
                                                CommandArgument='<%# container.DisplayIndex%>'
                                                 Width="20" />
                                        </itemtemplate>
                                        <itemstyle horizontalalign="Center" width="2%" />

                                </columns>
                                <alternatingrowstyle backcolor="#DDDDFF" />



ASPX.VB Code
------------
VB
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
       Try
           If e.CommandName = "Delete" Then
               Dim RowNo As Integer = Convert.ToInt32(e.CommandArgument)
               Dim PrNo As String = GridView1.DataKeys(RowNo)("PrNo").ToString()
               Dim RevisionNo As String = GridView1.DataKeys(RowNo)("RevisionNo").ToString()
               'Delete the record
               If PrNo.ToString IsNot Nothing Then
                   Conn.Open()
                   Qry = ""
                   Qry = "DELETE FROM PMT_PR WHERE PrNo='" & PrNo & "' and RevisionNo='" & RevisionNo & "'"
                   Cmd.Connection = Conn
                   Cmd.CommandText = Qry
                   Cmd.ExecuteNonQuery()
                   Conn.Close()
               End If
               FillGrid()
           End If
           'Session("Mode") = ""
           LblMessage.Visible = True
           LblMessage.Text = "PR No. Deleted"
       Catch ex As Exception
           WriteLogFile(Me.[GetType]().Name, "GridView1_RowCommand()", ex.Message.ToString())
       End Try
   End Sub
   Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
       'for Edit the PR Details to pass the PRNo and RevisionNo in the URL
       Dim Hyper1 As New HyperLink
       If e.Row.RowType = DataControlRowType.DataRow Then
           Hyper1 = e.Row.FindControl("hyperlink1")
           Dim PNo1 As Integer = e.Row.Cells(1).Text.ToString
           Dim RevNo1 As Integer = e.Row.Cells(3).Text.ToString
           Hyper1.NavigateUrl = "PR.aspx?PNo=" & e.Row.Cells(1).Text & "&RevNo=" & e.Row.Cells(3).Text & "&page=" & GridView1.PageIndex + 1
           Hyper1.DataBind()
       End If
   End Sub
 Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
       ResetFormControlValues(Me)
       LblMessage.Text = "PR Number Deleted"
   End Sub
Protected Sub drpRevNo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpRevNo.SelectedIndexChanged
       If Session("Mode") = "E" Or Session("Mode") = "N" Then
           If RecordExists() = False Then
               Dim myscript As String = "alert('Revision Number Already Selected. Select another Revision No.!');"
               System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me.Page, Me.GetType(), "myscript", myscript, True)
               drpRevNo.Focus()
               txtChangeRevNo.ReadOnly = True
               Exit Sub
           End If
       End If
   End Sub
   Function RecordExists() As Boolean
       Dim ExistFlg As Boolean
       ExistFlg = True
       Conn.Open()
       Qry = ""
       Qry = "select * from PMT_PR where PrNo='" & txtPRNo.Text & "' and RevisionNo='" & drpRevNo.SelectedValue & "'"
       Cmd.Connection = Conn
       Cmd.CommandText = Qry
       Rdr = Cmd.ExecuteReader
       If Rdr.HasRows Then
           While Rdr.Read
               ExistFlg = False
               Exit While
           End While
       End If
       Rdr.Close()
       Conn.Close()
       Return ExistFlg
       'End If
   End Function
Posted
Updated 3-Aug-11 5:39am
v4
Comments
Herman<T>.Instance 3-Aug-11 10:00am    
can you show your code?
gani7787 3-Aug-11 10:05am    
ASPX Code
---------
<asp:GridView ID="GridView1" runat="server" align="center" AllowPaging="true" DataKeyNames="PrNo,RevisionNo"
AutoGenerateColumns="False" class="tabulardata"
HorizontalAlign="Center" Width="100%">
<columns>
<asp:TemplateField HeaderText="Sl.No" ItemStyle-HorizontalAlign="Center"
ItemStyle-Width="4%">
<itemtemplate>
<%# Container.DataItemIndex + 1%>


<%-- <asp:TemplateField HeaderText="CheckAll">
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelectAll_CheckedChanged"/>
</HeaderTemplate>
<itemtemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged"/>
--%>
<%-- --%>
<asp:BoundField HeaderText="PrNo" Visible="true" DataField="PrNo"
ItemStyle-HorizontalAlign="center" ReadOnly="true" SortExpression="PrNo">
<itemstyle horizontalalign="Center" width="15%">

<asp:TemplateField HeaderText="PrNo" Visible="false" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="10%">
<itemtemplate>
<asp:Label ID="lblPrNo" runat="server" Text='<%# Eval("PrNo") %>'>

<HeaderStyle HorizontalAlign="Center" />

<asp:BoundField HeaderText="RevisionNo" Visible="true" DataField="RevisionNo"
ItemStyle-HorizontalAlign="center" ReadOnly="true" SortExpression="RevisionNo">
<itemstyle horizontalalign="Center" width="10%">

<asp:TemplateField HeaderText="RevisionNo" Visible="false" HeaderStyle-HorizontalAlign="Center" >
<itemtemplate>
<asp:Label ID="lblRevisionNo" runat="server" Text='<%# Eval("RevisionNo") %>'>

<HeaderStyle HorizontalAlign="Center" />

<asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center">
<itemtemplate>
<asp:HyperLink ID="hyperlink1" onclick="" BorderStyle="None" runat="server" Height="15"
ImageUrl="~/images/EditImage.png"
Width="50%" >

<itemstyle horizontalalign="Center" width="6%">
[no name] 3-Aug-11 10:25am    
Format your code using code blocks.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900