Home All Groups Group Topic Archive Search About

User control accessing outside control

Author
19 May 2006 1:35 AM
tshad
I am trying to put together a user control (.ascx).

Can you access an outside control from it?

My control:

login2.ascx
****************************************************************
<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
  if not IsPostBack
     myBody.Attributes.Add("onLoad","document.forms[0].UserName.focus()")
    UserName.Text = "This is a test"
  else
  end if
End Sub

</script>
<table border="0" width="106%" >
<tr valign="baseline">
  <td width="99" align="right" valign="middle" nowrap >User Name :</td>
  <td width="500" align="left" >
   <asp:textbox id="UserName" TextMode="SingleLine" Columns="25"
runat="server" />
    <asp:RequiredFieldValidator
     ControlToValidate="UserName"
     Text="User Name Required"
     runat="server" />
  </td>
</tr>
</table>
****************************************************************

I get an error on access "mybody" as:

     Name 'myBody' is not declared.

My aspx file looks essentially like:
**************************************************************
<html>
<%@ Register TagPrefix="fts" TagName="Login" Src="login2.ascx" %>
<head>
<title>:: Staffing Workshop ::</title>
</head>

<body id="myBody" runat="server">
  <form runat="server">
  <fts:Login runat="server"/>
  </form>
</body>
</html>
**************************************************************

If I comment the line:

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

it works fine.

Is there a way to access this outside body tag from my control?

Thanks,

Tom

Author
19 May 2006 12:17 PM
Altaf Al-Amin Najwani
1) Expose mybody as property of you user control so parent form can set
reference of mybody into it.

2) Set the UserControl.mybody = this.mybody in the parent page load.Then
call usercontrol load, it will work.

Please give feedback of this post.

Show quoteHide quote
"tshad" wrote:

> I am trying to put together a user control (.ascx).
>
> Can you access an outside control from it?
>
> My control:
>
> login2.ascx
> ****************************************************************
> <script runat="server">
>  Sub Page_Load(sender as Object, e as EventArgs)
>   if not IsPostBack
>      myBody.Attributes.Add("onLoad","document.forms[0].UserName.focus()")
>     UserName.Text = "This is a test"
>   else
>   end if
>  End Sub
>
> </script>
> <table border="0" width="106%" >
>  <tr valign="baseline">
>   <td width="99" align="right" valign="middle" nowrap >User Name :</td>
>   <td width="500" align="left" >
>    <asp:textbox id="UserName" TextMode="SingleLine" Columns="25"
> runat="server" />
>     <asp:RequiredFieldValidator
>      ControlToValidate="UserName"
>      Text="User Name Required"
>      runat="server" />
>   </td>
>  </tr>
> </table>
> ****************************************************************
>
> I get an error on access "mybody" as:
>
>      Name 'myBody' is not declared.
>
> My aspx file looks essentially like:
> **************************************************************
> <html>
> <%@ Register TagPrefix="fts" TagName="Login" Src="login2.ascx" %>
> <head>
> <title>:: Staffing Workshop ::</title>
> </head>
>
> <body id="myBody" runat="server">
>   <form runat="server">
>   <fts:Login runat="server"/>
>   </form>
> </body>
> </html>
> **************************************************************
>
> If I comment the line:
>
> '     myBody.Attributes.Add("onLoad","document.forms[0].UserName.focus()")
>
> it works fine.
>
> Is there a way to access this outside body tag from my control?
>
> Thanks,
>
> Tom
>
>
>
Are all your drivers up to date? click for free checkup

Author
19 May 2006 3:19 PM
tshad
"Altaf Al-Amin Najwani" <altaf.ala***@gmail.com> wrote in message
news:33588B4F-849E-4B91-8470-DA921CDC8D7E@microsoft.com...
> 1) Expose mybody as property of you user control so parent form can set
> reference of mybody into it.

How would I do that?

The problem is that it can't reference it - how do I make it a property in
the user control?

>
> 2) Set the UserControl.mybody = this.mybody in the parent page load.Then
> call usercontrol load, it will work.

That would work.

I just wanted to find out how to access properties of the parent page from a
user control.   This would obviously have to be looked at closely, but I am
trying to use my user controls on different parent pages that would all have
the same objects, but would be laid out differently.

Thanks,

Tom
Show quoteHide quote
>
> Please give feedback of this post.
>
> "tshad" wrote:
>
>> I am trying to put together a user control (.ascx).
>>
>> Can you access an outside control from it?
>>
>> My control:
>>
>> login2.ascx
>> ****************************************************************
>> <script runat="server">
>>  Sub Page_Load(sender as Object, e as EventArgs)
>>   if not IsPostBack
>>      myBody.Attributes.Add("onLoad","document.forms[0].UserName.focus()")
>>     UserName.Text = "This is a test"
>>   else
>>   end if
>>  End Sub
>>
>> </script>
>> <table border="0" width="106%" >
>>  <tr valign="baseline">
>>   <td width="99" align="right" valign="middle" nowrap >User Name :</td>
>>   <td width="500" align="left" >
>>    <asp:textbox id="UserName" TextMode="SingleLine" Columns="25"
>> runat="server" />
>>     <asp:RequiredFieldValidator
>>      ControlToValidate="UserName"
>>      Text="User Name Required"
>>      runat="server" />
>>   </td>
>>  </tr>
>> </table>
>> ****************************************************************
>>
>> I get an error on access "mybody" as:
>>
>>      Name 'myBody' is not declared.
>>
>> My aspx file looks essentially like:
>> **************************************************************
>> <html>
>> <%@ Register TagPrefix="fts" TagName="Login" Src="login2.ascx" %>
>> <head>
>> <title>:: Staffing Workshop ::</title>
>> </head>
>>
>> <body id="myBody" runat="server">
>>   <form runat="server">
>>   <fts:Login runat="server"/>
>>   </form>
>> </body>
>> </html>
>> **************************************************************
>>
>> If I comment the line:
>>
>> '
>> myBody.Attributes.Add("onLoad","document.forms[0].UserName.focus()")
>>
>> it works fine.
>>
>> Is there a way to access this outside body tag from my control?
>>
>> Thanks,
>>
>> Tom
>>
>>
>>
Author
19 May 2006 3:34 PM
tshad
Also, in this case I want to place the focus on my first textbox on initial
load of the logon page.

Normally, in my Page_Load of the page, I set the myBody attribute to set
this.  I could still do this from the login page, but I would like to do
this from the control as I want to dynamically load this control.

Thanks,

Tom
Show quoteHide quote
"tshad" <tscheider***@ftsolutions.com> wrote in message
news:enCwge1eGHA.2076@TK2MSFTNGP04.phx.gbl...
> "Altaf Al-Amin Najwani" <altaf.ala***@gmail.com> wrote in message
> news:33588B4F-849E-4B91-8470-DA921CDC8D7E@microsoft.com...
>> 1) Expose mybody as property of you user control so parent form can set
>> reference of mybody into it.
>
> How would I do that?
>
> The problem is that it can't reference it - how do I make it a property in
> the user control?
>
>>
>> 2) Set the UserControl.mybody = this.mybody in the parent page load.Then
>> call usercontrol load, it will work.
>
> That would work.
>
> I just wanted to find out how to access properties of the parent page from
> a user control.   This would obviously have to be looked at closely, but I
> am trying to use my user controls on different parent pages that would all
> have the same objects, but would be laid out differently.
>
> Thanks,
>
> Tom
>>
>> Please give feedback of this post.
>>
>> "tshad" wrote:
>>
>>> I am trying to put together a user control (.ascx).
>>>
>>> Can you access an outside control from it?
>>>
>>> My control:
>>>
>>> login2.ascx
>>> ****************************************************************
>>> <script runat="server">
>>>  Sub Page_Load(sender as Object, e as EventArgs)
>>>   if not IsPostBack
>>>
>>> myBody.Attributes.Add("onLoad","document.forms[0].UserName.focus()")
>>>     UserName.Text = "This is a test"
>>>   else
>>>   end if
>>>  End Sub
>>>
>>> </script>
>>> <table border="0" width="106%" >
>>>  <tr valign="baseline">
>>>   <td width="99" align="right" valign="middle" nowrap >User Name :</td>
>>>   <td width="500" align="left" >
>>>    <asp:textbox id="UserName" TextMode="SingleLine" Columns="25"
>>> runat="server" />
>>>     <asp:RequiredFieldValidator
>>>      ControlToValidate="UserName"
>>>      Text="User Name Required"
>>>      runat="server" />
>>>   </td>
>>>  </tr>
>>> </table>
>>> ****************************************************************
>>>
>>> I get an error on access "mybody" as:
>>>
>>>      Name 'myBody' is not declared.
>>>
>>> My aspx file looks essentially like:
>>> **************************************************************
>>> <html>
>>> <%@ Register TagPrefix="fts" TagName="Login" Src="login2.ascx" %>
>>> <head>
>>> <title>:: Staffing Workshop ::</title>
>>> </head>
>>>
>>> <body id="myBody" runat="server">
>>>   <form runat="server">
>>>   <fts:Login runat="server"/>
>>>   </form>
>>> </body>
>>> </html>
>>> **************************************************************
>>>
>>> If I comment the line:
>>>
>>> ' myBody.Attributes.Add("onLoad","document.forms[0].UserName.focus()")
>>>
>>> it works fine.
>>>
>>> Is there a way to access this outside body tag from my control?
>>>
>>> Thanks,
>>>
>>> Tom
>>>
>>>
>>>
>
>

Bookmark and Share