Solved

Request for sample code - C# and ASP.NET

Posted on 2016-08-23
14
57 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 69

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 69

Expert Comment

by:Éric Moreau
ID: 41767382
from the Project menu, select Add References...
0
 
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 69

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
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.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now