Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. setting focus to a button

setting focus to a button

Scheduled Pinned Locked Moved ASP.NET
databasejavascriptsysadminhelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mathew68007
    wrote on last edited by
    #1

    I have 2 textboxes and a button. for texbox 1 i have used a handler for autocompletion from database. and using javascript on key pressing "enter" key the focus changes to textbox 2.. in textbox2 textchanged event i am checking a value in database corresponding to the values in textbox1.

    so on post back the focus is set to textbox1. i want to be on button1.

    $(document).ready(function() {
    $("#<%=textbox1.ClientID%>").autocomplete('../wholesalers/Handler.ashx');
    });

    $(function() {
        $('input:text:first').focus();
        var $inp = $('input:text');
        $inp.bind('keydown', function(e) {
            //var key = (e.keyCode ? e.keyCode : e.charCode);
            var key = e.which;
            if (key == 13) {
                e.preventDefault();
                var nxtIdx = $inp.index(this) + 1;
                $(":input:text:eq(" + nxtIdx + ")").focus();
            }
        });
    });
    

    protected void Page_Load(object sender, EventArgs e)
    {
    lbl_Wname.Text = Request.QueryString["name"];
    if (!IsPostBack)
    {
    textbox1.Attributes.Add("onkeypress", "returnSetfocus(event,'" + textbox1.ClientID + "')");
    }
    else
    {
    button1.Focus();
    }
    }

    protected void textbox2_TextChanged(object sender, EventArgs e)
    {
    if (textbox2.Text == "" || textbox2 == null)
    {
    ScriptManager.RegisterStartupScript(this, GetType(), "error", "alert('Enter Quantity');", true);
    }
    else
    {

           MySqlConnection mcn = new MySqlConnection("server=localhost;---------------------");
           mcn.Open();
           MySqlCommand cmd = new MySqlCommand();
           cmd.CommandText = "searchp";
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Connection = mcn;
           cmd.Parameters.Add("p\_product\_name", MySqlDbType.VarChar).Value =textbox1.Text;
           DataTable dt = new DataTable();
           MySqlDataAdapter ada = new MySqlDataAdapter(cmd);
           ada.Fill(dt);
           if (dt.Rows.Count > 0)
           {
               string sts = dt.Rows\[0\]\["status"\].ToString();
               if (sts == "available")
               {
                   Image1.ImageUrl = "~/images/tick.gif";
               }
               if (sts == "not-available")
               {
                   Image1.ImageUrl = "~/images/cross.gif";
    
    A 1 Reply Last reply
    0
    • M mathew68007

      I have 2 textboxes and a button. for texbox 1 i have used a handler for autocompletion from database. and using javascript on key pressing "enter" key the focus changes to textbox 2.. in textbox2 textchanged event i am checking a value in database corresponding to the values in textbox1.

      so on post back the focus is set to textbox1. i want to be on button1.

      $(document).ready(function() {
      $("#<%=textbox1.ClientID%>").autocomplete('../wholesalers/Handler.ashx');
      });

      $(function() {
          $('input:text:first').focus();
          var $inp = $('input:text');
          $inp.bind('keydown', function(e) {
              //var key = (e.keyCode ? e.keyCode : e.charCode);
              var key = e.which;
              if (key == 13) {
                  e.preventDefault();
                  var nxtIdx = $inp.index(this) + 1;
                  $(":input:text:eq(" + nxtIdx + ")").focus();
              }
          });
      });
      

      protected void Page_Load(object sender, EventArgs e)
      {
      lbl_Wname.Text = Request.QueryString["name"];
      if (!IsPostBack)
      {
      textbox1.Attributes.Add("onkeypress", "returnSetfocus(event,'" + textbox1.ClientID + "')");
      }
      else
      {
      button1.Focus();
      }
      }

      protected void textbox2_TextChanged(object sender, EventArgs e)
      {
      if (textbox2.Text == "" || textbox2 == null)
      {
      ScriptManager.RegisterStartupScript(this, GetType(), "error", "alert('Enter Quantity');", true);
      }
      else
      {

             MySqlConnection mcn = new MySqlConnection("server=localhost;---------------------");
             mcn.Open();
             MySqlCommand cmd = new MySqlCommand();
             cmd.CommandText = "searchp";
             cmd.CommandType = CommandType.StoredProcedure;
             cmd.Connection = mcn;
             cmd.Parameters.Add("p\_product\_name", MySqlDbType.VarChar).Value =textbox1.Text;
             DataTable dt = new DataTable();
             MySqlDataAdapter ada = new MySqlDataAdapter(cmd);
             ada.Fill(dt);
             if (dt.Rows.Count > 0)
             {
                 string sts = dt.Rows\[0\]\["status"\].ToString();
                 if (sts == "available")
                 {
                     Image1.ImageUrl = "~/images/tick.gif";
                 }
                 if (sts == "not-available")
                 {
                     Image1.ImageUrl = "~/images/cross.gif";
      
      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      How to: Set Focus on ASP.NET Web Server Controls[^] should surely help you out.

      J 1 Reply Last reply
      0
      • A Abhinav S

        How to: Set Focus on ASP.NET Web Server Controls[^] should surely help you out.

        J Offline
        J Offline
        Jack Li
        wrote on last edited by
        #3

        <form id="form1" runat="server" defaultfocus="TextBox1" >

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups