loading ascx control dynamically in placeholder

Discussion in 'ASP .Net' started by Bert, Dec 11, 2006.

  1. Bert

    Bert Guest

    Hi

    I am trying to load my user control in a placeholder,

    i get an error: c:\inetpub\wwwroot\testinline\WebForm2.aspx.vb(26): Type
    'WebUserControl6' is not defined?

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    'Put user code to initialize the page here

    Dim uc As WebUserControl6 =
    DirectCast(Me.LoadControl("WebUserControl6.ascx"), WebUserControl6)

    PlaceHolder1.Controls.Add(uc)

    End Sub

    ascx control:



    <%@ Reference Control = "WebUserControl6.ascx" %>
    hello



    thanks



    B.
     
    Bert, Dec 11, 2006
    #1
    1. Advertising

  2. Bert

    Bert Guest

    thanks

    what if the ascx control has no code behind as in my case? So the compiler
    does not recognize type webusercontrol6? How do I do that?

    "Manish Bafna" <> wrote in message
    news:...
    > Hi,
    > Is the code which you have written for UserControl in the same project or
    > some other project?
    > If it is in different project then i think you need to add reference to it
    > i would suggest you to include the code which you writen for usercontrol
    > in
    > same project and see if it is working or not
    > also in following line of code
    > Dim uc As WebUserControl6 =
    >> DirectCast(Me.LoadControl("WebUserControl6.ascx"), WebUserControl6)

    >
    > WebUserControl6 should be the name of UserControl Class.
    >
    >
    >
    > Thanks and Regards,
    > manish bafna
    >
    > "Bert" wrote:
    >
    >> Hi
    >>
    >> I am trying to load my user control in a placeholder,
    >>
    >> i get an error: c:\inetpub\wwwroot\testinline\WebForm2.aspx.vb(26): Type
    >> 'WebUserControl6' is not defined?
    >>
    >> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    >> System.EventArgs) Handles MyBase.Load
    >>
    >> 'Put user code to initialize the page here
    >>
    >> Dim uc As WebUserControl6 =
    >> DirectCast(Me.LoadControl("WebUserControl6.ascx"), WebUserControl6)
    >>
    >> PlaceHolder1.Controls.Add(uc)
    >>
    >> End Sub
    >>
    >> ascx control:
    >>
    >>
    >>
    >> <%@ Reference Control = "WebUserControl6.ascx" %>
    >> hello
    >>
    >>
    >>
    >> thanks
    >>
    >>
    >>
    >> B.
    >>
    >>
    >>
    >>
    >>
     
    Bert, Dec 11, 2006
    #2
    1. Advertising

  3. Hi,
    I was wondering what purpose the UserControl will serve if it does not have
    code-behind.You will be not able to set properties/raise events etc.Can you
    describe in detail what your usercontrol is trying to do.

    Thanks and Regards,
    manish bafna

    "Bert" wrote:

    > thanks
    >
    > what if the ascx control has no code behind as in my case? So the compiler
    > does not recognize type webusercontrol6? How do I do that?
    >
    > "Manish Bafna" <> wrote in message
    > news:...
    > > Hi,
    > > Is the code which you have written for UserControl in the same project or
    > > some other project?
    > > If it is in different project then i think you need to add reference to it
    > > i would suggest you to include the code which you writen for usercontrol
    > > in
    > > same project and see if it is working or not
    > > also in following line of code
    > > Dim uc As WebUserControl6 =
    > >> DirectCast(Me.LoadControl("WebUserControl6.ascx"), WebUserControl6)

    > >
    > > WebUserControl6 should be the name of UserControl Class.
    > >
    > >
    > >
    > > Thanks and Regards,
    > > manish bafna
    > >
    > > "Bert" wrote:
    > >
    > >> Hi
    > >>
    > >> I am trying to load my user control in a placeholder,
    > >>
    > >> i get an error: c:\inetpub\wwwroot\testinline\WebForm2.aspx.vb(26): Type
    > >> 'WebUserControl6' is not defined?
    > >>
    > >> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > >> System.EventArgs) Handles MyBase.Load
    > >>
    > >> 'Put user code to initialize the page here
    > >>
    > >> Dim uc As WebUserControl6 =
    > >> DirectCast(Me.LoadControl("WebUserControl6.ascx"), WebUserControl6)
    > >>
    > >> PlaceHolder1.Controls.Add(uc)
    > >>
    > >> End Sub
    > >>
    > >> ascx control:
    > >>
    > >>
    > >>
    > >> <%@ Reference Control = "WebUserControl6.ascx" %>
    > >> hello
    > >>
    > >>
    > >>
    > >> thanks
    > >>
    > >>
    > >>
    > >> B.
    > >>
    > >>
    > >>
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?TWFuaXNoIEJhZm5h?=, Dec 12, 2006
    #3
  4. re:
    > I was wondering what purpose the UserControl will serve if it does not have
    > code-behind.You will be not able to set properties/raise events etc.


    Why not ?

    I'd find it really odd if this code didn't work, yet it doesn't use code-behind.

    DisplayNumber.ascx :
    -----------------------------

    <%@ Control Language="VB" ClassName="Spinner" %>
    <script runat="server">
    Private m_minValue As Integer = 0
    Private m_maxValue As Integer = 100
    Private m_currentNumber As Integer = 0
    Public Property MinValue() As Integer
    Get
    Return m_minValue
    End Get
    Set(ByVal value As Integer)
    If value >= Me.MaxValue Then
    Throw New Exception _
    ("MinValue must be less than MaxValue.")
    Else
    m_minValue = value
    End If
    End Set
    End Property

    Public Property MaxValue() As Integer
    Get
    Return m_maxValue
    End Get
    Set(ByVal value As Integer)
    If value <= Me.MinValue Then
    Throw New Exception _
    ("MaxValue must be greater than MinValue.")
    Else
    m_maxValue = value
    End If
    End Set
    End Property

    Public ReadOnly Property CurrentNumber() As Integer
    Get
    Return m_currentNumber
    End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    If IsPostBack Then
    m_currentNumber = CInt(ViewState("currentNumber"))
    Else
    m_currentNumber = Me.MinValue
    End If
    DisplayNumber()
    End Sub

    Protected Sub DisplayNumber()
    textNumber.Text = Me.CurrentNumber.ToString()
    ViewState("currentNumber") = Me.CurrentNumber.ToString()
    End Sub

    Protected Sub buttonUp_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    If m_currentNumber = Me.MaxValue Then
    m_currentNumber = Me.MinValue
    Else
    m_currentNumber += 1
    End If
    DisplayNumber()
    End Sub

    Protected Sub buttonDown_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    If m_currentNumber = Me.MinValue Then
    m_currentNumber = Me.MaxValue
    Else
    m_currentNumber -= 1
    End If
    DisplayNumber()
    End Sub
    </script>
    <asp:TextBox ID="textNumber" runat="server"
    ReadOnly="True" Width="32px" Enabled="False" />
    <asp:Button Font-Bold="True" ID="buttonUp" runat="server"
    Text="^" OnClick="buttonUp_Click" />
    <asp:Button Font-Bold="True" ID="buttonDown" runat="server"
    Text="v" OnClick="buttonDown_Click" />
    --------------------------------------------------------------------

    Bottom line : you are mistaken.
    You can do inline amything that you can do in code-behind.




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en espaƱol : http://asp.net.do/foros/
    ===================================
    "Manish Bafna" <> wrote in message
    news:...
    > Hi,
    > I was wondering what purpose the UserControl will serve if it does not have
    > code-behind.You will be not able to set properties/raise events etc.Can you
    > describe in detail what your usercontrol is trying to do.
    >
    > Thanks and Regards,
    > manish bafna
    >
    > "Bert" wrote:
    >
    >> thanks
    >>
    >> what if the ascx control has no code behind as in my case? So the compiler
    >> does not recognize type webusercontrol6? How do I do that?
    >>
    >> "Manish Bafna" <> wrote in message
    >> news:...
    >> > Hi,
    >> > Is the code which you have written for UserControl in the same project or
    >> > some other project?
    >> > If it is in different project then i think you need to add reference to it
    >> > i would suggest you to include the code which you writen for usercontrol
    >> > in
    >> > same project and see if it is working or not
    >> > also in following line of code
    >> > Dim uc As WebUserControl6 =
    >> >> DirectCast(Me.LoadControl("WebUserControl6.ascx"), WebUserControl6)
    >> >
    >> > WebUserControl6 should be the name of UserControl Class.
    >> >
    >> >
    >> >
    >> > Thanks and Regards,
    >> > manish bafna
    >> >
    >> > "Bert" wrote:
    >> >
    >> >> Hi
    >> >>
    >> >> I am trying to load my user control in a placeholder,
    >> >>
    >> >> i get an error: c:\inetpub\wwwroot\testinline\WebForm2.aspx.vb(26): Type
    >> >> 'WebUserControl6' is not defined?
    >> >>
    >> >> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    >> >> System.EventArgs) Handles MyBase.Load
    >> >>
    >> >> 'Put user code to initialize the page here
    >> >>
    >> >> Dim uc As WebUserControl6 =
    >> >> DirectCast(Me.LoadControl("WebUserControl6.ascx"), WebUserControl6)
    >> >>
    >> >> PlaceHolder1.Controls.Add(uc)
    >> >>
    >> >> End Sub
    >> >>
    >> >> ascx control:
    >> >>
    >> >>
    >> >>
    >> >> <%@ Reference Control = "WebUserControl6.ascx" %>
    >> >> hello
    >> >>
    >> >>
    >> >>
    >> >> thanks
    >> >>
    >> >>
    >> >>
    >> >> B.
    >> >>
    >> >>
    >> >>
    >> >>
    >> >>

    >>
    >>
    >>
     
    Juan T. Llibre, Dec 12, 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. nail

    Build control in a placeholder or like a placeholder

    nail, Sep 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    345
  2. Phoenix
    Replies:
    2
    Views:
    241
    Teemu Keiski
    Jun 6, 2005
  3. Ugur Ekinci

    Dynamically Loading User Control in PlaceHolder

    Ugur Ekinci, Nov 22, 2005, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    223
    Gokhan Altinoren
    Nov 27, 2005
  4. Object Delayed loading dynamically into Placeholder

    , Oct 5, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    130
  5. david mccormack
    Replies:
    1
    Views:
    249
    Ishigava Butahhi
    Aug 28, 2003
Loading...

Share This Page