Solved

Request for sample code - C# and ASP.NET

Posted on 2016-08-23
14
63 Views
Last Modified: 2016-08-26
I am requesting someone write a sample project for me that has the following parameters:

2 projects, one named Parent, one named Child.

Parent:
-webforms application
-only one page named Default.aspx
-will reference Child.DLL
-Has no knowledge of Child project location...all it has is the compiled DLL.


Child:
-webforms application
-has only one item, Child.ascx  (web user control)
-has one property, a string called FirstName
-at runtime will generate text inside of a <div> that says, "Hello <First Name>"
-compiles as Child.DLL
-Has no knowledge of Parent.DLL

I need detailed step-by-step instructions of how to deploy Child.DLL and how Parent consumes Child.DLL in order to make use of the web user control.

The reason for my request is that I have been having nothing but trouble getting the above to work.  It seems simple to me, but for some reason I just keep running into problems.  So I am thinking maybe if someone can generate a very simple web page that uses the web control, maybe I will realize what I am doing wrong.  I am sure it is something simple.

Right now I after Referencing the Child.DLL I am not sure what to do our how to expose the web user control inside the DLL.  I tried adding the Child.DLL to the Toolbox via "Choose Items..." but it said it could not load the Type Library.

Parent codebehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Parent
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

Open in new window



Parent markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Parent.Default" %>

<%@ Register Src="~/Child.dll" Namespace="Child" Assembly="Child" TagName="SayHello" TagPrefix="sh"%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>    
    <div>
    Parent web form

        <sh:SayHello ></sh:SayHello>
    </div>    
   
</body>
</html>

Open in new window






Child codebehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Child
{
    public partial class Child : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string FirstName
        {
            get
            {
                return firstnamediv.InnerHtml;
            }

            set
            {
                firstnamediv.InnerHtml = "Hello " + value;
            }
        }
    }
}

Open in new window


Child markup:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Child.ascx.cs" Inherits="Child.Child" %>
<div id="firstnamediv" runat="server"></div>

Open in new window

0
Comment
Question by:knowlton
  • 10
  • 3
14 Comments
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 250 total points
ID: 41767311
>>I tried adding the Child.DLL to the Toolbox via "Choose Items..."

You need to add a reference not as an item in the Toolbox (which is only for controls and components).

Your best way to do it would be to create a single solution containing the 2 projects.

For the deployment, just copy the child.dll at the same place as the parent.
0
 
LVL 5

Author Comment

by:knowlton
ID: 41767372
Let's just pretend, for the moment, that all I have is the Child.DLL.

How do I make use of ChildDLL inside of the Parent solution?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41767382
from the Project menu, select Add References...
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 5

Author Comment

by:knowlton
ID: 41767394
Done.
0
 
LVL 5

Author Comment

by:knowlton
ID: 41767403
This is what I get now:

cannot find ascx

Here is the parent markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Parent.Default" %>

<%@ Register TagPrefix="uc" Namespace="Child" Assembly="Child" %>

<%--<%@ Register TagPrefix="uc" Namespace="myctrl" Assembly="App_Web_0tcaeix_" %>--%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>    
    <div>
    Parent web form

        <uc:Child FirstName="Tom" runat="server"/>
    </div>    
   
</body>
</html>

Open in new window


Parent code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Child.Account;

namespace Parent
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            //Child.Child c = new Child.Child();

            //c.FirstName = "Tom";

            //this.Controls.Add(c);
        }
    }
}

Open in new window

0
 
LVL 5

Author Comment

by:knowlton
ID: 41768975
ping
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41769000
I have tried creating a simple web app on my laptop yesterday and VS froze twice. Today, I don't have much time for this and hope that somebody else could take the ball rolling!
0
 
LVL 5

Author Comment

by:knowlton
ID: 41769074
No worries, thank you for communicating, Eric.

Here is my current project as a ZIP file (rename extension from "123" to "zip")
0
 
LVL 5

Author Comment

by:knowlton
ID: 41769086
The first attempt to attach the file failed.

Here it is.
Default.aspx.cs
0
 
LVL 5

Author Comment

by:knowlton
ID: 41769087
0
 
LVL 5

Author Comment

by:knowlton
ID: 41769088
Could not attach the zipped file.
0
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 250 total points
ID: 41771339
You cannot use an ascx via http. Essentially you are trying to get the ASP.NET compiled and processed output of the user control if you do that.

Is this what you want ?

http://webproject.scottgu.com/CSharp/usercontrols/usercontrols.aspx

User Control Libraries Tutorial
0
 
LVL 5

Author Comment

by:knowlton
ID: 41771731
That is a Custom Server Control, right?

Yes, I decided to go that route.  I am no longer pursuing the .ascx.
0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 41771734
Thanks!

Tom
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question