[Solved] ASP.Net control Focus method not working when MaintainScrollPositionOnPostBack is True

Last Reply one month ago By pandeyism

Posted one month ago

I have two gridviews on my page. One is a header and the other is a detail.

You can open the details by hitting "select" on the header row:

<asp:LinkButton runat="server" Text="Select" Enabled="true" CommandName="Select" />

I have set maintainScrollPositionOnPostBack = "true" in the webconfig because I need this functionality in the heade.;The problem is that when I click "select" in the header to open details in the bottom gridview, I would like the scroll position to change to this gridview so the user does not have to manually scroll.

        <asp:GridView ID="gvtfaTransaction" runat="server" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" OnSorting="OnSorting" OnRowDataBound="gvtfaTransaction_RowDataBound" OnPageIndexChanging="OnPageIndexChanging" CssClass="table table-striped table-bordered table-hover" DataKeyNames="ID" Width="98%" HorizontalAlign="Center">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="0. ID" ShowHeader="false" HeaderStyle-Wrap="false" SortExpression="ID" />
                <asp:BoundField DataField="BankAccount" HeaderText="1. Bank Account" HeaderStyle-Wrap="false" SortExpression="BankAccount" visible="false"/>
                <asp:BoundField DataField="DateDeposited" HeaderText="2. Date Deposited" DataFormatString="{0:D}" HeaderStyle-Wrap="false" SortExpression="DateDeposited" />
                <asp:BoundField DataField="PayName" HeaderText="3. Payee" HeaderStyle-Wrap="false" SortExpression="PayName" />
                <asp:BoundField DataField="Reference" HeaderText="4. Reference" HeaderStyle-Wrap="false" SortExpression="Reference" />
                <asp:BoundField DataField="Memo" HeaderText="5. Memo" SortExpression="Memo"/>
                <asp:BoundField DataField="EntryType" HeaderText="6. Entry Type" HeaderStyle-Wrap="false" SortExpression="EntryType" />
                <asp:TemplateField HeaderText="7. GL Account" HeaderStyle-HorizontalAlign="Left" SortExpression="GLAcct">
                    <ItemTemplate>
                        <asp:Label ID="LabelGLAcct" runat="server" Text='<%# Eval("GLAcct") %>' Visible="false"></asp:Label>
                        <asp:DropDownList ID="DropdownlistGLAcct" runat="server" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="gvtfaTransactionGLAcct_SelectIndexChanging"/>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Left" Wrap="false" />
                </asp:TemplateField>
                <asp:BoundField DataField="Amount" HeaderText="8. Amount" DataFormatString="{0:C}" HeaderStyle-Wrap="false" SortExpression="Amount" />
                <asp:TemplateField HeaderText="9. Exchange" SortExpression="ExchangeRate">
                    <ItemTemplate>
                        <asp:Label ID="LabelExchangeRate" runat="server" Text='<%# String.Format("{0:0.00000}", Eval("ExchangeRate")) %>' visible="false"></asp:Label>
                        <asp:TextBox ID="TextExchangeRate" runat="server" Text='<%# Eval("ExchangeRate", "{0:0.00000}") %>' Visible="false" OnTextChanged="gvtfaTransaction_TextChanged" AutoPostBack="true" Width="65px" CssClass="MaskText"></asp:TextBox>
                    </ItemTemplate>
                    <HeaderStyle Wrap="False" />
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButtonSelect" runat="server" Text="Select" CommandName="Select" CommandArgument="<%# Container.DataItemIndex %>" Visible='<%# Eval("Details") %>'/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="btnIgnore" runat="server" Text='<%# Eval("Ignore") %>' Enabled="true" CommandName="Ignore" OnClick="Ignore_Click"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
                <SelectedRowStyle BackColor="lightyellow" />
        </asp:GridView> 
        <asp:GridView ID="gvtfaDetails" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" ShowFooter="True" AllowPaging="True" AllowSorting="False" OnRowDataBound="gvtfaDetails_RowDataBound" OnSorting="OnSorting" CssClass="table table-striped table-bordered table-hover" Width="98%" HorizontalAlign="Center">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="0. ID" ShowHeader="false" HeaderStyle-Wrap="false" SortExpression="ID"/>
                <asp:BoundField DataField="CheckDate" HeaderText="1. Check Date" ShowHeader="false" HeaderStyle-Wrap="false" DataFormatString="{0:D}" SortExpression="CheckDate"/>
                <asp:BoundField DataField="BorrowerName" HeaderText="2. Borrower Name" ShowHeader="false" HeaderStyle-Wrap="false" SortExpression="BorrowerName" />
                <asp:BoundField DataField="CheckNo" HeaderText="3. CheckNo" ShowHeader="false" HeaderStyle-Wrap="false" SortExpression="CheckNo" />                
                <asp:TemplateField HeaderText="4. Check Memo" SortExpression="CheckMemo">
                    <ItemTemplate>
                        <asp:Label ID="LabelCheckMemo" runat="server" Text='<%# Eval("CheckMemo") %>' visible="false"></asp:Label>
                        <asp:TextBox ID="TextCheckMemo" runat="server" Text='<%# Eval("CheckMemo") %>' Visible="false" OnTextChanged="gvtfaDetails_TextChanged" AutoPostBack="true" MaxLength="30"></asp:TextBox>
                    </ItemTemplate>
                    <HeaderStyle Wrap="False" />
                </asp:TemplateField>               
                <asp:BoundField DataField="LoanCode" HeaderText="5. Loan Code" ShowHeader="false" HeaderStyle-Wrap="false" SortExpression="LoanCode" />
                <asp:TemplateField HeaderText="6. GL Account" HeaderStyle-HorizontalAlign="Left" SortExpression="GLAcct">
                    <ItemTemplate>
                        <asp:Label ID="LabelGLAcct" runat="server" Text='<%# Eval("GLAcct") %>' Visible="false"></asp:Label>
                        <asp:DropDownList ID="DropdownlistGLAcct" runat="server" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="gvtfaDetailsGLAcct_SelectIndexChanging"/>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Left" Wrap="false" />
                </asp:TemplateField>                
                <asp:TemplateField HeaderText="7. Amount" HeaderStyle-HorizontalAlign="Right" SortExpression="Amount">
                    <ItemTemplate>
                        <asp:Label ID="LabelAmount" runat="server" Text='<%# String.Format("$ {0:#,##0.00}", Eval("Amount")) %>' visible="false"></asp:Label>
                        <asp:TextBox ID="TextAmount" runat="server" Text='<%# String.Format("$ {0:#,##0.00}", Eval("Amount")) %>' Visible="false" style="text-align: right" readonly="true" OnTextChanged="gvtfaDetails_TextChanged" AutoPostBack="true" onkeypress="return isFloatNumber(this,event);"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

 

Protected Sub gvtfaTransaction_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gvtfaTransaction.SelectedIndexChanged
    Dim conString As String = ConfigurationManager.ConnectionStrings("mogp_app").ConnectionString
    Using con As New SqlConnection(conString)
        Using cmd As New SqlCommand("usp_details")
            cmd.Parameters.AddWithValue("@Action", "SELECT")
            cmd.Parameters.AddWithValue("@User", LabelFullname.Text)
            cmd.Parameters.AddWithValue("@Parameter1", gvtfaTransaction.SelectedValue.ToString())
            cmd.Parameters.AddWithValue("@Parameter2", "")
            cmd.Parameters.AddWithValue("@Parameter3", "")
            Using sda As New SqlDataAdapter()
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Connection = con
                sda.SelectCommand = cmd
                Using dt As New DataTable()
                    sda.Fill(dt)
                    gvtfaDetails.DataSource = dt
                    gvtfaDetails.DataBind()
                    'Calculate Sum and display in Footer Row
                    Dim total As Decimal = dt.AsEnumerable().Sum(Function(row) row.Field(Of Decimal)("Amount"))
                    gvtfaDetails.FooterRow.Cells(6).Text = "Total"
                    gvtfaDetails.FooterRow.Cells(6).HorizontalAlign = HorizontalAlign.Right
                    gvtfaDetails.FooterRow.Cells(6).Font.Bold = True
                    gvtfaDetails.FooterRow.Cells(7).Text = total.ToString("C2")
                    gvtfaDetails.FooterRow.Cells(7).HorizontalAlign = HorizontalAlign.Right
                    gvtfaDetails.FooterRow.Cells(7).Font.Bold = True
                End Using
            End Using
        End Using
    End Using 
    btnCancelDetails.Visible = True 
End Sub

 

You are viewing reply posted by: pandeyism one month ago.
Posted one month ago

Hey Warren,

Please refer below sample.

HTML

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(window).load(function () {
        setTimeout(function () {
            $('#gvtfaDetails').focus();
            document.activeElement.scrollIntoView(true);
        }, 1);
    });
</script>
<div>
    <asp:GridView ID="gvtfaTransaction" runat="server" AutoGenerateColumns="False" AllowSorting="True"
        OnSorting="OnSorting" OnRowDataBound="gvtfaTransaction_RowDataBound" OnPageIndexChanging="OnPageIndexChanging"
        CssClass="table table-striped table-bordered table-hover" DataKeyNames="CustomerId"
        Width="98%" HorizontalAlign="Center" OnSelectedIndexChanged="gvtfaTransaction_SelectedIndexChanged1">
        <Columns>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:Label ID="lblName" Text='<%# Eval("ContactName") %>' runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Country" HeaderText="Country" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButtonSelect" runat="server" Text="Select" CommandName="Select"
                        CommandArgument="<%# Container.DataItemIndex %>" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <SelectedRowStyle BackColor="lightyellow" />
    </asp:GridView>
    <asp:GridView ID="gvtfaDetails" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerId"
        ShowFooter="True" AllowPaging="True" AllowSorting="False" OnRowDataBound="gvtfaDetails_RowDataBound"
        OnSorting="OnSorting" CssClass="table table-striped table-bordered table-hover"
        Width="98%" HorizontalAlign="Center">
        <Columns>
            <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" ShowHeader="false"
                HeaderStyle-Wrap="false" SortExpression="CustomerId" />
            <asp:TemplateField HeaderText="4. Check Memo" SortExpression="CompanyName">
                <ItemTemplate>
                    <asp:Label ID="LabelCheckMemo" runat="server" Text='<%# Eval("CompanyName") %>' Visible="false"></asp:Label>
                    <asp:TextBox ID="TextCheckMemo" runat="server" Text='<%# Eval("City") %>' Visible="false"
                        AutoPostBack="true" MaxLength="30"></asp:TextBox>
                </ItemTemplate>
                <HeaderStyle Wrap="False" />
            </asp:TemplateField>
            <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" ShowHeader="false"
                HeaderStyle-Wrap="false" SortExpression="PostalCode" />
        </Columns>
    </asp:GridView>
</div>

Namespaces

C#

using System.Configuration;
using System.Data.SqlClient;
using System.Data;

VB.Net

Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string strconstr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strconstr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        da.Fill(dt);
                        gvtfaTransaction.DataSource = dt;
                        gvtfaTransaction.DataBind();
                    }
                }
            }
        }
    }
}

protected void gvtfaTransaction_SelectedIndexChanged1(object sender, EventArgs e)
{
    int rowIndex = Convert.ToInt32(gvtfaTransaction.SelectedRow.RowIndex);
    GridViewRow row = gvtfaTransaction.Rows[rowIndex];
    string name = (row.FindControl("lblName") as Label).Text;
    string strconstr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strconstr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE ContactName=@Name", con))
        {
            cmd.Parameters.AddWithValue("@Name", name);
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                using (DataTable dtNew = new DataTable())
                {
                    da.Fill(dtNew);
                    gvtfaDetails.DataSource = dtNew;
                    gvtfaDetails.DataBind();
                }
            }
        }
    }
}
protected void gvtfaDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{

}

protected void OnSorting(object sender, GridViewSortEventArgs e)
{

}

protected void gvtfaTransaction_RowDataBound(object sender, GridViewRowEventArgs e)
{

}

protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{

}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim strconstr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(strconstr)
            Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
                Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Using dt As DataTable = New DataTable()
                        da.Fill(dt)
                        gvtfaTransaction.DataSource = dt
                        gvtfaTransaction.DataBind()
                    End Using
                End Using
            End Using
        End Using
    End If
End Sub

Protected Sub gvtfaTransaction_SelectedIndexChanged1(sender As Object, e As System.EventArgs) Handles gvtfaTransaction.SelectedIndexChanged
    Dim rowIndex As Integer = Convert.ToInt32(gvtfaTransaction.SelectedRow.RowIndex)
    Dim row As GridViewRow = gvtfaTransaction.Rows(rowIndex)
    Dim name As String = (TryCast(row.FindControl("lblName"), Label)).Text
    Dim strconstr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(strconstr)
        Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers WHERE ContactName=@Name", con)
            cmd.Parameters.AddWithValue("@Name", name)
            Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                Using dtNew As DataTable = New DataTable()
                    da.Fill(dtNew)
                    gvtfaDetails.DataSource = dtNew
                    gvtfaDetails.DataBind()
                End Using
            End Using
        End Using
    End Using
End Sub

Protected Sub OnPageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvtfaTransaction.PageIndexChanging

End Sub

Protected Sub gvtfaTransaction_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvtfaTransaction.RowDataBound

End Sub

Protected Sub gvtfaDetails_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvtfaDetails.RowDataBound

End Sub

Protected Sub OnSorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvtfaTransaction.Sorting

End Sub

Screenshot