Hidden objects given error

T

tshad

I have some textboxes that are being hidden using <div runat="server"> which
mean they are not on the page

But I am also trying to set focus to a Textbox when it is on the screen.
When the Textbox is not on the screen I am getting a JavaScript error:

document.forms.0.Email is null or not an object.

I tried to fix this by doing a test to see if it was not nothing. If it was
nothing, I would set the <body> tag to "". I tried this in the Page_Load as
well as Page_PreRender but this doesn't work either - like so:

Sub Page_PreRender(sender as Object, e as EventArgs)
if not Email is nothing then
myBody.Attributes.Add("onLoad","document.forms[0].Email.focus()")
else
myBody.Attributes.Add("onLoad","")
end if
End Sub

Is there a way to make this work?

Thanks,

Tom
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

tshad said:
I have some textboxes that are being hidden using <div runat="server"> which
mean they are not on the page

But I am also trying to set focus to a Textbox when it is on the screen.
When the Textbox is not on the screen I am getting a JavaScript error:

document.forms.0.Email is null or not an object.

I tried to fix this by doing a test to see if it was not nothing. If it was
nothing, I would set the <body> tag to "". I tried this in the Page_Load as
well as Page_PreRender but this doesn't work either - like so:

Sub Page_PreRender(sender as Object, e as EventArgs)
if not Email is nothing then
myBody.Attributes.Add("onLoad","document.forms[0].Email.focus()")
else
myBody.Attributes.Add("onLoad","")
end if
End Sub

Is there a way to make this work?

Thanks,

Tom

As you are checking if the server control exists, the check will never
come out false, as the server control always exists even if it's not
rendered to the final page.

Check instead the visibility of the control that is used to hide the
textbox.
 
G

Guest

In addtion to Goran's comments, you don't need to write your own set focus
code. there ASP.NET 2.0 introduced SetFocus function:

-- aspx code --
<div runat="server" id="container">
<asp:TextBox runat="server" ID="txtEmail"/>
</div>
-- end aspx code--

-- code behind --
Sub Page_PreRender(sender as Object, e as EventArgs)
if container.Visible then
SetFocus(txtEmail)
' or another way
SetFocus(txtEmail.ClientID)
end if
End Sub
-- end code behind --
Please also note you cannot use control ID in javascript, you have to use
ClientID in conjunction with document.getElementById() javascript function:

-- aspx code --
<script type="text/javascript">
var texbox = document.getElementById('<%=txtEmail.ClientID%>');
</script>
-- end aspx code --




-- end code behind --
 
M

Mark Fitzpatrick

Instead, check to see if a control is visible or enabled. If it's not
visible it's still there, just has the visibility to false. Usually though I
run this on the container object that I used to hide it to begin with such
as a div element, but normally a panel object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top