call c# usercontrol from page

  • 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 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
LVL 33
Robberbaron (robr)Asked:
Who is Participating?
Kyle AbrahamsConnect With a Mentor Senior .Net DeveloperCommented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.