Monday, March 26, 2012

Set Focus

Hi, i'm working on MS visual.net using C# asp.net web application. Can someone help with the code to set focus on a textbox when the page is load?
I tried many ways including the examples in metabuilders.com but just couldn't do it.
Wat is the code that need to be insert during pageload.

Hope someone will help me with this. Thanks.

RegisterStartupScript("myfocus", "<SCRIPT>document.all." + TextBox1.ClientID + ".focus();</SCRIPT>");

Thanks for your input. But where do i place this code in? is it in the pageload part? Because there is an error message.

Thank you.
what is the error?
pls post code and error.
the same

in vb .net
'write this function in side server code

Private Sub mySetFocus(ByVal ctrl As System.Web.UI.Control)

Dim strFocus As String
strFocus = strFocus + "<SCRIPT language='javascript'>"
strFocus = strFocus + "document.getElementById('" & ctrl.ID & "').focus();"
'if want select
strFocus = strFocus + "if ( document.getElementById('" & ctrl.ID & "').select != null ){document.getElementById('" & ctrl.ID & "').select();}"

strFocus = strFocus + "</SCRIPT>"
RegisterStartupScript("mySetFocus", strFocus)

End Sub

'call whit control for focus
call mySetFocus(txtControl)
Is there any way that we can do this without using javascript?
AFAIK, that's a client side / browser issue,
so Javascript can utimately do this in ASP.NET
hi ChinHan:

If you want set focus to a textbox when the page load,

if so txtbox is a textbox control that if you want , you can do like this

<body onload="javascript:document.all.txtbox.focus();"></body>
Thanks.. rox.scott. manage to solve the problem.. Thanks a lot.

I have another qns.. after i enter the data in the textbox, i have a search button to find the information.. but i can't press the ENTER key to search it. i need to use a mouse click.. how do i solve this.. In windows form there is no problem but in asp.net web application this problem occurs..

Thanks!
Hello,

that is another task where you can use javascript to accomplish this. In my opinion one of the best way to solve this has made Janus Kamp Hansen with his script. I want to show you this in the following example:

Public Sub DefaultButton(ByRef Page As System.Web.UI.Page, ByRef objTextControl As TextBox, ByRef objDefaultButton As Button)

' Sets default buttons.
' Created by Janus Kamp Hansen - http://www.kamp-hansen.dk
Dim sScript As New System.Text.StringBuilder()

sScript.Append("<SCRIPT language=""javascript"">" & vbCrLf)
sScript.Append("function fnTrapKD(btn){" & vbCrLf)
sScript.Append(" if (document.all){" & vbCrLf)
sScript.Append(" if (event.keyCode == 13)" & vbCrLf)
sScript.Append(" { " & vbCrLf)
sScript.Append(" event.returnValue=false;" & vbCrLf)
sScript.Append(" event.cancel = true;" & vbCrLf)
sScript.Append(" btn.click();" & vbCrLf)
sScript.Append(" } " & vbCrLf)
sScript.Append(" } " & vbCrLf)
sScript.Append("}" & vbCrLf)
sScript.Append("</SCRIPT>" & vbCrLf)

objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." & objDefaultButton.ClientID & ")")
Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString)

End Sub

Put this code in your code-behind module and in the page load event you can define the default buttons for the ENTER-Key like in the following sample:
 DefaultButton(Me.Page, Me.txtField, Me.cmdDefaultButton)
For any explanation please look at the web-address in the code...

HTH,
If you are using ServerSide button control, then if you have your code in buttonX_Click event then on click on that buitton only that event fires.

But, there is an alternative solution for this.

If you enter data in TextBox and press Enter, then again Page_Load event will fires as my understanding.

So what you have to do is

on Page_Load check if the textbox has value or not, if the textbox has value then call your button_click event

Ex:

on page load event

If len(textbox1.text) > 0 Then
Button1_Click(sender,e)
End if

this kind of logic might helps to your problem!
hi kbrocksi SEC.. i'm working on a C# project. Do u have it in C#? thanks!
Hi, I am having the opposite problem...I have multiple textboxes on my page, and I have set focus to the first one...but when the user presses the enter key it fires the btnEdit_Click event...causing all kinds of issues. How do I stop it from running the button click and make it set focus to the next string??

I've tried capturing the keypress event of the form, to simply move it to another control, but that doesn't seem to work either.
here's the code:


frmMain.Attributes.Add("onkeypress", "frmMain_keyPress;")

Dim sbJavaFunction As StringBuilder = Nothing
sbJavaFunction = New StringBuilder

sbJavaFunction.Append("<script language=""javascript"">function frmMain_keyPress()")
sbJavaFunction.Append("{frmmain.lblTest.Text='hi';")
sbJavaFunction.Append("if (event.keyCode == 13){")
'sbJavaFunction.Append("event.cancelBubble = true;")
'sbJavaFunction.Append("event.returnValue = false;")
sbJavaFunction.Append("document.forms[0].txtTotalPairs.Focus;")
'sbJavaFunction.Append("document.forms[0].txtTurn_TextChanged();")
sbJavaFunction.Append("}}</script>")
Page.RegisterClientScriptBlock("HandleEnterKey", sbJavaFunction.ToString())
sbJavaFunction = Nothing

any ideas would be helpful!
Chinhan...here is the translation of the above code:

private void Page_Load(object sender, System.EventArgs e)
{
DefaultButton(this.Page, this.txtField, this.cmdDefaultButton);
}

public void DefaultButton(Page Page, TextBox objTextControl, Button objDefaultButton)
{
// Sets default buttons.
// Created by Janus Kamp Hansen - http://www.kamp-hansen.dk
System.Text.StringBuilder sScript = new System.Text.StringBuilder();

sScript.Append("<SCRIPT language='javascript'>\n");
sScript.Append("function fnTrapKD(btn){\n");
sScript.Append(" if (document.all){\n");
sScript.Append(" if (event.keyCode == 13)\n");
sScript.Append(" { \n");
sScript.Append(" event.returnValue=false;\n");
sScript.Append(" event.cancel = true;\n");
sScript.Append(" btn.click();\n");
sScript.Append(" } \n");
sScript.Append(" } \n");
sScript.Append("}\n");
sScript.Append("</SCRIPT>\n");

objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." + objDefaultButton.ClientID + ")");
Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString());
}


Daisy, why not use theonblur event

0 comments:

Post a Comment