User control accessing outside control

Discussion in 'ASP .Net' started by tshad, May 19, 2006.

  1. tshad

    tshad Guest

    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
    tshad, May 19, 2006
    #1
    1. Advertising

  2. 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.

    "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
    >
    >
    >
    =?Utf-8?B?QWx0YWYgQWwtQW1pbiBOYWp3YW5p?=, May 19, 2006
    #2
    1. Advertising

  3. tshad

    tshad Guest

    "Altaf Al-Amin Najwani" <> wrote in message
    news:...
    > 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
    >>
    >>
    >>
    tshad, May 19, 2006
    #3
  4. tshad

    tshad Guest

    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
    "tshad" <> wrote in message
    news:...
    > "Altaf Al-Amin Najwani" <> wrote in message
    > news:...
    >> 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
    >>>
    >>>
    >>>

    >
    >
    tshad, May 19, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig G
    Replies:
    3
    Views:
    715
    Craig G
    Nov 30, 2004
  2. Mad Scientist Jr
    Replies:
    0
    Views:
    624
    Mad Scientist Jr
    Mar 22, 2006
  3. tshad
    Replies:
    4
    Views:
    395
    tshad
    Aug 4, 2006
  4. Kiran More
    Replies:
    2
    Views:
    305
    Kiran More
    Nov 14, 2006
  5. Krzysztof Poc

    outside type, outside function

    Krzysztof Poc, Feb 3, 2012, in forum: C++
    Replies:
    1
    Views:
    272
    Victor Bazarov
    Feb 7, 2012
Loading...

Share This Page