Accessing User Controls in Code Behind

5 posts, 0 answers
  1. Flappie
    Flappie avatar
    13 posts
    Member since:
    Nov 2009

    Posted 26 Nov 2009 Link to this post

    Hello,

    I'm using a number of User Controls, which are all defined in web.config using the tagname/prefix and source. When I add them to a page, and refer to them using the ID in the code behind, to set properties and such things, I get a JustCode error and also when I add an event listener in the code behind.

    For example,
    LatestProductList.Selected += new Controls_Products_ProductsList.SelectedEventHandler(LatestProductList_Selected); 
    gives the two errors:
    "The type 'Controls_Products_ProductsList' is defined in an assembly that is not referenced." (the first bit is underlined)
    "Unknown entity 'Controls_Products_ProductsList'. Maybe a reference to [location of the current project] is missing." (the bit just after new is underlined)

    Everything works fine and as expected, I just have 156 JustCode errors in 8 files which are all related to that, which makes it a little annoying to search for ones that might actually break something.

    Now my question is, am I doing the usercontrol thing all wrong, or is this a bug in JustCode? If the first, how should it be done?

    Thanks
  2. Tsviatko Yovtchev
    Admin
    Tsviatko Yovtchev avatar
    408 posts

    Posted 30 Nov 2009 Link to this post

    Hi MIke,

    That's definitely not a known bug.  You cannot be doing anything wrong, too since your project compiles.

    Could you send me your web.config? A small sample project with one of y our user controls would be even nicer if possible.

    Best wishes,
    Tsviatko
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. Flappie
    Flappie avatar
    13 posts
    Member since:
    Nov 2009

    Posted 30 Nov 2009 Link to this post

    Hello,

    While creating a small sample project, I noticed that the problem didn't appear to be there when I created a website directly, but my website is part of a larger solution. I don't appear to be able to upload a zip, so here's what I did:

    Create a new blank solution.
    Add a new solution folder to that solution.
    Add a new website to that folder.
    Create a "Control" folder in that website.
    Add a "TestControl.ascx" file to that folder
    Add a radcombobox to that file with any random datasource (in this case a Dictionary in the app_code folder)
    set the combobox to auto postback

    Add this to the .cs file:
    public delegate void SelectedEventHandler(int selected);  
    public event SelectedEventHandler Selected;  
      
    protected void Page_Load(object sender, EventArgs e)  
    {  
    }  
    protected void RadComboBox1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)  
    {  
        if (this.Selected != null)  
        {  
            this.Selected(Int32.Parse(RadComboBox1.SelectedValue));  
        }  


    Add the control to the web.config like so:
    <add tagPrefix="test" tagName="TestList" src="~/Controls/TestControl.ascx"/> 

    Add that control to the Default.aspx:
    <form id="form1" runat="server"
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server"
    </telerik:RadScriptManager> 
    <div> 
        <test:TestList ID="TestList1" runat="server" /> 
        <asp:Label ID="ResultLabel" runat="server"></asp:Label> 
    </div> 
    </form> 

    And put this in the Default.aspx.cs:

    protected void Page_Load(object sender, EventArgs e) 
        TestList1.Selected += new TestControl.SelectedEventHandler(TestList1_Selected); 
     
    void TestList1_Selected(int selected) 
        ResultLabel.Text = selected.ToString(); 

    This line: TestList1.Selected += new TestControl.SelectedEventHandler(TestList1_Selected);
    Gives the errors:
    C#: The type 'TestControl' is defined in an assembly that is not referenced.    Default.aspx.cs    C:\...\Test\TestWebSite\    17    3
    C#: The type 'TestControl.SelectedEventHandler' is defined in an assembly that is not referenced.    Default.aspx.cs    C:\...\Test\TestWebSite\    17    13
    C#: Unknown entity 'TestControl'. Maybe a reference to C:\Users\Michel\Projects\Test\TestWebSite\ is missing    Default.aspx.cs    C:\...\Test\TestWebSite\    17    29

    However, when I run the project, and change the combobox, the resultlabel is updated, so it does work. If you need the sample project, let me know how to send it.

    Thanks
  5. Flappie
    Flappie avatar
    13 posts
    Member since:
    Nov 2009

    Posted 30 Nov 2009 Link to this post

    Hello again,

    When I opened the project just now, JustCode told me there was an update, which I installed, and then the problem went away.
  6. Tsviatko Yovtchev
    Admin
    Tsviatko Yovtchev avatar
    408 posts

    Posted 01 Dec 2009 Link to this post

    Well, there are some changes in the latest version that might have fixed that. So, if thee problem does not reoccur we might consider it fixed.

    I really appreciate the effort on your side to help us reproduce that issue, though. So I added some well deserved Telerik Points to your account. 

    Kind regards,
    Tsviatko

    P.S. Don't hesitate to let us know if you encounter that issue again.

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top
DevCraft banner