Showing posts with label cursor. Show all posts
Showing posts with label cursor. Show all posts

Wednesday, March 28, 2012

set default cursor position

how to set the default cursor position to the one textbox in asp.netHere is one way to do it (using body onLoad event):
<body onLoad='<%= "window.document.forms[0]." + TextBoxID.ClientID +
".focus();" %>'>
OR
<body onLoad='window.document.forms[0].<%= TextBoxID.ClientID %>.focus();'>
"Grey" <erickwyum@.i-cable.com> wrote in message
news:uMeZzwFoEHA.3464@.TK2MSFTNGP14.phx.gbl...
how to set the default cursor position to the one textbox in asp.net

Saturday, March 24, 2012

Set focus and cursor at end of text?

I use this code to set focus to a textbox when I load a page:

private void SetFocus(System.Web.UI.Control ctrl)
{
string s = "<SCRIPT language=\"javascript\">document.getElementById('" +
ctrl.ID + "').focus()</SCRIPT>";

RegisterStartupScript("focus", s);
}

I would also like the cursor to be positioned at the end of the text field.
Using only the above code, the cursor is positioned at the beginning of the
textbox even if there are some text present in the field.

How can I position the cusrsor at the end of the text?

OlavOlav,

Here we go:

Create a new function in your <head> tag in the HTML section:

function SetEnd (TB)
{
if (TB.createTextRange)
{
var FieldRange = TB.createTextRange();
FieldRange.moveStart('character', TB.value.length);
FieldRange.collapse();
FieldRange.select();
}
}

Create a new onfocus JavaScript event in your asp text box tag (asp:textBox)
that calls the SetEnd function above:

<asp:textbox id="TextBox1" runat="server" Width="65px"
onfocus="SetEnd(this)">Existing text</asp:textbox
Make sure you keep your existing code that calls the SetFocus function as it
is:

private void SetFocus(System.Web.UI.Control ctrl)
{
string s = "<SCRIPT language=\"javascript\">document.getElementById('" +
ctrl.ID + "').focus()</SCRIPT>";

RegisterStartupScript("focus", s);
}

Don't forget to call the above SetFocus() function in your Body tag on page
load ....( <body onload="SetFocus()"> )

Good luck!

Regards,

Mohammad Samara.

ICS (London) Ltd.

"Olav Tollefsen" <x@.y.com> wrote in message
news:uxrxLYt3DHA.3656@.TK2MSFTNGP11.phx.gbl...
> I use this code to set focus to a textbox when I load a page:
> private void SetFocus(System.Web.UI.Control ctrl)
> {
> string s = "<SCRIPT language=\"javascript\">document.getElementById('" +
> ctrl.ID + "').focus()</SCRIPT>";
> RegisterStartupScript("focus", s);
> }
> I would also like the cursor to be positioned at the end of the text
field.
> Using only the above code, the cursor is positioned at the beginning of
the
> textbox even if there are some text present in the field.
> How can I position the cusrsor at the end of the text?
> Olav

Set Focus on text box

Hi, can anyone tell me how to set cursor focus to one textbox eg. textbox1 after the form has been posted back?
In windows form using vb.net, we could use textbox1.setfocus(), but it seems cannot find this in asp.net.
Thanks!Focus of a textbox can only be acheived by using javascript.

Page.RegisterStartUpScript("setBoxFocus","var x = document.getElementById('" & myTextBox.clientID & "'); x.setFocus();")

Set focus on the textbox field in datagrid

I have implemented the datagrid with add function successfully. However,
when one record was added in the grid, the cursor was lost focus to
somewhere. is it possible to set focus on the textbox for further adding
record in the datagrid
Million thanksCheck out this articles, it might help you.
http://aspalliance.com/aldotnet/exa...autoscroll.aspx
http://www.dotnetjunkies.com/howto/default.aspx?id=38
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com
"Grey" <erickwyum@.i-cable.com> wrote in message
news:#A2nIPNVEHA.712@.TK2MSFTNGP11.phx.gbl...
> I have implemented the datagrid with add function successfully. However,
> when one record was added in the grid, the cursor was lost focus to
> somewhere. is it possible to set focus on the textbox for further adding
> record in the datagrid
> Million thanks
>

Set focus on the textbox field in datagrid

I have implemented the datagrid with add function successfully. However,
when one record was added in the grid, the cursor was lost focus to
somewhere. is it possible to set focus on the textbox for further adding
record in the datagrid

Million thanksCheck out this articles, it might help you.
http://aspalliance.com/aldotnet/exa...autoscroll.aspx
http://www.dotnetjunkies.com/howto/default.aspx?id=38

--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com

"Grey" <erickwyum@.i-cable.com> wrote in message
news:#A2nIPNVEHA.712@.TK2MSFTNGP11.phx.gbl...
> I have implemented the datagrid with add function successfully. However,
> when one record was added in the grid, the cursor was lost focus to
> somewhere. is it possible to set focus on the textbox for further adding
> record in the datagrid
> Million thanks

set focus on the control

Can I set a specific control on focus? My case is after I click a "Submit" button, I want to set the cursor on a textbox .

Million thanksCheck out this code snippet,
http://www.extremeexperts.com/Net/C...erPostback.aspx

--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com

"Grey" <ericyum@.i-cable.com> wrote in message news:equliHu5DHA.1504@.TK2MSFTNGP12.phx.gbl...
Can I set a specific control on focus? My case is after I click a "Submit" button, I want to set the cursor on a textbox .

Million thanks
This is the code i use. it takes a control and sets the focus.
public static void SetInitialFocus(System.Web.UI.Control control)

{

if (control.Page == null)

{

throw new ArgumentException(

"The Control must be added to a Page before you can set the IntialFocus to it.");

}

if (control.Page.Request.Browser.JavaScript == true)

{

// Create JavaScript

System.Text.StringBuilder s = new System.Text.StringBuilder();

s.Append("\n<SCRIPT LANGUAGE='JavaScript'>\n");

s.Append("<!--\n");

s.Append("function SetInitialFocus()\n");

s.Append("{\n");

s.Append(" document.");

// Find the Form

System.Web.UI.Control p = control.Parent;

while (!(p is System.Web.UI.HtmlControls.HtmlForm))

p = p.Parent;

s.Append(p.ClientID);

s.Append("['");

s.Append(control.UniqueID);

// Set Focus on the selected item of a RadioButtonList

System.Web.UI.WebControls.TextBox rbl = control as System.Web.UI.WebControls.TextBox;

s.Append("'].focus();\n");

s.Append("}\n");

if (control.Page.SmartNavigation)

s.Append("window.setTimeout(SetInitialFocus, 500);\n");

else

s.Append("window.onload = SetInitialFocus;\n");

s.Append("// -->\n");

s.Append("</SCRIPT>");

// Register Client Script

control.Page.RegisterClientScriptBlock("InitialFocus", s.ToString());

}

}

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Grey" <ericyum@.i-cable.com> wrote in message news:equliHu5DHA.1504@.TK2MSFTNGP12.phx.gbl...
Can I set a specific control on focus? My case is after I click a "Submit" button, I want to set the cursor on a textbox .

Million thanks
In code-behind file, register JavaScript code to handle client onclick method of submit button:

e.g. btnSubmit.Attributes.Add("onclick", "OnSubmitTest");

in .aspx file, add following JavaScript method:

function OnSubmitTest()
{
if (null != document.all("txtTextbox"))
document.all("txtTextbox").focus();
}

Tiendq,
tien@.tienonsoftware.com
"Grey" <ericyum@.i-cable.com> wrote in message news:equliHu5DHA.1504@.TK2MSFTNGP12.phx.gbl...
Can I set a specific control on focus? My case is after I click a "Submit" button, I want to set the cursor on a textbox .

Million thanks

Thursday, March 22, 2012

Set Focus to TextBox

I would like to set the focus to a textbox when the page first openes. I have a couple of textboxes on the form and would like the cursor to appear in the first text box when the page opens.

This is the code that I am tring to work with


<td style="WIDTH: 81px; HEIGHT: 27px" align="left">Defect:</td
<td style="WIDTH: 141px; HEIGHT: 27px">
<asp:textbox id="Defect" Runat="server" Width="304px"></asp:textbox></td>

Can anyone help me set the focus to this textbox.

Thanks.Use this JavaScript:


function SetFocusToControl(id) {
document.getElementById(id).focus();
}

In you code, just use the following code:

protected TextBox Defect;

void Page_Load(object sender, EventArgs e) {
Page.RegisterStartUpScript("focusScript", String.Format("<script language=\"JavaScript\">SetFocusToControl('{0}');</script>", Defect.ClientID));
}