setting focus to a button
-
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";
-
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";
-
How to: Set Focus on ASP.NET Web Server Controls[^] should surely help you out.