Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
focus on control after page load with use a master page in asp.net
Posted
Comments
JoCodes 18-Nov-13 1:35am    
Can you elaborate on what you are looking for???Not Clear

Hi,

I think you want to set focus on load of a page.

Write the following Jquery code to do this,

JavaScript
$(document).ready( function(){

      $('#controlID').focus();

});
 
Share this answer
 
You try any of the ways as below

1. Set directly form tag of your page. Or

2. Or in PageLoad event try InputControlId.Focus();

if you want to set through clientside you try as Rockstar's solution.

Hope this helps...
 
Share this answer
 
one more way

Declare JavaScript block in your content page like below

XML
<%@ Page Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication4.WebForm2" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    <script>
    var controlId_focus= '<%=controlIdForFocus%>';
    function CallSetFocus(){
      if(controlId_focus !="" && document.getElementById(controlId_focus)!=null)    {
        document.getElementById(controlId_focus).focus();
      }
    }
     setTimeout('CallSetFocus()',200);
    </script>
</asp:Content>



Now in your code behind file declare protected string "controlIdForFocus" and assign ClientID of a control where you want focus like below code snippet

C#
public partial class WebForm2 : System.Web.UI.Page
   {
       protected string controlIdForFocus = "";
       protected void Page_Load(object sender, EventArgs e)
       {
           controlIdForFocus = TextBox3.ClientID;
       }
   }
 
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