loadcontrol

Reply Forum
Forum postnasser @ 2009-02-08 16:20:58
ReplyReply to
hello dear asp.net instructor.
i'm just started asp.net and i have a little background on object oriented programming in c#.
my question is: why can't we use "m1 = new mywc()" instead of " m1=(mywc)LoadControl("~/mywc.ascx" ) ".
thank you for your nice tutorial.


Forum postWebmaster @ 2009-02-09 10:56:04
ReplyReply to
Hi,

Try using the Reference declaration on your page:

<%@ Reference Control="~/MyUserControls/MyControl.ascx" %>


Now you should be able to use the type from your Code Behind file. I'm not sure if you can instantiate it like any other type, with the new operator, or if you have to use the LoadControl method and then typecast to your type when you want to use specific members on your UserControl. Try it and let me know :)


Forum postOwl @ 2009-04-15 17:53:20
ReplyReply to
Hello webmaster,

is there possibly the CodeBehind file in its final finished form for us to look at somewhere?

I was making it ok in the tutorial until i got to this page:
http://asp.net-tutorials.com/user-controls/using/

protected void Page_Load(object sender, EventArgs e)
{
// These values can come from anywhere, but right now, we just hardcode them
MyUserInfoBoxControl.UserName = "Jane Doe";
MyUserInfoBoxControl.UserAge = 33;
MyUserInfoBoxControl.UserCountry = "Germany";
}

the above is causing the dreaded "Error 3 Expected class, delegate, enum, interface, or struct"

phUserInfoBox.Controls.Add(LoadControl("~/UserInfoBoxControl.ascx"));

It's unclear where exactly in the CodeBehind to put the above snippet.
Yes, i'm an ASP newbie.
Thanks in advance! :D


Forum postWebmaster @ 2009-04-16 08:20:54
ReplyReply to
Hi Owl,

A more complete example would look something like this:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BlahBlah : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		//Page_Load code here
    }
}


The BlahBlah part should be changed to the name of your current class :)











© net-tutorials.com 2006 - 2012

MailContact net-tutorials.com