ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.[Solved] ASP.Net control Focus method not working when MaintainScrollPositionOnPostBack is True<p>Hey <a class="username" rel="Warren"> Warren</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(window).load(function () { setTimeout(function () { $(&#39;#gvtfaDetails&#39;).focus(); document.activeElement.scrollIntoView(true); }, 1); }); &lt;/script&gt; &lt;div&gt; &lt;asp:GridView ID=&#34;gvtfaTransaction&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; AllowSorting=&#34;True&#34; OnSorting=&#34;OnSorting&#34; OnRowDataBound=&#34;gvtfaTransaction_RowDataBound&#34; OnPageIndexChanging=&#34;OnPageIndexChanging&#34; CssClass=&#34;table table-striped table-bordered table-hover&#34; DataKeyNames=&#34;CustomerId&#34; Width=&#34;98%&#34; HorizontalAlign=&#34;Center&#34; OnSelectedIndexChanged=&#34;gvtfaTransaction_SelectedIndexChanged1&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;Name&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblName&#34; Text=&#39;&lt;%# Eval(&#34;ContactName&#34;) %&gt;&#39; runat=&#34;server&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; /&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&#34;LinkButtonSelect&#34; runat=&#34;server&#34; Text=&#34;Select&#34; CommandName=&#34;Select&#34; CommandArgument=&#34;&lt;%# Container.DataItemIndex %&gt;&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;SelectedRowStyle BackColor=&#34;lightyellow&#34; /&gt; &lt;/asp:GridView&gt; &lt;asp:GridView ID=&#34;gvtfaDetails&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; DataKeyNames=&#34;CustomerId&#34; ShowFooter=&#34;True&#34; AllowPaging=&#34;True&#34; AllowSorting=&#34;False&#34; OnRowDataBound=&#34;gvtfaDetails_RowDataBound&#34; OnSorting=&#34;OnSorting&#34; CssClass=&#34;table table-striped table-bordered table-hover&#34; Width=&#34;98%&#34; HorizontalAlign=&#34;Center&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;CustomerId&#34; HeaderText=&#34;CustomerId&#34; ShowHeader=&#34;false&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;CustomerId&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;4. Check Memo&#34; SortExpression=&#34;CompanyName&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;LabelCheckMemo&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;CompanyName&#34;) %&gt;&#39; Visible=&#34;false&#34;&gt;&lt;/asp:Label&gt; &lt;asp:TextBox ID=&#34;TextCheckMemo&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;City&#34;) %&gt;&#39; Visible=&#34;false&#34; AutoPostBack=&#34;true&#34; MaxLength=&#34;30&#34;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;HeaderStyle Wrap=&#34;False&#34; /&gt; &lt;/asp:TemplateField&gt; &lt;asp:BoundField DataField=&#34;PostalCode&#34; HeaderText=&#34;PostalCode&#34; ShowHeader=&#34;false&#34; HeaderStyle-Wrap=&#34;false&#34; SortExpression=&#34;PostalCode&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Configuration; using System.Data.SqlClient; using System.Data; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { string strconstr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(strconstr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM Customers&#34;, 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(&#34;lblName&#34;) as Label).Text; string strconstr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(strconstr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM Customers WHERE ContactName=@Name&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@Name&#34;, 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) { }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(strconstr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM Customers&#34;, 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(&#34;lblName&#34;), Label)).Text Dim strconstr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(strconstr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM Customers WHERE ContactName=@Name&#34;, con) cmd.Parameters.AddWithValue(&#34;@Name&#34;, 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</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/sAxyxp3.gif" alt="" width="361" height="379" /></p>https://www.aspforums.net:443/Threads/577127/Solved-ASPNet-control-Focus-method-not-working-when-MaintainScrollPositionOnPostBack-is-True/https://www.aspforums.net:443/Threads/577127/Solved-ASPNet-control-Focus-method-not-working-when-MaintainScrollPositionOnPostBack-is-True/Thu, 13 Dec 2018 04:09:03 GMT