Solved

ASP.net Pagejumps when GridView buttons are clicked

Posted on 2013-11-06
4
371 Views
Last Modified: 2013-11-09
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
0
Comment
Question by:murbro
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39627557
What if you through the grid in an update panel?

eg:

<asp:UpdatePanel runat="server" id="UP1">
<contenttemplate>
  <%-- grid goes here %-->
</contenttemplate>
</asp:UpdatePanel>
0
 

Author Comment

by:murbro
ID: 39627595
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.
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39627882
add a script manager at the top of the page:

<asp:ScriptManager runat="server" id= "SM1">
</asp:ScriptManager>
0
 

Author Closing Comment

by:murbro
ID: 39635405
Thanks very much
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question