Results 1 to 15 of 15

Thread: Dynamic web User Controls

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    77

    Dynamic web User Controls

    Hi all.

    this is probably so simple but I've tried and failed!

    I want to put my controls is a folder to keep the project tidy.
    (seemed like a good idea).

    so I ended up with this code:
    Code:
                    
    Dim wucDayNum As Control = LoadControl("~/Controls/wucOnightOpsDayNum.ascx")
                    wucDayNum.ID = "wucDayNum" + "1"
                    pnlDayNum.Controls.Add(wucDayNum)
    then I tried something like:

    Code:
                    Dim wucdn As wucOnightOpsDayNum = pnlDayNum.FindControl("wucDayNum1")
    
                    Dim txtMyName As TextBox = DirectCast(pnlDayNum.FindControl("wucDayNum1"), wucOnightOpsDayNum)
    both lines tell me that wucOnightOpsDayNum is not defined.

    what is the syntax to find my control in the /controls folder?

    thanks is advance.

  2. #2
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Dynamic web User Controls

    Remember with dynamic control you have to store them yourself and reload them whenever the page loads. If you just add them to a panel and then do a post back, they will be gone.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  3. #3
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Dynamic web User Controls

    Also, in your aspx file, you need to have a line like:

    Code:
    <&#37;@ Register src="~/Controls/wucOnightOpsDayNum.ascx" TagName="wucOnightOpsDayNum" TagPrefix="wucOnightOpsDayNum" %>
    under you page directive.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    77

    Re: Dynamic web User Controls

    thanks for your help but that isn't the problem.

    I've done some more investigation and here is a more complete view of the problem:

    1. within visual studio I create a folder called "Controls"

    2. I add a web user control to the folder called WebUserControl1.ascx

    3. i drag and drop the control onto the default page and then delete it off of the page which leaves this:
    <%@ Register Src="Controls/WebUserControl1.ascx" agName="WebUserControl1" TagPrefix="uc1" %>

    4. in the default page i put:
    Code:
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim wuc As Control
            wuc = LoadControl("~/Controls/WebUserControl1.ascx")
            wuc.ID = "wuc"
            Panel1.Controls.Add(wuc)
    
            wuc = LoadControl("~/Controls/WebUserControl1.ascx")
            wuc.ID = "wuc2"
            Panel1.Controls.Add(wuc)
    
    
        End Sub
    which works fine.

    5. i then put in the default page:
    Code:
       Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim wucb As WebUserControl1 = Panel1.FindControl("wuc")
            wucb.txt = "zzz"
        End Sub
    which is where all the trouble starts.

    I have tried this on multiple machines each running visual studio 2005 and ...

    it works fine if i start with a web application.
    BUT if I start with a website i get the error:
    Error 1 Type 'WebUserControl1' is not defined. C:\TRASH\WebSite4\Default.aspx.vb 18 21 C:\TRASH\WebSite4\

    am I going mad, or is Microsoft driving me mad?

  5. #5
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Dynamic web User Controls

    Did you hand type this?

    <%@ Register Src="Controls/WebUserControl1.ascx" agName="WebUserControl1" TagPrefix="uc1" %>

    Should be:

    <%@ Register Src="~/Controls/WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  6. #6
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Dynamic web User Controls

    Maybe try dropping a copy of the user control in the same folder as you aspx page to see if it helps?
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    77

    Re: Dynamic web User Controls

    sorry Sean.

    I've already tried both.

    <%@ Register Src="Controls/WebUserControl1.ascx" agName="WebUserControl1" TagPrefix="uc1" %>

    and

    <%@ Register Src="~/Controls/WebUserControl1.ascx" agName="WebUserControl1" TagPrefix="uc1" %>

    the first is what VS creates if you manually drop the control on the page.

    I have also tried putting the control at the top level with the page instead of in the /Controls folder.

    this is gooing to be one of those funny ones. it all works fine if i use a web app but not if i use a website.

    I dont think the company are going to want me to convert the entire site from a website to a web app!

    I'm going to try VS2010 this evening.

    any other suggestions would be welcome.
    (banging my head on a wall will be nice when it stops!)

  8. #8
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Dynamic web User Controls

    Sorry I'm out of ideas. That seems to be all I do to user Dynamic User Controls. Hopefully Gep will be on today and can give you some other suggestions.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Dynamic web User Controls

    Hello,

    When you created the folder, and added the UserControl to the folder, was there any changes to the Namespace in which the control reside? Visual Studio normally handles all of this for you, but if you are not expecting it, it can cause problems like you are seeing here.

    I suspect that you need to add an Imports statement to the top of the page in order to allow ASP.Net to know which User Control you are referring to.

    If you are not sure what I am talking about, feel free to ask, and it might help if you can upload a small project that is giving you the problem so that we can take a look.

    Gary

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    77

    Re: Dynamic web User Controls

    hi, thanks for the reply.

    I tried adding:

    Imports Controls_WebUserControl1

    but that didn't help.

    I have the same problem on vs2005 and 2008.

    The thing that really gets me is that it works if I set it up as a web app instead of a website.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    77

    Re: Dynamic web User Controls

    here is the complete code in case that shows something that I can't see.

    default.aspx.vb
    Code:
    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim wuc As Control
            wuc = LoadControl("~/Controls/WebUserControl1.ascx")
            wuc.ID = "wuc"
            Panel1.Controls.Add(wuc)
    
            wuc = LoadControl("~/Controls/WebUserControl1.ascx")
            wuc.ID = "wuc2"
            Panel1.Controls.Add(wuc)
    
        End Sub
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim wucb As WebUserControl1 = Panel1.FindControl("wuc")
    
    
        End Sub
    End Class
    Default.aspx
    Code:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <%@ Register Src="Controls/WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <br />
            <br />
            <asp:Panel ID="Panel1" runat="server" Height="168px" Width="368px">
            </asp:Panel>
            &nbsp;</div>
            <br />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" /><br />
        </form>
    </body>
    </html>

    /Controls/WebUserControl1.ascx.vb
    Code:
    Partial Class Controls_WebUserControl1
        Inherits System.Web.UI.UserControl
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.TextBox1.Text = "wuc button clicked"
    
        End Sub
    
        Public Property txt() As String
            Get
                Return TextBox1.Text
            End Get
            Set(ByVal value As String)
                TextBox1.Text = value
            End Set
        End Property
    End Class
    /Controls/WebUserControl1.ascx
    Code:
    <%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl1.ascx.vb" Inherits="Controls_WebUserControl1" %>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" />

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    77

    Re: Dynamic web User Controls

    Hi everyone.

    I have found a "dirty" workaround:

    Code:
      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Dim wucb As WebUserControl1 = Panel1.FindControl("wuc")
            Dim tb1 As TextBox = Panel1.FindControl("wuc").FindControl("TextBox1")
            Me.TextBox1.Text = tb1.Text
            'Me.TextBox1.Text = "zzz"
    
        End Sub
    I can access the controls of the WebUserControl without declaring WebUserControl1.

    Before I set out on this route, can somebody try and recreate my problem.
    I just want to make sure that I am not going mad and that the better option is definately not going to work.

  13. #13
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Dynamic web User Controls

    Hello,

    Is it possible that you can upload a complete application that shows the problem that you are having?

    Gary

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    77

    Re: Dynamic web User Controls

    Hi Gary.

    thanks for looking at this.


    WEBSITE_DOESNT_WORK.zip

    WEBAPP_WORKS.zip

    I have attched the website that i can't get to do what I want,
    and also the web app that works just fine.

    I keep gettnig the feeling that I must be missing something really simple,
    I can't be the first person to try this!

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    77

    Re: Dynamic web User Controls

    Hi all.

    found it! yes i was missing something really simple.
    Not sure if I had a senior citizen moment or i've just been microsofted.

    although when you create a WebUserControl in a web app and a website
    they have the same name, they don't have the same class name.

    so for web app:
    class = Public Partial Class WebUserControl
    Inherits System.Web.UI.Page
    so you use: Dim wucdn As WebUserControl = Panel1.FindControl("wuc")

    for website:
    class= Partial Class Controls_WebUserControl
    Inherits System.Web.UI.UserControl
    so you useim wucdn As Controls_WebUserControl = Panel1.FindControl("wuc")


    I'm a happy bunny

    but can anyone explain why the web user control inherits from different places (which i assume is why microsoft gave the controls different class names)?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width