Well I wasn't to sure if post this on the javascript forum or here, but since is an error that probably I got because something related to my ASP.Net code I decided to posted here.
Ok my problem is that I need to set focus on a textbox everytime the page is loaded. Maybe you already thinking what I thought at first, that with a textbox.setFocu s() that will do it. Nop this is .Net Framework 1.1 and textbox.setFocu s() didn't exist back then. So I did the other simple thing add a javascript on body onload="documen t.getElementByI d(<%= txtPCode.Client ID %>).focus(). It works on page load but not in postback. I really have try a couple of other things like adding this function in the .vb -
-
Private Sub SetFocus(ByVal ClientID As String)
-
Dim Script As New System.Text.StringBuilder
-
'Dim ClientID As String = FocusControl.ClientID
-
With Script
-
.Append("<script language='javascript'>")
-
.Append("document.getElementById('")
-
.Append(ClientID)
-
.Append("').focus();")
-
.Append("</script>")
-
End With
-
RegisterStartupScript("setFocus", Script.ToString())
-
End Sub
-
and calling it in from the Page_Load like this SetFocus(textbo x.ClientID).
Anyway nothing of this have work to set the focus of the textbox on the page post back.
Anyone have another idea?
Thanks,
Erick
Jul 20 '07
24 3924 Frinavale 9,735
Recognized Expert Moderator Expert
Why all this talk of hidden textboxes?
He knows the name of the textbox.
It's not hidden.
mynothiddentext box.focus()
(Assuming one uses the NAME property, otherwise you'll need the getElementById( ))
Hmm I had to read the question again.
Maybe this person's using an Ajax call to do a partial page update?
I'm tired of looking for the problem. I didn't create the code, it was the guy before me so maybe he have something that it's not allowing this to work. Here is the code if anyone can figured it out. -
Imports System.Data.Odbc
-
-
Namespace ASPNET.StarterKit.Portal
-
-
Public Class ProductLookUpPage
-
'Inherits System.Web.UI.Page
-
Inherits ASPNET.StarterKit.Portal.PortalPage
-
Protected WithEvents lnkReturn0 As System.Web.UI.WebControls.LinkButton
-
Protected WithEvents lnkReturn1 As System.Web.UI.WebControls.LinkButton
-
-
Protected WithEvents phBanner As System.Web.UI.WebControls.PlaceHolder
-
Protected WithEvents lblTitle As System.Web.UI.WebControls.Label
-
Protected WithEvents cmdLocate As System.Web.UI.WebControls.Button
-
Protected WithEvents txtPCode As System.Web.UI.WebControls.TextBox
-
Protected WithEvents vsPCode As System.Web.UI.WebControls.ValidationSummary
-
Protected WithEvents rfvPCode As System.Web.UI.WebControls.RequiredFieldValidator
-
Protected WithEvents revPCode As System.Web.UI.WebControls.RegularExpressionValidator
-
Protected WithEvents cvPCode As System.Web.UI.WebControls.CustomValidator
-
Protected WithEvents dlProductNumbers As System.Web.UI.WebControls.DataList
-
Protected WithEvents lblPCodeValue As System.Web.UI.WebControls.Label
-
Protected WithEvents lblDescValue As System.Web.UI.WebControls.Label
-
Protected WithEvents lblSizeValue As System.Web.UI.WebControls.Label
-
Protected WithEvents lblWHHead As System.Web.UI.WebControls.Label
-
Protected WithEvents lblWHQty As System.Web.UI.WebControls.Label
-
Protected WithEvents lblStoreHead As System.Web.UI.WebControls.Label
-
Protected WithEvents lblStoreQty As System.Web.UI.WebControls.Label
-
Protected WithEvents btnPriceIt As System.Web.UI.HtmlControls.HtmlInputButton
-
Protected WithEvents spnContent As System.Web.UI.HtmlControls.HtmlGenericControl
-
-
#Region " Web Form Designer Generated Code "
-
-
'This call is required by the Web Form Designer.
-
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
-
-
End Sub
-
-
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
-
'CODEGEN: This method call is required by the Web Form Designer
-
'Do not modify it using the code editor.
-
InitializeComponent()
-
End Sub
-
-
#End Region
-
-
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
Trace.Warn("Default.aspx.vb", "Called Page_Load()")
-
'DO NOT remove this line
-
'This is what loads the banner into the top
-
phBanner.Controls.Add(Page.LoadControl("../../../DesktopPortalBanner.ascx"))
-
-
'07/20/07
-
SetFocus(txtPCode.ClientID)
-
-
'Counteract the DHTML onclick event assigned to the Look-Up Command Button
-
txtPCode.Attributes.Add("onfocus", "document.forms(0).cmdLookup.disabled = false;")
-
-
' 03/16/05 WNS Added PriceIt
-
btnPriceIt.Visible = False
-
Page.RegisterHiddenField("__EVENTTARGET", cmdLocate.ClientID)
-
If Page.IsPostBack = False Then
-
' Store URL Referrer to return to portal
-
Try
-
ViewState("UrlReferrer") = Request.UrlReferrer.ToString()
-
Catch ex As Exception
-
Dim clsApp As New CustomAppSettings
-
ViewState("UrlReferrer") = clsApp.GetAppSetting("StoreTab_Url")
-
End Try
-
-
Page.RegisterHiddenField("__EVENTTARGET", cmdLocate.ClientID)
-
-
If Request.Params("pcode") <> "" Then
-
Dim clsProducts As New ABCProducts
-
If clsProducts.IsValidPCode(Request.Params("pcode")) Then
-
txtPCode.Text = Request.Params("pcode")
-
LocateProduct()
-
End If
-
End If
-
-
End If
-
RegisterStartupScript("SetFocus", ("<script>document.getElementById('txtPCode').focus();</script>"))
-
-
End Sub
-
-
'DO NOT remove this return procedure
-
Private Sub lnkReturn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkReturn0.Click, lnkReturn1.Click
-
'Return to Portal
-
Response.Redirect(CType(ViewState("UrlReferrer"), String))
-
End Sub
-
-
Private Sub cmdLocate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLocate.Click
-
'A PCode has been entered and the submit button clicked
-
-
'Query the SIRBALP by PCode and load into DataSet
-
If Page.IsValid Then
-
LocateProduct()
-
End If
-
-
End Sub
-
-
Public Sub ValidatePCode(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
-
Try
-
Dim intTest As Integer = txtPCode.Text
-
Catch ex As Exception
-
e.IsValid = False
-
Exit Sub
-
End Try
-
-
'July 11, 2007
-
'Validates PCODE against AS400 to see if is valid
-
Dim sqlCmd As String = "SELECT * FROM ABCPROD.INVMSTP WHERE (PCODE = " & txtPCode.Text & ") "
-
Dim conn As New OdbcConnection(System.Configuration.ConfigurationSettings.AppSettings("AS400"))
-
Dim cmd As New OdbcCommand(sqlCmd, conn)
-
conn.Open()
-
Dim AS400reader As OdbcDataReader = cmd.ExecuteReader
-
-
AS400reader.Read()
-
-
If AS400reader.HasRows Then
-
e.IsValid = True
-
Else
-
e.IsValid = False
-
End If
-
End Sub
-
-
Private Sub LocateProduct()
-
txtPCode.Text.Trim()
-
-
Dim sqlCmd As String = "SELECT STORE_NO, PCODE, END_QTY, S#STAT " & _
-
"FROM ABCPROD.SIRBALP, ABCPROD.STRMSTP " & _
-
"WHERE ((ABCPROD.SIRBALP.STORE_NO = ABCPROD.STRMSTP.STRNO) " & _
-
"AND (S#STAT = 'A')) " & _
-
"AND (PCODE = " & txtPCode.Text & ") " & _
-
"AND (STORE_NO <> 10) " & _
-
"AND (STORE_NO <> 11) " & _
-
"AND (STORE_NO < 219) " & _
-
"AND (END_QTY > 0) " & _
-
"ORDER BY STORE_NO, PCODE, END_QTY"
-
-
Dim conn As New OdbcConnection(System.Configuration.ConfigurationSettings.AppSettings("AS400"))
-
Dim cmd As New OdbcCommand(sqlCmd, conn)
-
cmd.CommandType = CommandType.Text
-
Dim da As New OdbcDataAdapter(cmd)
-
Dim ds As New DataSet
-
da.Fill(ds)
-
-
cmd.CommandText = "SELECT PCODE, ASIZE, DESC, OHQTY " & _
-
"FROM ABCPROD.INVMSTP " & _
-
"WHERE (PCODE = " & txtPCode.Text & ") "
-
da.SelectCommand = cmd
-
da.Fill(ds, "Product")
-
-
lblPCodeValue.Text = ds.Tables("Product").Rows(0).Item("PCODE")
-
lblDescValue.Text = ds.Tables("Product").Rows(0).Item("DESC")
-
lblSizeValue.Text = ds.Tables("Product").Rows(0).Item("ASIZE")
-
lblWHQty.Text = ds.Tables("Product").Rows(0).Item("OHQTY")
-
-
lblWHHead.Visible = True
-
lblWHQty.Visible = True
-
-
' if the requestor is at a store PC, then...
-
Dim vlVisitor As New ABCNetDepartmentMenu(Request.ServerVariables("remote_addr"))
-
If vlVisitor.IsStore Then
-
Dim cmdStore As New OdbcCommand("SELECT STORE_NO, PCODE, END_QTY " & _
-
"FROM ABCPROD.SIRBALP " & _
-
"WHERE STORE_NO = " & vlVisitor.StoreNumber & _
-
" AND PCODE = " & txtPCode.Text, conn)
-
Dim daStore As New OdbcDataAdapter(cmdStore)
-
conn.Open()
-
Dim dr As OdbcDataReader = cmdStore.ExecuteReader
-
-
lblStoreQty.Text = 0
-
While dr.Read
-
lblStoreQty.Text = dr.Item("END_QTY")
-
End While
-
-
daStore.Dispose()
-
cmdStore.Dispose()
-
conn.Dispose()
-
-
lblStoreHead.Visible = True
-
lblStoreQty.Visible = True
-
End If
-
vlVisitor.Dispose()
-
-
dlProductNumbers.DataSource = ds.Tables(0)
-
dlProductNumbers.DataBind()
-
-
ds.Dispose()
-
da.Dispose()
-
cmd.Dispose()
-
conn.Dispose()
-
-
' 03/16/05 WNS Added PriceIt
-
With btnPriceIt
-
.Visible = True
-
.Attributes.Add("onclick", "NewWindow('PriceIt.aspx?pcode=" & txtPCode.Text & "','PriceLookUp',350,350,0,'center');")
-
End With
-
-
'6/29/07 Clear textbox
-
txtPCode.Text = ""
-
SetFocus(txtPCode.ClientID)
-
End Sub
-
-
'07/20/07 This procedure sets focus to specific controls.
-
Private Sub SetFocus(ByVal ClientID As String)
-
Dim Script As New System.Text.StringBuilder
-
'Dim ClientID As String = FocusControl.ClientID
-
With Script
-
.Append("<script language='javascript'>")
-
.Append("document.getElementById('")
-
.Append(ClientID)
-
.Append("').focus();")
-
.Append("</script>")
-
End With
-
RegisterStartupScript("setFocus", Script.ToString())
-
End Sub
-
End Namespace
-
Thanks
Frinavale 9,735
Recognized Expert Moderator Expert
I'm tired of looking for the problem. I didn't create the code, it was the guy before me so maybe he have something that it's not allowing this to work. Here is the code if anyone can figured it out.
I remember having a similar problem to you in the past.
From what I remember, the problem was that the RegisterStartup Script method puts the JavaScript code in the <Body> portion of the Html. In order for the body to call your JavaScript function during the page's onload event, the JavaScript must exist in the <head> section of your Html.
What I to solve this solution was to put my JavaScript into an external JavaScript file hard code the import into the <head> section of my Html.
You don't need to put the JavaScript into an external file, but I'm almost certain that the JavaScript has to be defined in the <head> section in order to make the JavaScript call during your page's onload event.
eg: -
-
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
-
<HTML>
-
<HEAD>
-
<title>My Page's Title</title>
-
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
-
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
-
<meta content="VBScript" name="vs_defaultClientScript">
-
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
-
<LINK href="Styles.css" type="text/css" rel="stylesheet">
-
<script type="text/javascript">
-
onload = function()
-
{
-
document.getElementById('myTextbox').focus();
-
}
-
</script>
-
-
</HEAD>
-
Have you tried hard coding the JavaScript into the <head> section to see if this works?
-Frinny
I have tried that, don't work for me. This is how my script section on the page looks. -
<script language="javascript">
-
-
var win=null;
-
-
function NewWindow(mypage,myname,w,h,scroll,pos)
-
{
-
if(win!=null && win.open) win.close()
-
-
if(pos=="random")
-
{
-
LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
-
TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
-
}
-
-
if(pos=="center")
-
{
-
LeftPosition=(screen.width)?(screen.width-w)/2:100;
-
TopPosition=(screen.height)?(screen.height-h)/2:100;
-
}
-
else if((pos!="center" && pos!="random") || pos==null)
-
{
-
LeftPosition=0;TopPosition=20;
-
}
-
-
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
-
win=window.open(mypage,myname,settings);
-
-
if(win.focus)
-
{
-
win.focus();
-
}
-
}
-
-
function CloseNewWin()
-
{
-
if(win!=null && win.open) win.close();
-
}
-
-
onload = function()
-
{
-
document.getElementById('txtPCode').focus();
-
}
-
</script>
-
Frinavale 9,735
Recognized Expert Moderator Expert
I have tried that, don't work for me. This is how my script section on the page looks.
Are you getting an JavaScript errors on the page at all?
Like an "object expected" error or the like?
Plater 7,872
Recognized Expert Expert
As much as I hate to admit it (:-P) if you run it in firefox with that DOM parser thing on you will see a lot more hidden errors.
Are you remembering to have your body tag's onload function call out to that javascript onload thing
Frinavale 9,735
Recognized Expert Moderator Expert
I'm going to ask one more small but vital question.
Are you using any use of Ajax in your html design?
Are you using the AjaxControlTool kit's UpdatePanel?
I just tried with firefox and it works, but not with Internet explorer. I didn't get any javascript error. I don't get it, whats wrong with IE (lol) then.
I'm not using AJAX in this application, this is an old application, it was develop long time before I started working here.
Frinavale 9,735
Recognized Expert Moderator Expert
I just tried with firefox and it works, but not with Internet explorer. I didn't get any javascript error. I don't get it, whats wrong with IE (lol) then.
I'm not using AJAX in this application, this is an old application, it was develop long time before I started working here.
Have you checked out w3c?
Have you seen their focus() method example?
I tried their example in IE and it works fine....
Maybe you might find then answer in w3c.
Have you checked out w3c?
Have you seen their focus() method example?
I tried their example in IE and it works fine....
Maybe you might find then answer in w3c.
It doesn't seem to work with a asp button, I got this error:
Compiler Error Message: BC30390: 'ASPNET.Starter Kit.Portal.Prod uctLookUpPage.P rivate Sub SetFocus(Client ID As String)' is not accessible in this context because it is 'Private'.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Marco Liedekerken |
last post by:
Hi,
Is it possible to retrieve the control that had the focus when the page was
posted back?
Because the focus is lost when a postback occurs I want to manually set the
focus to the control that previously had the focus (smartnavigation doesn't
do the trick).
Thanks, Marco
|
by: Elliot M. Rodriguez |
last post by:
Is it possible to change a control's focus at runtime? I'm sure you can....
I have a form with 2 textbox controls that cause postbacks. They are located
in the middle of my form.
When a postback occurs, no control has the focus. I would like it to be so
that the control that causes the postback keeps the focus. Since I have 2
controls that potentally need to retain focus, using Javascript in this case
is a PIA.
|
by: Mike |
last post by:
I have a few textbox controls that have autopostback so that when they loose
focus they update a label control that shows the count of characters in
their respective text control. This works fine, except that after the
postback, the page is shown reset -- scrolled to the top and with no control
having focus. Can I maintain scroll/focus w/o writing client-side
scripting?
thanks
|
by: Dexter |
last post by:
Hello All,
I have a web control that when receive the focus, a postback is called,
using getPostBackEventReference. But, when the PostBack is called, i want
that the focus goes to the web control.
Somebody know how to make this?
Dexter
|
by: psual |
last post by:
Hi
I have a page (1st page) with a button. this button opens a new page (2nd
page) in full-screen mode.
The problem is that the 2nd page is always loaded before the postback of the
1st page, so the 1st page takes back the focus instead of remaining behind
the 2nd page.
Even if the 2nd page takes time to render, the 1st page waits for its
| |
by: Finn Stampe Mikkelsen |
last post by:
Hi
How can i set a focus to a textbox in my codebehind page??
I have this WebForm, that takes information from a user and 2 buttons on the
form. One that takes action on the entered information and one that resets
the form...
The second on, the reset button, i would like to set focus back to the first
textbox on the form. I already have an onload in my body, which sets the
|
by: Rey |
last post by:
Howdy all.
Using visual web developer (VB) on xp pro box.
My problem with with a web form that on accessing the calendar control
causes a postback that moves the cursor back to the txtFirstName
field.
Form uses the Page_LoadComplete in order to access the last employeeID
# in a dataview which is populated by an sqlDataObject querying an SQL
Server dbs.
|
by: BizEd |
last post by:
I have a textbox that fires an autopostback when filled in. The next
field after the textbox is a RadioButtonList which I set focus to.
The radiobutton list does have focus and activates when you hit the
spacebar, but the little selection box does not appear around the
first item like it does when you tab to a radiobutton list. So in
essence, the user has no idea where they are on the screen. I've
tried both the focus() method and...
|
by: Mel |
last post by:
I have several text boxes and drop-down lists in an AJAX Update
Panel. All user inputs have the Postback property set to True. After
I type something in the first input entry and press the "Tab" key how
can I set the focus to the next box after the postback? Please help!
Using Visual Studio 2005 Pro, Asp.net 2.0, vb.net, WinXP
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |