Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I user web user control from another project? 2

Status
Not open for further replies.

passs

Programmer
Dec 29, 2003
170
0
0
RU
Hello everybody!

Can you please help me? I have one solution with two projects. One of them has some web user controls and I want to use them in another project inside this solution.
I just use drug and drop and then try to build it but get an error:
Parser Error Message: The virtual path '/TestUserControl2/Control1.ascx' maps to another application, which is not allowed.
How can I handle it?
Will be very glad for any help!

Thanks,
Alex
 
you don't need to make any drag and drops!

you need to add a reference to that project.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Hey,

Thanks for an answer.
But what exactly I must do?
I add a refeernce to another project. And this thing:
<%@ Register TagPrefix="uc1" TagName="Control01" Src="../TestUserControls/Control01.ascx" %>
should I remove it or not?

Thank you!
Alex
 
once you add the reference, you can create an instance of that control using the namespace of that project and the name of the class of the control.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Yes I tried to do that, but there is no actually such user web control in my form. Look what have I done:

using TestUserControls;//declare namespace

protected Control01 newObj = new Control01();//create an object of that class, which is needed control

this.FindControl("Form1").Controls.Add(newObj);
add this control to a form in some page in this another project.

But as a result I get nothing, like I have an empty object (Control).
How to solve it?

Thank you!
AP
 
you should add the controls as
this.Controls.Add(newObj);


also make sure that your control actually outputs something to the page.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Hm, maybe I do something wrong. My web use control has some web controls, such as textboxes, buttons etc. I paste such code you gave me, but recieve nothing. That looks like I create an object of that class but it is empty. Don't know something is wronghere:)

Thank you!
AP
 
can you post your control code?

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Thank you!
Yeap, sure, I can do it:)
here is a code which is call for that control:

protected Control01 newObj = new Control01();
private void Page_Load(object sender, System.EventArgs e)
{
//this.Controls.Add(newObj);
Response.Write(newObj.Controls.Count);
//newObj.Page.Load(sender,e);
this.Controls.Add(newObj);
// Put user code to initialize the page here
}

and a control from another project:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="Control01.ascx.cs" Inherits="TestUserControls.Control01" TargetSchema="<asp:LinkButton id="LinkButton1" runat="server">LinkButton</asp:LinkButton>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>

and code behind:
public void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
LinkButton1.Text = "hellow world!";
TextBox1.Text = "aaaaaaaa";
}

something like this:)
 
load the control in a placeholder.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
still nothing((
maybe I need to inicialize it some way?
 
dazzled,
Are you sure this can be done. I've been following this thread and have written a little stub. I too can not get it to work.

When you deploy a web site at least the solutions .dll must be in the directory and all the .aspx and .ascx must be in the directory.

For things like this I build composite controls a class inheriting Control and build the page controls using HtmlTextWriter and by overrideing Render, SaveViewState and LoadViewState.

Have never done it with a UserControl (inheriting UserControl) and ascx. And cannot see how it can be done if the .ascx is not in the directory.
I hope I am wrong and would like to see the solution.
Marty
 
i will try to make a working example.

in this case the controls are added in the source of the aspx file not in the codebehind (class). if the controls (textboxes etc.) were to be added in the class file, it should work ok.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
that would be great, I mean working example:)
Thank you!
 
passs,

Try this:

Add the User Control (<file>.ascx) as an existing object to the project you want to instantiate in. Make sure that you change the Namespace to the Namespace of your current project.

Then declare the user control:

protected UserControl <usercontrolname>;

then add it to the Page_Load as follows

<usercontrolname>.Visible = true;

This should work.

Good Luck!





John Pasko
jpasko@telemetrysoft.com
 
heym thanks, yes that works I know. Atually I was curios about the using the web user control from another project directly:)
Thank you

 
Hey, cappmgr!

Thanks:) Think that will help!
Thank you!
AP
 
cappmgr
thank you!!! That really works!!!
Many thanks!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top