This is a follow-up to a question I asked yesterday.
I'm loading a UC programatically as such:
================================================== =
public customContentControl as UserControl
customContentControl =
CType(Page.LoadControl("mycontrol.ascx"),UserContr ol)panel_controlHolder.Con
trols.Add(customContentControl)
================================================== =
This works fine, but got hung up on how to now pass a property or variable
value to this loaded control.
It was suggested to use a base class or an interface. I've done a bit with
interfaces, but wanted to try understanding how the base class option would
be imnplemented.
What I eventually would like to be able to do is pass a value like this:
================================================== =
customContentControl.property = 'whatever'
================================================== =
Now, from what I understand, my problem is that I'm casting the loaded UC as
a 'UserControl' instead of a base-class derived from the UserControl class.
Am I on the right path with that logic? If so, what does the base-class
definition do. Is it merely creating a namespace so I can access it? 8 1580
This is the other way round.
When you create a user control, you are creating a class that inherits from
UserControl (ie. UserControl is your control base class).
As when you load your control, you use a UserControl variable, you are only
able to access to what UserControl does (and your control is able to perform
all this as it inherits these capabilities from the UserControl (base)
class).
If you load your control using a MyControl variable, you'll be able to
access all the capabilites included those you added (change also the CType).
Patrice
--
"darrel" <no*****@hotmail.com> a écrit dans le message de
news:ut*************@tk2msftngp13.phx.gbl... This is a follow-up to a question I asked yesterday.
I'm loading a UC programatically as such:
================================================== = public customContentControl as UserControl customContentControl =
CType(Page.LoadControl("mycontrol.ascx"),UserContr ol)panel_controlHolder.Con trols.Add(customContentControl) ================================================== =
This works fine, but got hung up on how to now pass a property or variable value to this loaded control.
It was suggested to use a base class or an interface. I've done a bit with interfaces, but wanted to try understanding how the base class option
would be imnplemented.
What I eventually would like to be able to do is pass a value like this:
================================================== = customContentControl.property = 'whatever' ================================================== =
Now, from what I understand, my problem is that I'm casting the loaded UC
as a 'UserControl' instead of a base-class derived from the UserControl
class. Am I on the right path with that logic? If so, what does the base-class definition do. Is it merely creating a namespace so I can access it?
> If you load your control using a MyControl variable, you'll be able to access all the capabilites included those you added (change also the
CType).
Your explanation helps. Thanks! I'm still not quite sure *what* I need to
change the CType to, though. Is it the class of the UC I am loading?
-Darrel
Hello darrel,
I posted an example of how to accomplish what you need in my answer to your
previous post. Did you have additional questions?
--
Matt Berther http://www.mattberther.com If you load your control using a MyControl variable, you'll be able to access all the capabilites included those you added (change also the CType).
Your explanation helps. Thanks! I'm still not quite sure *what* I need to change the CType to, though. Is it the class of the UC I am loading?
-Darrel
> I posted an example of how to accomplish what you need in my answer to
your previous post. Did you have additional questions?
I'm taking a second look at your example. It's in C#, so it's a bit foreign
to me, but from what I can tell, you are creating a class based on the
System.Web.UI.UserControl namespace and then, within that class, setting
some properties. That appears to be the same thing as using an interface. Is
that correct?
So based on that, would all of my controls that I want to dynamically load
then be based on this particular base class? And then, instead of loading
these as usercontrols, instead load them as usercontrol.myBaseClass?
-Darrel
I imagine you've figured it out by now, but here's what worked for me:
I created a base class:
Public Class BaseReportControl
Inherits System.Web.UI.UserControl
with properties and Overridable subs and functions that I might be
using in
the inherited controls.
Then to load one of the controls, I used:
Dim ctlReport As BaseReportControl
ctlReport = CType(LoadControl(SubReport), BaseReportControl)
where SubReport is a string like "ctl.ascx"
It's important to set the ID property if you're going to want to have
controls reloaded and functioning on postback. The point is to make
sure
a control has the same (arbitrary) value for ID every time.
darrel wrote: I posted an example of how to accomplish what you need in my answer
to your previous post. Did you have additional questions? I'm taking a second look at your example. It's in C#, so it's a bit
foreign to me, but from what I can tell, you are creating a class based on
the System.Web.UI.UserControl namespace and then, within that class,
setting some properties. That appears to be the same thing as using an
interface. Is that correct?
So based on that, would all of my controls that I want to dynamically
load then be based on this particular base class? And then, instead of
loading these as usercontrols, instead load them as usercontrol.myBaseClass?
-Darrel
Hello darrel,
Yes... an interface would work as well.
When you load them, you can do something like this:
[C#]
IMyControlInterface instance = (IMyControlInterface)Page.LoadControl("somecontrol .ascx");
[VB]
Dim instance As IMyControlInterface
instance = CType(Page.LoadControl("somecontrol.ascx"), IMyControlInterface)
My VB.NET knowledge is non-existant, so the above may not be syntactically
correct, but you should get the idea.
--
Matt Berther http://www.mattberther.com I posted an example of how to accomplish what you need in my answer to your
previous post. Did you have additional questions? I'm taking a second look at your example. It's in C#, so it's a bit foreign to me, but from what I can tell, you are creating a class based on the System.Web.UI.UserControl namespace and then, within that class, setting some properties. That appears to be the same thing as using an interface. Is that correct?
So based on that, would all of my controls that I want to dynamically load then be based on this particular base class? And then, instead of loading these as usercontrols, instead load them as usercontrol.myBaseClass?
-Darrel
> I imagine you've figured it out by now, but here's what worked for me: I created a base class: Public Class BaseReportControl Inherits System.Web.UI.UserControl
with properties and Overridable subs and functions that I might be using in the inherited controls.
Then to load one of the controls, I used: Dim ctlReport As BaseReportControl ctlReport = CType(LoadControl(SubReport), BaseReportControl)
where SubReport is a string like "ctl.ascx"
It's important to set the ID property if you're going to want to have controls reloaded and functioning on postback. The point is to make sure a control has the same (arbitrary) value for ID every time.
Thanks! That helps! I think I got this working. I'll likely post a follow-up
question tomorrow.
-Darrel
> Yes... an interface would work as well.
Thanks for all the help, Matt. I think I got things working using the
base-class concept. I'll likely be posting a followup in the next day or two
;o)
-Darrel This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: james |
last post by:
I have a custom UserControl, which can have many sub class levels
derived from it. I want to be able to discover all the components at
Load time, but the only components I can see from the base...
|
by: Wysiwyg |
last post by:
I'm new to c# programming and can't figure out how to avoid duplicating
common code in multiple classes when I'm restricted to using different
system base classes.. I'm using c# in asp.net to write...
|
by: Davíð Þórisson |
last post by:
Please can someone tell me how on earth to create an instance of my top
level (base) Page class so that I can access it's objects from an user
control?? Someone told me
public myParent =...
|
by: Wade |
last post by:
Hi all,
We have created some "Base" class pages for our WebForms and UserControls.
For instance, when we create a WebForm called "WebForm1.aspx", instead of
inheriting from "System.Web.UI.Page"...
|
by: Jackson |
last post by:
I want a class that will determine its base class by the argument passed
in. What I am about to write _does_not_work_, but it shows what I am
trying to do.
class ABC(some_super):
def...
|
by: Swami |
last post by:
I have 2 questions relating to website design in asp .net:
1. In a website that I am building I have everything as a user control. Even
the header, which contains the navigation tabs is in a user...
|
by: droyad |
last post by:
I have recently upgraded a web project from ASP.NET 1.1 to ASP.NET 2.0,
and now the event handlers no longer work, due to the event handlers
being defined in base classes.
To start a bit of...
|
by: nyathancha |
last post by:
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is...
|
by: rjl444 |
last post by:
web developer express asp.net 2.0:
I need to have 3 user controls. these controls include a textbox and
several asp controls (checkbox, button, etc). I would like to have a
base class that...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
| |