We help IT Professionals succeed at work.

call c# usercontrol from vb.net page

Low Priority
Last Modified: 2018-02-14
  • Asp.net 4.6
  • I have a c# user control tested and working with a c# webform, made possible from sample code on EE and SO.
  • I now need to call that c# usercontrol from an older vb.net page but cant work out how to make the delegate work.

the ctl
using System.Configuration;

public delegate void ReturnIdtoPageHandler(string messageToThePage);

public partial class Controls_getProjectId :  System.Web.UI.UserControl
    public event ReturnIdtoPageHandler ReturnIdtoPage;

    #region Properties
    public string ProjectId { get; set; }
    public string ProjectDetails { get; set; }
    public string NameMinor { get; set....

    protected void butSelect_Click(object sender, EventArgs e)
        ProjectId = lbResults.SelectedItem.Value;
        string[] separators = { "::", "__" };
        ProjectDetails = lbResults.SelectedItem.Text;
        string[] nms = lbResults.SelectedItem.Text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
        NameMajor = nms[1]; if (nms.Length == 3) { NameMinor = nms[2]; } else { NameMinor = ""; }
        [b]ReturnIdtoPage( ProjectId);[/b]

Open in new window

c# webform
        ctlGetProjectId.ReturnIdtoPage += delegate (string message)
            tbProjId.Text = message;
            Label1.Text = ctlGetProjectId.NameMajor + ctlGetProjectId.NameMinor;

Open in new window

    Protected Sub Controls_getProjectId_ReturnIdtoPageHandler(ByVal sender As Object, ByVal e As EventArgs) Handles Controls_getProjectId.ReturnIdtoPageHandler

    End Sub

Open in new window

but the PageHandler is not recognised as an event
Watch Question

Senior .Net Developer
Use addhandler:

Public Sub ReturnIdtoPageHandler(ByVal message As String)
  tbProjId.Text = message
  Label1.Text = ctlGetProjectId.NameMajor + ctlGetProjectId.NameMinor
end sub

'add this to your page_load
AddHandler ctlGetProjectId.ReturnIdtoPage, AddressOf  ReturnIdtoPageHandler

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.