Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

Controls accessing outside tags

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tshad - 25 Jul 2007 18:49 GMT
I have a page where I have the Body tag set up as:

<body id="myBody" runat="server" >

This allows me to set the focus to different attributes on my page.  I have
all my Body tags set to this.

I just tried to strip out the main part of the page and make it a control
(to allow different looks and feels).  I have the following statement in the
control:

      myBody.Attributes.Add("onLoad","document.forms[0].UserName.focus()")

And I get the error:

**************************************************
Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'myBody' is not declared.

Source Error:

Line 9:    dim IsCheckResumes as Boolean = true
Line 10:   if not IsPostBack
Line 11:
myBody.Attributes.Add("onLoad","document.forms[0].UserName.focus()")
Line 12:   end if
Line 13:  End Sub
*****************************************************

How can I get this control to be able to access an outside tag?

All my pages that use this control would always have the tag set up this
way.

The aspx file is:
**************************************************************
<%@ Page Language="VB" trace="false" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ Register TagPrefix="fts" TagName="Logon" Src="test.ascx" %>
<html>
<head>
<title>Login</title>
</head>
<body id="myBody" runat="server" >
<form id="addForm" runat="server">
  <fts:Logon runat="Server"/>
</form>
</html>
**************************************************************

The ascx file is:
**************************************************************
<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="MyFunctions" %>
<%@ Import Namespace="RolesBasedAuthentication" %>
<script runat="server" >
Sub Page_Load(sender as Object, e as EventArgs)
 dim IsCheckResumes as Boolean = true
 if not IsPostBack
      myBody.Attributes.Add("onLoad","document.forms[0].UserName.focus()")
 end if
End Sub

</script>

<asp:textbox id="UserName" TextMode="SingleLine" Columns="25" runat="server"
/>
<asp:RequiredFieldValidator
ControlToValidate="UserName"
Text="User Name Required"
runat="server" />
**************************************************************

Thanks,

Tom
bruce barker - 25 Jul 2007 20:52 GMT
this feature is builtin, the form has a DefaultFocus property.

-- bruce (sqlwork.com)

> I have a page where I have the Body tag set up as:
>
[quoted text clipped - 81 lines]
>
> Tom
Göran Andersson - 25 Jul 2007 20:56 GMT
> I have a page where I have the Body tag set up as:
>
[quoted text clipped - 10 lines]
>
> And I get the error:

> Compiler Error Message: BC30451: Name 'myBody' is not declared.

> How can I get this control to be able to access an outside tag?

Make a public property in the page that exposes the body control or an
attribute in the body control.

From the controls you can cast the Page property to the actual class of
the page, so that you can access the property.

Signature

Göran Andersson
_____
http://www.guffa.com

tshad - 25 Jul 2007 22:24 GMT
>> I have a page where I have the Body tag set up as:
>>
[quoted text clipped - 20 lines]
> From the controls you can cast the Page property to the actual class of
> the page, so that you can access the property.

How would I do that in my example?

Would this apply also to aspx controls (labels or Textbox, for example)?

If I changed the .aspx page to have an aspx label outside of the Form tag
and a texbox inside the Form tag, would I be able to access these from
inside the control?:

For example, if the new aspx file is:
**************************************************************
<%@ Page Language="VB" trace="false" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ Register TagPrefix="fts" TagName="Logon" Src="test.ascx" %>
<html>
<head>
<title>Login</title>
</head>
<body id="myBody" runat="server" >

<asp:Label ID="GenericMessage" runat="server"/>

<form id="addForm" runat="server">

   <asp:TextBox ID="UserName" runat="server"/>

  <fts:Logon runat="Server"/>
</form>
</html>
**************************************************************

Thanks,

Tom
tshad - 25 Jul 2007 23:50 GMT
I almost got this to work.

I can now access the <body ID="myBody" runat="server"> tag.  When I add the
onLoad event to the myBody tag - it is there, but the control still doesn't
get focus.  I named the User Control to make sure I can get to it, but it
still doesn't work.

Here are the 2 files:

test.aspx:
**********************************************
<%@ Page Language="VB" trace="false" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ Register TagPrefix="fts" TagName="Logon" Src="test.ascx" %>
<html>
<head>
<title>Login</title>
</head>
<body id="myBody" runat="server" >
<form id="addForm" runat="server">
  <fts:Logon ID="LoginID" runat="Server"/>
</form>
</body>
</html>
***********************************************

test.ascx:
***********************************************
<script runat="server" >
Sub Page_Load(sender as Object, e as EventArgs)
 dim IsCheckResumes as Boolean = true
 if not IsPostBack
   Dim a as htmlControl
   a = CType(Page.FindControl("myBody"),htmlControl)
   a.Attributes.Add("onLoad","document.forms[0].LoginID.UserName.focus()")
 end if
End Sub

Public Property UserName AS String
 Get
  Return txtUserName.Text
 End Get
 Set
  txtUserName.Text = Value
 End Set
End Property

</script>

<asp:textbox id="txtUserName" TextMode="SingleLine" Columns="25"
runat="server" />
<asp:RequiredFieldValidator
ControlToValidate="txtUserName"
Text="User Name Required"
runat="server" />
**********************************************

I am getting an error that says:

Error: "document.forms.0.LoginID.UserName" is null or not an object

I am confused here.  Isn't LoginID part of forms[0]? And isn't UserName part
of LoginID????  Or is it how I am accessing it?

I also tried to changed the Forms ID to "addForm" in the Attributes line to:

   a.Attributes.Add("onLoad","document.addForm.LoginID.UserName.focus()")

and get the same error except the forms.0 is replaced with addform.

Here is the Tree from the Trace:

__PAGE ASP.test_aspx
   _ctl0 System.Web.UI.LiteralControl
   _ctl1 System.Web.UI.LiteralControl
   myBody System.Web.UI.HtmlControls.HtmlGenericControl
       _ctl2 System.Web.UI.LiteralControl
       addForm System.Web.UI.HtmlControls.HtmlForm
           _ctl3 System.Web.UI.LiteralControl
           LoginID ASP.test_ascx
               LoginID:txtUserName System.Web.UI.WebControls.TextBox
               LoginID:_ctl1 System.Web.UI.LiteralControl
               LoginID:_ctl0
System.Web.UI.WebControls.RequiredFieldValidator
               LoginID:_ctl2 System.Web.UI.LiteralControl
           _ctl4 System.Web.UI.LiteralControl
       _ctl5 System.Web.UI.LiteralControl
   _ctl6 System.Web.UI.LiteralControl

Thanks,

Tom

>>> I have a page where I have the Body tag set up as:
>>>
[quoted text clipped - 56 lines]
>
> Tom
tshad - 26 Jul 2007 01:02 GMT
I tried looped through the Form elements and found that the name of the
textbox is actually - LoginID:txtUserName.  I change the attribute lines to:

   Dim a as htmlControl
   a = CType(Page.FindControl("myBody"),htmlControl)
   a.Attributes.Add("onLoad","onload=document.addForm.LoginID:txtUserName.focus();")

I am not getting there error now, but I am also not getting focus.

What is missing here?

Thanks,

Tom

>I almost got this to work.
>
[quoted text clipped - 152 lines]
>>
>> Tom
tshad - 26 Jul 2007 01:53 GMT
Ok, I got it to work.  But not the way I wanted.

As well as the one below I tried:

   a.Attributes.Add("onLoad","onload=addForm.LoginID:txtUserName.focus();")

and

   a.Attributes.Add("onLoad","onload=LoginID:txtUserName.focus();")

Neither one of these worked.  There was no error but there was no focus,
either.

I made a change by calling a Javascript function from the onload function
and this worked.  The LoginID:txtUserName is the correct name but why does
the above not work???

The files that work are:

test.aspx:
*************************************************
<%@ Page Language="VB" trace="true" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ Register TagPrefix="fts" TagName="Logon" Src="test.ascx" %>
<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
 if not IsPostBack
  Dim a as htmlControl
  a = CType(Page.FindControl("addForm"),htmlControl)
  For each frmCtrl as Control in a.Controls
   trace.warn("frmCtrl = " & frmCtrl.GetType.ToString())
   trace.warn("frmCtrl.ID = " & frmCtrl.ID)
  Next
 end if
end sub
</script>
<html>
<head>
<title>Login</title>
<script language="JavaScript">
function GetFocus()
{
     if (document.getElementById("LoginID:txtUserName") != null)
  {
          document.getElementById("LoginID:txtUserName").focus();
  }
}
</script>
</head>
<body id="myBody" runat="server">
<form id="addForm" runat="server">
  <fts:Logon ID="LoginID" runat="Server"/>
</form>
</body>
</html>
*************************************************

test.ascx:
*************************************************
<script language="vb" runat="server" >
Sub Page_Load(sender as Object, e as EventArgs)
 dim IsCheckResumes as Boolean = true
 if not IsPostBack
   Dim a as htmlControl
   a = CType(Page.FindControl("myBody"),htmlControl)
   a.Attributes.Add("onLoad","onload=GetFocus();")
 end if
End Sub

Public Property UserName AS String
 Get
  Return txtUserName.Text
 End Get
 Set
  txtUserName.Text = Value
 End Set
End Property

</script>

<asp:textbox id="txtUserName" TextMode="SingleLine" Columns="25"
runat="server" />
<asp:RequiredFieldValidator
ControlToValidate="txtUserName"
Text="User Name Required"
runat="server" />
*************************************************

This works but I would rather do the focus directly from the <body> tag and
not have to build an extra function for each page that needs to have focus.

Can anyone explain to me why this isn't working?  Does it have to do with
timing on when a control is put on the page?  I would assume this wouldn't
be the case as the page is built before the onload function is called (isn't
it?).

Thanks,

Tom

>I tried looped through the Form elements and found that the name of the
>textbox is actually - LoginID:txtUserName.  I change the attribute lines
[quoted text clipped - 170 lines]
>>>
>>> Tom
Göran Andersson - 26 Jul 2007 12:20 GMT
> I tried looped through the Form elements and found that the name of the
> textbox is actually - LoginID:txtUserName.

That's odd. That name is not very usable. I would expect the name to be
LoginID$txtUserName and the id to be LoginID_txtUserName. That's how
controls are usually named.

What does the generated html code for the form look like?

> I change the attribute lines to:
>
>     Dim a as htmlControl
>     a = CType(Page.FindControl("myBody"),htmlControl)
>     a.Attributes.Add("onLoad","onload=document.addForm.LoginID:txtUserName.focus();")

An identifier in Javascript can not contain a colon. If the name really
contains a colon, you would need to use a string to reference it:

onload=document.addForm.elements('LoginID:txtUserName').focus();

> I am not getting there error now, but I am also not getting focus.

The browser doesn't display Javscript errors by default. In Internet
Explorer they show up as a notice in the status bar. In Firefox you open
the Error Console to see them.

Signature

Göran Andersson
_____
http://www.guffa.com

tshad - 26 Jul 2007 16:59 GMT
>> I tried looped through the Form elements and found that the name of the
>> textbox is actually - LoginID:txtUserName.
[quoted text clipped - 4 lines]
>
> What does the generated html code for the form look like?

Here is the line with the input tag:

<input name="LoginID:txtUserName" type="text" size="25"
id="LoginID_txtUserName" />
<span id="LoginID__ctl0" controltovalidate="LoginID_txtUserName"
evaluationfunction="RequiredFieldValidatorEvaluateIsValid" initialvalue=""
style="color:Red;visibility:hidden;">User Name Required</span>

What is interesting is that the javascript uses the LoginID:txtUserName and
the ID uses the underscore.  The trace shows the ":" which is the Name not
the ID.

__PAGE ASP.test_aspx
   _ctl0 System.Web.UI.LiteralControl
   _ctl1 System.Web.UI.ResourceBasedLiteralControl
   myBody System.Web.UI.HtmlControls.HtmlGenericControl
       _ctl2 System.Web.UI.LiteralControl
       addForm System.Web.UI.HtmlControls.HtmlForm
           _ctl3 System.Web.UI.LiteralControl
           LoginID ASP.test_ascx
               LoginID:_ctl1 System.Web.UI.LiteralControl
               LoginID:txtUserName System.Web.UI.WebControls.TextBox
               LoginID:_ctl2 System.Web.UI.LiteralControl
               LoginID:_ctl0
System.Web.UI.WebControls.RequiredFieldValidator
               LoginID:_ctl3 System.Web.UI.LiteralControl
           _ctl4 System.Web.UI.LiteralControl
       _ctl5 System.Web.UI.LiteralControl
   _ctl6 System.Web.UI.LiteralControl

The code that works uses the ":" in the function just not in the inline code
as shown here:

function GetFocus()
{
     if (document.getElementById("LoginID:txtUserName") != null)
  {
          document.getElementById("LoginID:txtUserName").focus();
  }
}

Thanks,

Tom

>> I change the attribute lines to:
>>
[quoted text clipped - 13 lines]
> Explorer they show up as a notice in the status bar. In Firefox you open
> the Error Console to see them.
Göran Andersson - 26 Jul 2007 12:08 GMT
>> Make a public property in the page that exposes the body control or an
>> attribute in the body control.
[quoted text clipped - 5 lines]
>
> Would this apply also to aspx controls (labels or Textbox, for example)?

As the references to the controls are declared as protected by default,
you can't reach them from the user control. You either have to change
the declaration to public or make a public property to expose them.

> If I changed the .aspx page to have an aspx label outside of the Form tag
> and a texbox inside the Form tag, would I be able to access these from
> inside the control?:

No.

> For example, if the new aspx file is:
> **************************************************************
[quoted text clipped - 23 lines]
>
> Tom

Signature

Göran Andersson
_____
http://www.guffa.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.