Your code snippets are not complete. What I mean with "list only relevant lines" is that you try to narrow down your problem by deleting all lines that have nothing to do with the problem. But I don't mean that you should leave out important lines which HAVE to do with the problem.
If you do this, then the solution comes by itself in most cases. What I try to teach is a principle how to solve problems. If a thirsty man asks me for water and I show him how to dig a well instead, I will help him more than if I give him a small glas of water and the next days he comes to ask me again and again.
But I can see your good spirit in trying, so I will give you the solution and will also discuss your tries and why they probably don't work.
for example here is a code snippet that works. It focuses on the second text box and I verified that it works in my browser (IE). Just open notepad, paste this code below and save it as test.html. Then open with your browser.
- <input type=text id=t1>
-
<input type=text id=t2>
-
<input type=text id=t3>
-
<script language=JavaScript>
-
function setFocus()
-
{
-
document.getElementById("t2").focus();
-
}
-
window.setTimeout("setFocus()", 1);
-
</script>
ok,let's discuss your solutions:
Quote:
Originally Posted by pablorp80
As I said, the problem is that I can't find a way/method for set the cursor and the focus on a textbox (in this case txtGT).
I have tried this in the page_load event as well as in UpdatePanel1_Load and Page_LoadComplete:
[HTML] Page.SetFocus(txtGT)[/HTML]
never get it to work.
Also in the page_load event as well as in UpdatePanel1_Load and Page_LoadComplete:
[HTML] txtGT.Focus()[/HTML]
never get it to work.
where is "txtGT" defined? What is "Page" ? Is this VB or Javascript code? Code snippets should be consistent. that means if you define variables, you must also list the part of the code where they are declared..
Quote:
Originally Posted by pablorp80
Also lots of java scripts like:
[HTML]<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="infcontrol.aspx.vb" Inherits="orders_CutGroupAssign" title="eKanban Ordenes Especiales" %>
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="infcontrol.aspx.vb" Inherits="orders_CutGroupAssign" title="eKanban Ordenes Especiales" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<body onload="document.getElementById("txtGT").focus()"\ >
<asp:UpdateProgress ID="UpdPrgInf" runat="server" AssociatedUpdatePanelID="UpdatePanel1" ...[/HTML]
commands starting with "<%@" and ending with "%>" is serverside code.
You are mixing here serverside and clientside script. It is irrelevant for the problem HOW you generate the HTML on your server. Only the final HTML on your client counts for your problem. You should have chosen "View->Source" in your browser and posted this code instead.
Besides that, it should work if you write
- <body onload="document.getElementById("txtGT").focus()">
The only difference between my example snippet and this way is that if elements are generated dynamic (I mean part of the HTML), and this dynamic part contains the <input type=text id=txtGT> element, then it is not available at onLoad-time and therefor would give an error. Whereas my function window.setTimeout() runs AFTER all other HTML elements have been created and therefore will not crash in such a case.
I can't see the element <input type=text id=txtGT> listed in your code snippet, therefore I don't know if it is there in your original code or not. I mean if you just simply forgot to list it in your code snippet or not. Or you listed it, but you gave it a different ID. or you forgot to close the bracket of the HTML tag. Or you mixed up lowercase and uppercase in your name, or you created it dynamically, or ...You see I must do a lot of guessing.That is what I meant with "list relevant lines".
Quote:
Originally Posted by pablorp80
And it gots me this error: Syntax Error.
Looking at the page source, the error is originated somewhere here:
[HTML]<script type="text/javascript">
//<![CDATA[
Sys.WebForms.PageRequestManager._initialize('ctl00 $ScriptManager1', document.getElementById('aspnetForm'));
Sys.WebForms.PageRequestManager.getInstance()._upd ateControls(['tctl00$ContentPlaceHolder1$UpdatePanel1'], [], [], 90);
//]]>
</script>[/HTML]
"Sys.WebForms.PageRequestManager" tells me that this kind of an error message is thrown when your system tries to generate the HTML on the serverside and has nothing to do with the problem, either. Most likely just some syntax errors in your ASP page or whatever. Because "CDATA" means DTD for XML (which elements are valid and which not) and has nothing to do with HTML.
Quote:
Originally Posted by pablorp80
Also this method: (which is supposed to keep the focus on the last element that had the focus)
- Private Const SCRIPT_DOFOCUS As String = "window.setTimeout('DoFocus()', 1); " & _
-
"Function DoFocus() " & _
-
"{ " & _
-
"try { " & _
-
"document.getElementById('REQUEST_LASTFOCUS').focus(); " & _
-
"} catch (ex) {} " & _
-
"}"
-
Private Sub HookOnFocus(ByVal CurrentControl As Control)
-
If TypeOf CurrentControl Is TextBox Then
-
CType(CurrentControl, WebControl).Attributes.Add("onfocus", "try{document.getElementById('__LASTFOCUS').value=this.id} catch(e) {}")
-
End If
-
If CurrentControl.HasControls Then
-
For Each CurrentChildControl As Control In CurrentControl.Controls
-
HookOnFocus(CurrentChildControl)
-
Next
-
End If
-
End Sub
-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
If Not Page.IsPostBack Then
-
HookOnFocus(CType(Me.Page, Control))
-
End If
-
ScriptManager.RegisterStartupScript(Me, Me.GetType, "ScriptDoFocus", SCRIPT_DOFOCUS.Replace("REQUEST_LASTFOCUS", Request("__LASTFOCUS")), True)
-
End Sub
This dropped me the following error and never worked:
Expected ';' (can't find where the ';' is expected by the way)
This is Visual basic script. It only runs in IE. If you use Firefox or any other browser, it won't run. Maybe this is your problem.
Only the script is given.But not the needed HTML where this script works on. Therefore this snipped is incomplete.
I can only guess again, that you may have not put the code for the text-input element with the Id="'REQUEST_LASTFOCUS'". I mean in your snippets above you gave it the ID="txtGT" but "txtGT" is nowwhere to find in this snippet. Only I can see
- document.getElementById('REQUEST_LASTFOCUS').focus();