Access C# class in another page

Associate
Joined
6 Oct 2008
Posts
41
Hi folks,

I'm having difficulty accessing C# classes from other pages.

To keep this explanation simple, I have a couple of ascx pages which are loaded into an aspx page.
I have created a custom method in the ascx page that will save everything from the page controls into a database table.

ascx page:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public class panels_app1 : System.Web.UI.UserControl
{
    public void AppSave()
    {
        if (Page.IsValid)
        {
            ApplicantBLL appBLL = new ApplicantBLL();

            appBLL.AddApplicant(chkHealth.Checked, chkHelp.Checked, chkTalk.Checked, ddlTitle.SelectedValue,
                    txtFirstName.Text, txtSurname.Text, ddlGender.SelectedValue, txtAddr1.Text, txtAddr2.Text,
                    txtAddr3.Text, txtAddr4.Text, txtAddr5.Text, ddlCountry.SelectedValue,
                    Convert.ToInt32(txtUKYears.Text), txtPostcode.Text, txtTel.Text, txtMobile.Text,
                    txtEMail.Text, Convert.ToDateTime(txtDOB.Text), Convert.ToInt32(txtAge.Text),
                    ddlEthnicity.SelectedValue, txtEthnicOther.Text, txtNI.Text, txtULN.Text);

            Response.Redirect("http://sqlserv1/misintranet");
        }
    }
}

aspx page:
Code:
using AjaxControlToolkit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Apply : System.Web.UI.Page
{
    protected Control _formApp, _formCourse, _formEducation, _formRefs, _formSubmit;

    private void LoadPanels()
    {
        _formApp = Page.LoadControl("~/panels/app1.ascx");
        plhApp.Controls.Add(_formApp);

        _formCourse = Page.LoadControl("~/panels/course.ascx");
        plhCourse.Controls.Add(_formCourse);

        _formEducation = Page.LoadControl("~/panels/education.ascx");
        plhEducation.Controls.Add(_formEducation);

        _formRefs = Page.LoadControl("~/panels/references.ascx");
        plhRefs.Controls.Add(_formRefs);

        _formSubmit = Page.LoadControl("~/panels/submit.ascx");
        plhSubmit.Controls.Add(_formSubmit);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadPanels();
    }

    protected void btnSave1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            _formApp.AppSave();
        }
    }
}

But I can't seem to access the method from the ascx page, does anyone know where I am going wrong?
 
Its because, as that point, you are accessing just a 'Control' with all the 'Control' methods and properties. Try:
((panels_app1)_formApp).AppSave();

This will work as long as you get the type right, else you will get an invalidcastexception.

You could, also, change:
protected Control _formApp, _formCourse, _formEducation, _formRefs, _formSubmit;
to
protected panels_app1 _formApp, _formCourse, _formEducation, _formRefs, _formSubmit;
 
As Goksly says, you don't seem to be dealing with a concrete type, merely an object of type Control. You could try casting the control to a concrete type
(i.e. panels_app1 _formApp = Page.LoadControl("~/panels/app1.ascx") as panels_app1; ?

Would that not work?

EDIT: eugh, too many underscores for my liking! :p
 
OK, I've changed the top line to:

Code:
protected panels_app1 _formApp, _formCourse, _formEducation, _formRefs, _formSubmit;

But it still can't find panels_app1:
Error 1 The type or namespace name 'panels_app1' could not be found (are you missing a using directive or an assembly reference?)
 
An ascx is a control. You don't have controls talking directly to the database. Your controls should expose the necessary properties to the page, which does all the database work for you.
 
You need to register the control on the page... i.e.
<%@ Register Src="~/Controls/PickForm.ascx" TagName="PickForm" TagPrefix="pop" %>
 
Back
Top Bottom