accessing User Controls - URGENT please help!

Discussion in 'ASP .Net' started by Guest, Nov 1, 2004.

  1. Guest

    Guest Guest

    Hi all,

    I've got a page with a user control on, added via VS. I'm trying to get to a
    property of the user control (or more precisely, a public var).

    Here's the code at the top of my aspx page...

    <%@ Register TagPrefix="dI2" TagName="dartsUC_rolodexsearch"
    Src="dartsUC_rolodexsearch.ascx" %>

    then in the html...

    <dI2:dartsUC_rolodexsearch id="dartsUC_rolodexsearch1"
    runat="server"></dI2:dartsUC_rolodexsearch>

    and the top of the ascx file looks like...

    <%@ Control Language="c#" AutoEventWireup="false"
    Codebehind="dartsUC_rolodexsearch.ascx.cs"
    Inherits="dartsIntranet.WUC_rolodexsearch"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>

    My question is how do I get to the user control. If I try

    protected dartsUC_rolodexsearch dartsUC_rolodexsearch1;

    in my page cs file, I get a cannot find type or namespace
    "dartsUC_rolodexsearch".

    Please, please help. I've been stuck on this for 2wks now - can't find
    anything that makes sense!

    Cheers


    Dan
     
    Guest, Nov 1, 2004
    #1
    1. Advertisements

  2. Guest

    Guest Guest

    Hi,

    It is mentioned as in ascx file, the Inherits property is,

    Inherits="dartsIntranet.WUC_rolodexsearch"

    It means that, ur namespace is "dartsIntranet" and your class name is
    "WUC_rolodexsearch".

    So, if u like to use ur usercontrol in one of the aspx pages, create an
    instance of ur usercontrol class and not the one that is given in the Tag
    Prefix.

    so the actual code be,

    protected WUC_rolodexsearch dartsUC_rolodexsearch1;

    Regards,
    Kamal T.
     
    Guest, Nov 1, 2004
    #2
    1. Advertisements

  3. Guest

    Guest Guest

    Kamal,

    Thanks, that'sworked. Sort of. I still cant get access to my variable though.

    I added the line you suggested. I also added a label to my page.

    Now, the final line in my control is rolodexSQL = "SELECT * FROM ContactsDB";

    so, in my aspx page, I'm doing

    lblSql.Text = dartsUC_rolodexsearch1.rolodexSQL;

    The rolodexSQL part popped up in the drop down list, so that was nice.
    However, when I run the program, I get nothing in my label control. Almost
    like rolodexSQL is returning "" or null. If I add .ToString() to the line, i
    get a "object reference not set to an instance of an object" error.

    Any ideas much appreciated :eek:)

    Cheers


    Dan
     
    Guest, Nov 1, 2004
    #3
  4. Guest

    Guest Guest

    Hi,

    Write a public property in ur control to expose the value of "rolodexSQL",
    and use the public propertty to get the value in ur aspx page.

    For eg:

    write a peroperty,

    public string GetRolodexSQLValue
    {
    get
    {
    return rolodexSQL;
    }
    }

    In ur aspx page,

    lblSql.Text = dartsUC_rolodexsearch1.GetRolodexSQLValue;

    Hope this shld. help u out....

    Regards,
    Kamal T.
     
    Guest, Nov 2, 2004
    #4
  5. Guest

    Guest Guest

    Kamal,

    Thanks, that looks better, however it's still not working. I wrote the
    property as so...

    private string rolodexSQL;
    public string GetRolodexSQLValue
    {
    get
    {
    return rolodexSQL != null ? rolodexSQL : "NA";
    }
    }

    and then in Page_Load of the control..

    rolodexSQL = "test";

    then, as the last line in my aspx page_load...

    PageTitle.InnerHTML = dartsUC_rolodexsearch1.GetRolodexSQLValue;

    where PageTitle is the id of the <title> element with a protected var
    attached.

    Compiles and runs fine, but I always get NA in the title bar. It's like my
    statement

    rolodexSQL = "test"; statement is being ignored completely.

    :(

    Thanks for your help


    Dan
     
    Guest, Nov 2, 2004
    #5
  6. Guest

    Guest Guest

    Hi peeps

    Further to the earlier post, I've done some more investigating.

    It seems that, although I cannot get to the variable from the user control,
    I can infact get to it in the aspx page with the following statement...

    <%= dartsUC_rolodexsearch1.GetRolodexSQLValue %>

    Any ideas why this works and yet I can't get to the UC variable in
    code-behind, which is where I actually need it.

    Cheers


    Dan
     
    Guest, Nov 2, 2004
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.