Object not there on load

T

tshad

I have an object surrounded by <div visible=false runat="server"></div>. So
it doesn't show on the page until I set visible=true.

I need to set focus to the object on post back:

if not IsPostBack then
Dim temp as String = "document.forms[0].Password.focus()"
myBody.Attributes.Add("onload",temp)
end if

This code works fine but it does give me a Javascript error symbol at the
bottom of the IE screen which says that it is null or not an object.

How can I set this up so that I don't get the error?

Thanks,

Tom
 
G

Guest

Hi there,

This is because server controls are not rendered if visible property is set
to fale. Please also note client id might be different than just 'Password'
(have a look at generated HTML source in the browser). This is because
asp.net makes it unique by including all ids of the parent server controls.
Anyway, i see you just want to set focis on a give control. ASP.NET has built
in SetFocus function that takes care of rendering javascript for you.

if not IsPostBack then
if myDiv.Visible then
SetFocus(Password)
end if
end if

or

if not IsPostBack then
if myDiv.Visible then
SetFocus(Password.ClientID)
end if
end if

Hope this helps
 
T

tshad

Milosz Skalecki said:
Hi there,

This is because server controls are not rendered if visible property is
set
to fale. Please also note client id might be different than just
'Password'
(have a look at generated HTML source in the browser). This is because
asp.net makes it unique by including all ids of the parent server
controls.

This I know.

But if I am not setting it except on Postback - why does it give me an
error. It does work correctly, it just gives me the error at the bottom of
the screen.

How would you deal with this if you were to create the object on the fly
and had to do the same thing. There also wouldn't be an object on the page
until you create it.
Anyway, i see you just want to set focis on a give control. ASP.NET has
built
in SetFocus function that takes care of rendering javascript for you.

I am using Asp.net 1.1 and I don't think that is available until 2.0 (at
least I think that is the case).

Thanks,

Tom
if not IsPostBack then
if myDiv.Visible then
SetFocus(Password)
end if
end if

or

if not IsPostBack then
if myDiv.Visible then
SetFocus(Password.ClientID)
end if
end if

Hope this helps
--
Milosz


tshad said:
I have an object surrounded by <div visible=false runat="server"></div>.
So
it doesn't show on the page until I set visible=true.

I need to set focus to the object on post back:

if not IsPostBack then
Dim temp as String = "document.forms[0].Password.focus()"
myBody.Attributes.Add("onload",temp)
end if

This code works fine but it does give me a Javascript error symbol at the
bottom of the IE screen which says that it is null or not an object.

How can I set this up so that I don't get the error?

Thanks,

Tom
 
G

Guest

Good morning,

What do you mean ‘creating dynamically’ – on the server or client side?

1. If you create it on the server side, use Page.SetFocus(control), or
Page.SetFocus(control.ClientID) after control is created
2. Client side,

<script type=â€text/javascriptâ€>
var txt = document.getElementById(‘controlId’);
// or document.getElementById(‘<%=serverControl.ClientID %>’); for server
controls
if (txt)
{
txt.focus();
}
</script>

It also might be something different that I cannot see without the code.
Could you paste it please?

--
Milosz


tshad said:
Milosz Skalecki said:
Hi there,

This is because server controls are not rendered if visible property is
set
to fale. Please also note client id might be different than just
'Password'
(have a look at generated HTML source in the browser). This is because
asp.net makes it unique by including all ids of the parent server
controls.

This I know.

But if I am not setting it except on Postback - why does it give me an
error. It does work correctly, it just gives me the error at the bottom of
the screen.

How would you deal with this if you were to create the object on the fly
and had to do the same thing. There also wouldn't be an object on the page
until you create it.
Anyway, i see you just want to set focis on a give control. ASP.NET has
built
in SetFocus function that takes care of rendering javascript for you.

I am using Asp.net 1.1 and I don't think that is available until 2.0 (at
least I think that is the case).

Thanks,

Tom
if not IsPostBack then
if myDiv.Visible then
SetFocus(Password)
end if
end if

or

if not IsPostBack then
if myDiv.Visible then
SetFocus(Password.ClientID)
end if
end if

Hope this helps
--
Milosz


tshad said:
I have an object surrounded by <div visible=false runat="server"></div>.
So
it doesn't show on the page until I set visible=true.

I need to set focus to the object on post back:

if not IsPostBack then
Dim temp as String = "document.forms[0].Password.focus()"
myBody.Attributes.Add("onload",temp)
end if

This code works fine but it does give me a Javascript error symbol at the
bottom of the IE screen which says that it is null or not an object.

How can I set this up so that I don't get the error?

Thanks,

Tom
 
T

tshad

Milosz Skalecki said:
Good morning,

What do you mean 'creating dynamically' - on the server or client side?

1. If you create it on the server side, use Page.SetFocus(control), or
Page.SetFocus(control.ClientID) after control is created
2. Client side,

<script type="text/javascript">
var txt = document.getElementById('controlId');
// or document.getElementById('<%=serverControl.ClientID %>'); for server
controls
if (txt)
{
txt.focus();
}
</script>

It also might be something different that I cannot see without the code.
Could you paste it please?

Actually, it was going through the code and that was what was causing the
problem. My mistake. I thought the error was happening as the page was
loaded when it saw an object refered to that didn't exist on the page.

Thanks,

Tom
--
Milosz


tshad said:
Milosz Skalecki said:
Hi there,

This is because server controls are not rendered if visible property is
set
to fale. Please also note client id might be different than just
'Password'
(have a look at generated HTML source in the browser). This is because
asp.net makes it unique by including all ids of the parent server
controls.

This I know.

But if I am not setting it except on Postback - why does it give me an
error. It does work correctly, it just gives me the error at the bottom
of
the screen.

How would you deal with this if you were to create the object on the fly
and had to do the same thing. There also wouldn't be an object on the
page
until you create it.
Anyway, i see you just want to set focis on a give control. ASP.NET has
built
in SetFocus function that takes care of rendering javascript for you.

I am using Asp.net 1.1 and I don't think that is available until 2.0 (at
least I think that is the case).

Thanks,

Tom
if not IsPostBack then
if myDiv.Visible then
SetFocus(Password)
end if
end if

or

if not IsPostBack then
if myDiv.Visible then
SetFocus(Password.ClientID)
end if
end if

Hope this helps
--
Milosz


:

I have an object surrounded by <div visible=false
runat="server"></div>.
So
it doesn't show on the page until I set visible=true.

I need to set focus to the object on post back:

if not IsPostBack then
Dim temp as String = "document.forms[0].Password.focus()"
myBody.Attributes.Add("onload",temp)
end if

This code works fine but it does give me a Javascript error symbol at
the
bottom of the IE screen which says that it is null or not an object.

How can I set this up so that I don't get the error?

Thanks,

Tom
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top