Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.net Pagejumps when GridView buttons are clicked

Hi

I have the following GridView on my ASP.net web form. It has five buttons (Event code shown further down) that the user clicks. This usually works fine, but about one in ten users experience a jumpy screen even though I have MaintainScrollPositionOnPostback="true"
Is there a way to prevent this jumpy screen. Some of the users refuse to use the site because of it.


<asp:GridView ID="GridView_Questions" runat="server" BackColor="White"
                BorderColor="#3366CC" BorderStyle="Solid" BorderWidth="1px" CellPadding="4"
                Height="147px" Visible="False" Width="795px">
                <Columns>

                      <asp:TemplateField HeaderText="Strongly Agree" HeaderStyle-Font-Size ="Small"
                                            ControlStyle-Font-Bold ="false" ControlStyle-Forecolor ="#006600"
                                            ControlStyle-Font-Size ="Smaller">
                    <ItemTemplate>
                        <asp:Button ID="Button6" runat="server"
                      CommandName="MyButton1"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="Strongly Agree" />
                    </ItemTemplate>
                    <ControlStyle Font-Bold="False" Font-Size="Smaller" ForeColor="#006600" />
                    <HeaderStyle Font-Size="Small" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Agree" HeaderStyle-Font-Size ="Small" ControlStyle-Font-Bold ="false"
                                            ControlStyle-Forecolor ="#009900"
                    ControlStyle-Font-Size ="Smaller">
                    <ItemTemplate>
                        <asp:Button ID="Button7" runat="server"
                      CommandName="MyButton2"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="Agree" />
                    </ItemTemplate>
                    <ControlStyle Font-Bold="False" Font-Size="Smaller" ForeColor="#009900" />
                    <HeaderStyle Font-Size="Small" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Undecided" HeaderStyle-Font-Size ="Small"
                                            ControlStyle-Font-Bold ="false" ControlStyle-Forecolor ="SlateGray"
                                            ControlStyle-Font-Size ="Smaller">
                    <ItemTemplate>
                        <asp:Button ID="Button8" runat="server"
                      CommandName="MyButton3"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="Undecided" />
                    </ItemTemplate>
                    <ControlStyle Font-Bold="False" Font-Size="Smaller" ForeColor="SlateGray" />
                    <HeaderStyle Font-Size="Small" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Disagree" HeaderStyle-Font-Size ="Small"
                                            ControlStyle-Font-Bold ="false" ControlStyle-Forecolor ="#FF6600"
                                            ControlStyle-Font-Size ="Smaller">
                    <ItemTemplate>
                        <asp:Button ID="Button9" runat="server"
                      CommandName="MyButton4"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="Disagree" />
                    </ItemTemplate>
                    <ControlStyle Font-Bold="False" Font-Size="Smaller" ForeColor="#FF6600" />
                    <HeaderStyle Font-Size="Small" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Strongly Disagree" HeaderStyle-Font-Size ="Small"
                                            ControlStyle-Font-Bold ="false" ControlStyle-Forecolor ="DarkRed"
                                            ControlStyle-Font-Size ="Smaller">
                    <ItemTemplate>
                        <asp:Button ID="Button10" runat="server"
                      CommandName="MyButton5"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="Strongly Disagree" />
                    </ItemTemplate>
                    <ControlStyle Font-Bold="False" Font-Size="Smaller" ForeColor="DarkRed" />
                    <HeaderStyle Font-Size="Small" />
                </asp:TemplateField>
 
                </Columns>
                <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                <HeaderStyle BackColor="White" Font-Bold="False" ForeColor="#000066" />
                <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                <RowStyle BackColor="White" ForeColor="#003399" />
                <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                <SortedAscendingCellStyle BackColor="#EDF6F6" />
                <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                <SortedDescendingCellStyle BackColor="#D6DFDF" />
                <SortedDescendingHeaderStyle BackColor="#002876" />
            </asp:GridView>



 


   Private Sub GridView2_RowCommand1(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView_Questions.RowCommand
        Try

            If Me.Label_LoggedIn.Text = "Not logged in" Then
                Me.Label_Error1.Text = "Please log in to complete this survey"
                Exit Sub
            Else
                Me.Label_Error1.Text = ""
            End If

            If (e.CommandName = "MyButton1") Then
                ' Retrieve the row index stored in the CommandArgument property.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)

                ' Retrieve the row that contains the button
                ' from the Rows collection.
                Dim row As GridViewRow = GridView_Questions.Rows(index)

                row.Cells(5).BackColor = Drawing.Color.DarkGreen
                row.Cells(6).BackColor = Drawing.Color.DarkGreen
                row.ForeColor = Drawing.Color.White

                Dim Q As Integer = CInt(row.Cells(5).Text)

                If Q < 6 Then
                    row.BackColor = Me.G.ForeColor
                ElseIf Q > 5 And Q < 11 Then
                    row.BackColor = Me.R.ForeColor
                ElseIf Q > 10 And Q < 16 Then
                    row.BackColor = Me.E.ForeColor
                ElseIf Q > 15 And Q < 21 Then
                    row.BackColor = Me.A.ForeColor
                ElseIf Q > 20 And Q < 26 Then
                    row.BackColor = Me.T.ForeColor
                ElseIf Q > 25 Then
                    row.BackColor = System.Drawing.Color.SandyBrown
                End If



            ElseIf (e.CommandName = "MyButton2") Then
                ' Retrieve the row index stored in the CommandArgument property.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)

                ' Retrieve the row that contains the button
                ' from the Rows collection.
                Dim row As GridViewRow = GridView_Questions.Rows(index)

                'row.BackColor = Drawing.Color.LightGray


                row.Cells(5).BackColor = Drawing.Color.Green
                row.Cells(6).BackColor = Drawing.Color.Green
                row.ForeColor = Drawing.Color.White

                Dim Q As Integer = CInt(row.Cells(5).Text)

                If Q < 6 Then
                    row.BackColor = Me.G.ForeColor
                ElseIf Q > 5 And Q < 11 Then
                    row.BackColor = Me.R.ForeColor
                ElseIf Q > 10 And Q < 16 Then
                    row.BackColor = Me.E.ForeColor
                ElseIf Q > 15 And Q < 21 Then
                    row.BackColor = Me.A.ForeColor
                ElseIf Q > 20 And Q < 26 Then
                    row.BackColor = Me.T.ForeColor
                ElseIf Q > 25 Then
                    row.BackColor = System.Drawing.Color.SandyBrown
                End If

            ElseIf (e.CommandName = "MyButton3") Then
                ' Retrieve the row index stored in the CommandArgument property.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)

                ' Retrieve the row that contains the button
                ' from the Rows collection.
                Dim row As GridViewRow = GridView_Questions.Rows(index)

                'row.BackColor = Drawing.Color.LightGray


                row.Cells(5).BackColor = Drawing.Color.SlateGray
                row.Cells(6).BackColor = Drawing.Color.SlateGray
                row.ForeColor = Drawing.Color.White

                Dim Q As Integer = CInt(row.Cells(5).Text)

                If Q < 6 Then
                    row.BackColor = Me.G.ForeColor
                ElseIf Q > 5 And Q < 11 Then
                    row.BackColor = Me.R.ForeColor
                ElseIf Q > 10 And Q < 16 Then
                    row.BackColor = Me.E.ForeColor
                ElseIf Q > 15 And Q < 21 Then
                    row.BackColor = Me.A.ForeColor
                ElseIf Q > 20 And Q < 26 Then
                    row.BackColor = Me.T.ForeColor
                ElseIf Q > 25 Then
                    row.BackColor = System.Drawing.Color.SandyBrown
                End If

            ElseIf (e.CommandName = "MyButton4") Then
                ' Retrieve the row index stored in the CommandArgument property.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)

                ' Retrieve the row that contains the button
                ' from the Rows collection.
                Dim row As GridViewRow = GridView_Questions.Rows(index)

                'row.BackColor = Drawing.Color.LightGray

                row.Cells(5).BackColor = Drawing.Color.DarkOrange
                row.Cells(6).BackColor = Drawing.Color.DarkOrange
                row.ForeColor = Drawing.Color.White

                Dim Q As Integer = CInt(row.Cells(5).Text)

                If Q < 6 Then
                    row.BackColor = Me.G.ForeColor
                ElseIf Q > 5 And Q < 11 Then
                    row.BackColor = Me.R.ForeColor
                ElseIf Q > 10 And Q < 16 Then
                    row.BackColor = Me.E.ForeColor
                ElseIf Q > 15 And Q < 21 Then
                    row.BackColor = Me.A.ForeColor
                ElseIf Q > 20 And Q < 26 Then
                    row.BackColor = Me.T.ForeColor
                ElseIf Q > 25 Then
                    row.BackColor = System.Drawing.Color.SandyBrown
                End If

            ElseIf (e.CommandName = "MyButton5") Then
                ' Retrieve the row index stored in the CommandArgument property.
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)

                ' Retrieve the row that contains the button
                ' from the Rows collection.
                Dim row As GridViewRow = GridView_Questions.Rows(index)

                'row.BackColor = Drawing.Color.LightGray


                row.Cells(5).BackColor = Drawing.Color.DarkRed
                row.Cells(6).BackColor = Drawing.Color.DarkRed
                row.ForeColor = Drawing.Color.White

                Dim Q As Integer = CInt(row.Cells(5).Text)

                If Q < 6 Then
                    row.BackColor = Me.G.ForeColor
                ElseIf Q > 5 And Q < 11 Then
                    row.BackColor = Me.R.ForeColor
                ElseIf Q > 10 And Q < 16 Then
                    row.BackColor = Me.E.ForeColor
                ElseIf Q > 15 And Q < 21 Then
                    row.BackColor = Me.A.ForeColor
                ElseIf Q > 20 And Q < 26 Then
                    row.BackColor = Me.T.ForeColor
                ElseIf Q > 25 Then
                    row.BackColor = System.Drawing.Color.SandyBrown
                End If

            End If

        Catch ex As Exception
            Me.Label_Error1.Text = ex.Message
        End Try
    End Sub
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

What if you through the grid in an update panel?

eg:

<asp:UpdatePanel runat="server" id="UP1">
<contenttemplate>
  <%-- grid goes here %-->
</contenttemplate>
</asp:UpdatePanel>
Avatar of Murray Brown

ASKER

I get the error The control with ID 'UP1' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it.
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks very much