C # Webforms User Controls in VB.NET

I have an existing VB.NET web project that requires some new webforms. For a number of reasons, these new screens must be developed in C #. Realizing that this is really possible: Adding a C # web form to a VB web application and adding a C # control to an existing asp.net vb.net project

However, I did a little test:

  • VB.NET Solution Containing VB WebForms Application Project
  • Added new C # ASP.NET project to solve
  • Created a web user control in a C # assembly.
  • The linked and used web user control in Default.aspx is 1.

The problem is that the user control does not appear on the page. What I missed here. Is this something that is not possible in a VB.NET WebForms application? Below is the corresponding code:

Default.aspx

<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="waWithCUserControl._Default" %> <%@ Register TagPrefix="uac" Namespace="waUserControls" Assembly="waUserControls" %> <asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"> <uac:UcTakeTest ID="myUserControl" runat="server" /> </asp:Content> 

User control in a separate assembly waUserControls

 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ucTakeTest.ascx.cs" Inherits="waUserControls.UcTakeTest" %> <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet"> <div class="container"> <div class="row"> <div class="col-md-2"> <div class="list-group"> <a href="#" class="list-group-item active">Question</a> <a href="#" class="list-group-item">Question</a> <a href="#" class="list-group-item">Question</a> <a href="#" class="list-group-item">Question</a> <a href="#" class="list-group-item">Question</a> <a href="#" class="list-group-item">Question</a> <a href="#" class="list-group-item">Question</a> <a href="#" class="list-group-item">Question</a> <a href="#" class="list-group-item">Question</a> <a href="#" class="list-group-item">Question</a> </div> </div> </div> </div> 
+6
source share
1 answer

You need to rebuild the markup (only ASCX files) from the C # project to the project of your VB web application and specify these ASCX instead of C #. It sounds weird, but trust me, it works.

1 - Add the post-build event to the C # project, which copies the UserControl markup (.ASCX files only) to the folder in your web application project; for example, a folder under the root of your web application called "ExternalUserControls"

(Note that they must have the same Inherits property, i.e. the namespace and class name from the C # project.)

2 - Make sure the C # project is referencing (it seems that it already exists)

3 - Add management declarations to the <controls> <pages> of <system.web> in web.config: -

  <system.web> . . <pages> <controls> <add tagPrefix="cc" src="~/ExternalUserControls/MyControl.ascx" tagName="MyControl" /> <add .... etc 

4 - link to the controls on your page: -

 <cc:MyControl id="testControl" runat="server" .. 

5 - make sure that Import correct namespace for the control, as defined in a C # project

6 - Please note that sometimes the VS page designer refuses to add a variable for your control, therefore, in order to access your control in the code, you will need to add a level of protected at the page level (or any other VB equivalent, I cannot remember) a variable of the correct type, using the same name as in the page markup (testControl in the above example)

Let me know if you want an example.

+3
source

All Articles