Passing values to a custom control

C

Chris Kennedy

I am building a custom composite control which reads in information from an
XML file. I want to be able to point my control at different XML files for
maximum reusablity. How can I pass a parameter to my component. I am a bit
new to this but ideally would like the rest of my team to be able to use
this component in their projects and just tell the component which XML file
to point at.
 
M

Mark Fitzpatrick

Through Properties. Basically you have a private variable defined in your
control, such as

private string _Location = "";

Now, you create get/set accessors like so:
public string Location
{
get
{
return _Location;
}
set
{
_Location = value;
}
}

That will set it up so that you now have a publically available property
called Location that will be abe to have a value passed to it, and retrieved
from it. Now, the only other thing you'll have to do is make use of this
value. The problem is, you don't want to use the Page_Load event of the
control. That happens at the wrong time in the scheme of things. The
Page_Load of the a sub-control will be loaded before the Page_Load of the
parent page. That means any value you set the control to in the parent page
won't be useful to you in the Page_Load event of the sub-control. You can
override the PreRender instead and place your code that makes use of the
_Location value there instead.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
V

Victor Garcia Aprea [MVP]

Chris, Mark,
This is actually not correct. The Load event for the Page will fire before
the Load event of any of its children.
Hummm... maybe thats your impression, I dont see anything wrong with that
order :)

Also chances are that you may want to use viewstate instead of a private
member field as the storage for your property so it maintains state between
postbacks.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
 
C

Chris Kennedy

That has pointed me in the right direction but the problem I am having now
is the control doesn't seem to want to use the string. To clarify, what I am
doing is using the XML file to tell the control dynamically what controls to
put on the page so it is called in the CreateChildControls() event. I am
setting my location property in the onLoad event of the parent page. I am a
little new to this and find the timing of events a little confusing when
compared the classic ASP. Regards.
 
C

Chris Kennedy

Oh I thought I'd better mention the error I get is CompControl1.Location =
"C:\Inetpub\wwwroot\AVTest\XMLFile1.xml"
The path is not of a legal form.
 
S

Sanket Naik

Hi,
Maybe u can try this stuff
"C:\\Inetpub\\wwwroot\\AVTest\\XMLFile1.xml"
If this does not work
the other way is
Server.MapPath(Path of the xml file)

Cheers,
Sanket Naik



C:\Inetpub\wwwroot\AVTest\XMLFile1.xml
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top