473,698 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How To? - Use Javascript To Test If a TextBox Exists On a Page

I have a home page with username and password textboxes and a login
button for purposes of users being able to log in (forms
authentication) directly on the site home page. I also have a dedicated
"Login.aspx " page, but I don't want users redirected there unless
absolutely necessary.

I have registered a javascript block via "Page.RegisterS tartupScript"
that sets focus to the "txtUserNam e" textbox control on Page Load. It
works great for IE and Firefox.

But...

The problem is that the "txtUserNam e" textbox does not exist once the
user has logged in and the page posts back. The code behind then knows
that the user is authenticated, and so it does a ".Visible=false " for
the "txtUserNam e", "txtPasswor d" and "btnLogin" controls.

Then, when the page loads after the postback, the javascript block
causes an error icon in the lower left corner of the browser.
Apparently it is still trying to set focus to the control? I thought I
had this fixed when I set the code behind to register the page startup
script only when the page loaded for the first time...but that didn't
work for some reason, because it still tries to set focus on postbacks.
I also tried disabling the page viewstate along with the above, but
that didn't work either.

Can the javascript block be coded to test if the "txtUserNam e"
(document.txtUs erName ?) textbox exists before trying to set focus to
it? If so then that would do it...but how would the code look? Also, is
there another solution that I am not seeing here? I need to get this
working correctly.

Any suggestions are appreciated. Thanks.

Nov 19 '05 #1
7 4357
var txtBox=document ,getElementById ( "txtUserNam e");
if (txtBox!=null)
txtBox.focus();
Eliyahu

<jo*********@to pscene.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I have a home page with username and password textboxes and a login
button for purposes of users being able to log in (forms
authentication) directly on the site home page. I also have a dedicated
"Login.aspx " page, but I don't want users redirected there unless
absolutely necessary.

I have registered a javascript block via "Page.RegisterS tartupScript"
that sets focus to the "txtUserNam e" textbox control on Page Load. It
works great for IE and Firefox.

But...

The problem is that the "txtUserNam e" textbox does not exist once the
user has logged in and the page posts back. The code behind then knows
that the user is authenticated, and so it does a ".Visible=false " for
the "txtUserNam e", "txtPasswor d" and "btnLogin" controls.

Then, when the page loads after the postback, the javascript block
causes an error icon in the lower left corner of the browser.
Apparently it is still trying to set focus to the control? I thought I
had this fixed when I set the code behind to register the page startup
script only when the page loaded for the first time...but that didn't
work for some reason, because it still tries to set focus on postbacks.
I also tried disabling the page viewstate along with the above, but
that didn't work either.

Can the javascript block be coded to test if the "txtUserNam e"
(document.txtUs erName ?) textbox exists before trying to set focus to
it? If so then that would do it...but how would the code look? Also, is
there another solution that I am not seeing here? I need to get this
working correctly.

Any suggestions are appreciated. Thanks.

Nov 19 '05 #2
I am not sure what the actual problem is. I understand what you are trying to
do, but not the why you are trying to do it.

Is the problem, there are certain pages that a user should not have to log
in to use? If so, restrict those pages by turning authentication off for
those pages. You set up a separate section of the web.config to exclude those
pages. Conversely, you can restrict the small number of login pages, if they
are fewer.

Is the problem, depending on how a user gets into the app they may be
authenticated by the network. If so, set up the login cookie when you find
those types of users and they will never be redirected to the login.aspx page.

What am I getting at? You are asking how to do something that is rather
kludgy and it is more likely you are doing this out of a misunderstandin g of
how ASP.NET authentication works rather than a real need to do it this way.
By rephrasing the why, you might find that you do not have to go to the
trouble of adding JavaScript to the login page to hide controls from the user.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************** ************
Think Outside the Box!
*************** ************
"jo*********@to pscene.com" wrote:
I have a home page with username and password textboxes and a login
button for purposes of users being able to log in (forms
authentication) directly on the site home page. I also have a dedicated
"Login.aspx " page, but I don't want users redirected there unless
absolutely necessary.

I have registered a javascript block via "Page.RegisterS tartupScript"
that sets focus to the "txtUserNam e" textbox control on Page Load. It
works great for IE and Firefox.

But...

The problem is that the "txtUserNam e" textbox does not exist once the
user has logged in and the page posts back. The code behind then knows
that the user is authenticated, and so it does a ".Visible=false " for
the "txtUserNam e", "txtPasswor d" and "btnLogin" controls.

Then, when the page loads after the postback, the javascript block
causes an error icon in the lower left corner of the browser.
Apparently it is still trying to set focus to the control? I thought I
had this fixed when I set the code behind to register the page startup
script only when the page loaded for the first time...but that didn't
work for some reason, because it still tries to set focus on postbacks.
I also tried disabling the page viewstate along with the above, but
that didn't work either.

Can the javascript block be coded to test if the "txtUserNam e"
(document.txtUs erName ?) textbox exists before trying to set focus to
it? If so then that would do it...but how would the code look? Also, is
there another solution that I am not seeing here? I need to get this
working correctly.

Any suggestions are appreciated. Thanks.

Nov 19 '05 #3
Gregory, I think it is you that does not understand. While I know that
I have a lack of understanding about some things in asp.net (like
everyone else?), I am fairly confident that I do not have a
misunderstandin g! :-)

In my original post I never said anything about using javascript to
hide controls...that would not make any sense here. Maybe I just worded
it in a confusing way...if so then I am sorry.

I am only using javascript to set focus to my txtUserName textbox. The
hiding of controls is occuring in the code behind once the user logs
in, as it should. The problem is that the javascript continues to try
to set focus once the page does a post back and the controls are hidden
(null). This generates the error. I simply wanted a javascript sample
that could be used to test for the controls on the page before
attempting to set focus.

I think that Eliyahu's post above will do just that.

Anyways, thanks a bunch to both of you!

JP>

Nov 19 '05 #4
You can also do

if(typeof(txtUs erName) != "undefined" )
txtBox.focus();

Sreejith

"Eliyahu Goldin" wrote:
var txtBox=document ,getElementById ( "txtUserNam e");
if (txtBox!=null)
txtBox.focus();
Eliyahu

<jo*********@to pscene.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I have a home page with username and password textboxes and a login
button for purposes of users being able to log in (forms
authentication) directly on the site home page. I also have a dedicated
"Login.aspx " page, but I don't want users redirected there unless
absolutely necessary.

I have registered a javascript block via "Page.RegisterS tartupScript"
that sets focus to the "txtUserNam e" textbox control on Page Load. It
works great for IE and Firefox.

But...

The problem is that the "txtUserNam e" textbox does not exist once the
user has logged in and the page posts back. The code behind then knows
that the user is authenticated, and so it does a ".Visible=false " for
the "txtUserNam e", "txtPasswor d" and "btnLogin" controls.

Then, when the page loads after the postback, the javascript block
causes an error icon in the lower left corner of the browser.
Apparently it is still trying to set focus to the control? I thought I
had this fixed when I set the code behind to register the page startup
script only when the page loaded for the first time...but that didn't
work for some reason, because it still tries to set focus on postbacks.
I also tried disabling the page viewstate along with the above, but
that didn't work either.

Can the javascript block be coded to test if the "txtUserNam e"
(document.txtUs erName ?) textbox exists before trying to set focus to
it? If so then that would do it...but how would the code look? Also, is
there another solution that I am not seeing here? I need to get this
working correctly.

Any suggestions are appreciated. Thanks.


Nov 19 '05 #5
Ideally, you should remove the scripts that are not needed for the current
execution of the page.

On page_load, if the request is a not a postback, attach the scripts to the
startup for the textbox focus. If the request is a postback, then don't
attach the scripts.
--
Direct Email: Michael.Baltic@ RemoveCharacter sUpTo#NCMC.Com

Staff Consultant II
Enterprise Web Services
Cardinal Solutions Group
"Sreejith Ram" wrote:
You can also do

if(typeof(txtUs erName) != "undefined" )
txtBox.focus();

Sreejith

"Eliyahu Goldin" wrote:
var txtBox=document ,getElementById ( "txtUserNam e");
if (txtBox!=null)
txtBox.focus();
Eliyahu

<jo*********@to pscene.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I have a home page with username and password textboxes and a login
button for purposes of users being able to log in (forms
authentication) directly on the site home page. I also have a dedicated
"Login.aspx " page, but I don't want users redirected there unless
absolutely necessary.

I have registered a javascript block via "Page.RegisterS tartupScript"
that sets focus to the "txtUserNam e" textbox control on Page Load. It
works great for IE and Firefox.

But...

The problem is that the "txtUserNam e" textbox does not exist once the
user has logged in and the page posts back. The code behind then knows
that the user is authenticated, and so it does a ".Visible=false " for
the "txtUserNam e", "txtPasswor d" and "btnLogin" controls.

Then, when the page loads after the postback, the javascript block
causes an error icon in the lower left corner of the browser.
Apparently it is still trying to set focus to the control? I thought I
had this fixed when I set the code behind to register the page startup
script only when the page loaded for the first time...but that didn't
work for some reason, because it still tries to set focus on postbacks.
I also tried disabling the page viewstate along with the above, but
that didn't work either.

Can the javascript block be coded to test if the "txtUserNam e"
(document.txtUs erName ?) textbox exists before trying to set focus to
it? If so then that would do it...but how would the code look? Also, is
there another solution that I am not seeing here? I need to get this
working correctly.

Any suggestions are appreciated. Thanks.


Nov 19 '05 #6
I agree with totally...and I tried that. I put the
Page.RegisterSt artupScript code line within the "if(!Page.IsPos tBack)
block. However, that did not work for some reason. The script continued
to be emitted within the HTML for postbacks. I am not sure why.

But, I did get it working properly. Thanks.

Nov 19 '05 #7
I never liked the idea of attaching scripts from the server code. If the
script is short, you won't feel any difference if it or there or not. If it
is long, say more then 20 lines of code, you can put it in a separate file
that will be cached on client side.

Eliyahu

"Michael Baltic" <Mi***********@ discussions.mic rosoft.com> wrote in message
news:B9******** *************** ***********@mic rosoft.com...
Ideally, you should remove the scripts that are not needed for the current
execution of the page.

On page_load, if the request is a not a postback, attach the scripts to the startup for the textbox focus. If the request is a postback, then don't
attach the scripts.
--
Direct Email: Michael.Baltic@ RemoveCharacter sUpTo#NCMC.Com

Staff Consultant II
Enterprise Web Services
Cardinal Solutions Group
"Sreejith Ram" wrote:
You can also do

if(typeof(txtUs erName) != "undefined" )
txtBox.focus();

Sreejith

"Eliyahu Goldin" wrote:
var txtBox=document ,getElementById ( "txtUserNam e");
if (txtBox!=null)
txtBox.focus();
Eliyahu

<jo*********@to pscene.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
> I have a home page with username and password textboxes and a login
> button for purposes of users being able to log in (forms
> authentication) directly on the site home page. I also have a dedicated > "Login.aspx " page, but I don't want users redirected there unless
> absolutely necessary.
>
> I have registered a javascript block via "Page.RegisterS tartupScript" > that sets focus to the "txtUserNam e" textbox control on Page Load. It > works great for IE and Firefox.
>
> But...
>
> The problem is that the "txtUserNam e" textbox does not exist once the > user has logged in and the page posts back. The code behind then knows > that the user is authenticated, and so it does a ".Visible=false " for > the "txtUserNam e", "txtPasswor d" and "btnLogin" controls.
>
> Then, when the page loads after the postback, the javascript block
> causes an error icon in the lower left corner of the browser.
> Apparently it is still trying to set focus to the control? I thought I > had this fixed when I set the code behind to register the page startup > script only when the page loaded for the first time...but that didn't > work for some reason, because it still tries to set focus on postbacks. > I also tried disabling the page viewstate along with the above, but
> that didn't work either.
>
> Can the javascript block be coded to test if the "txtUserNam e"
> (document.txtUs erName ?) textbox exists before trying to set focus to > it? If so then that would do it...but how would the code look? Also, is > there another solution that I am not seeing here? I need to get this
> working correctly.
>
> Any suggestions are appreciated. Thanks.
>

Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
5472
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net that works well as an html page. It brings up a modal popup window that I have been trying to work out for days now and this was the closest I have been able to come. I added a little asp.net code and an asp.net button and cannot get it to
0
1526
by: Caesar Augustus | last post by:
I'm having a problem with two different javascript controls in my app. The first chuck of javascript that I pasted into my app is the client-side calendar control popup which works fine when first used. The second piece of javascript pasted into the app serves as a data validation message box that fires after some server-side code executes (if a certain error condition exists). This action is tied to a submit button that upon NO error...
10
4696
by: Shadow Lynx | last post by:
That subject packs a whallop, so let me explain in better detail what's happening and how it relates to ASPX pages... In a nutshell, if the first <script /on a page is of type "text/vbscript", you cannot use inline JavaScript statements that call setTimeout with functions that start with a double-underscore. This is very relavant to ASPX (ASP Dot Net) pages because it means that AutoPostBacks will fail since they generally call the...
2
2176
by: VMI | last post by:
In the Page_Load() of my webForm, I have the following code, and on my HTML button, the onclick button calls writeVal() (the javascript function). When I click on the button, I see the messagebox from the Alert(), but the server-side Textbox text is not set to 'TEST'. I get a javascript error saying, "document.getElementById(...) is null or is not an object.". What could the problem be? The textbox is inside a Wizard control. Here's the...
1
2575
by: KRISHNA PRAVI | last post by:
the error is "runtime error object expected" here is the code....................................................................................... <script language="javascript" src="../Scripts/calender.js"></script> </HEAD> <body bgColor="white" MS_POSITIONING="GridLayout"> <form id="Form1" style="LEFT: 0px; POSITION: absolute; TOP: 0px" method="post" runat="server"> <!--<table width="100%" style="LEFT: 0px; POSITION:...
4
1217
by: Andy B | last post by:
How do you add javascript to a page from codebehind? I have a method I am working on that has a few conditions in it: 1. if either 1 or both WordTextBox or DefinitionTextBox is empty, show a popup window telling the user that the textbox(s) can't be empty and return them to the page with no further action. 2. The value in WordTextBox is already in the collection as a key, so show a popup telling the user that the word already exists and...
4
5357
by: Peter | last post by:
ASP.NET I have an application which use ASP.NET Autocomplete extender which works great. But I have a question how to update all the fields on the screen using Ajax. Users starts typing in a text field which causes the Autocomplete extender to display 10 like items, after the users selects an item (which is a key in the database) I want the application to go to the database retrieve a record and populate the fields.
2
37687
Frinavale
by: Frinavale | last post by:
JavaScript in ASP.NET Using JavaScript in ASP.NET pages can make your application seem to work faster and prevent unnecessary calls to the server. JavaScript can be used to perform client-side functionality in the user's browser without having to make calls back to the server; thus, saving resources. The following example demonstrates how you can use an ASP.NET button to trigger a JavaScript call which modifies an ASP.NET TextBox. ...
2
6949
by: pankajsingh5k | last post by:
Dear All, Please help me... I had read an article to lazy load a tab in a tabcontainer using an update panel on http://mattberseth.com/blog/2007/07/how_to_lazyload_tabpanels_with.html and i am implementing it in my website....
0
8683
marktang
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...
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
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,...
0
8876
tracyyun
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...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
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...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.