User Control question

A

Andrea Williams

When I step through my code, the ASPX Page_Load happens before the Page_Load
in the User Control. This means that the property values are not set to
their defaults yet in the User Control and all the properties are blank. So
my question is, where is the best place to put the code that handles the
form. I was hoping to put it in the Page_Load on the ASPX page. Since the
properties aren't available to the ASPX page at that point, I have only a
few options that I can see.

1) Add all the Insert/Update code to the User Control, which I would rather
not do. I would like to keep the data manipulation out of the User Controls
so that I can use them other places. They are basically formatted forms
with exposed Properties. One problem with the separation idea is that the
Controls where I need to add events need to handled by the file that adds
the Control, unless I'm missing someway of wiring up events in the ASPX file
that happen in the User Control. Haven't seen a way to do that yet.
2) Use Request.Form to get the variables passed in the post. Although I've
found this to be problematic because sometimes the variable is prefixed with
'_ucl' or the User Control's name and a colon and sometimes it's not.
3) Add the manipulation code to and event that happens in the ASPX page.
For a form post I would add an event to the control I'm using for the submit
button.

Anyone care to comment? Give me some new ideas maybe?

Thanks in advance!
Andrea
 
B

bruce barker

you should implement IPostBackDataHandler, so that your user control will be
updated with postback data before the onload event.

-- bruce (sqlwork.com)
 
A

Andrea Williams

I've looked around on the web sites (that I know about) that offer samples
and found pretty cryptic information on this subject. Interfaces are pretty
new to me and I'm not sure that they have been properly explianed to me. (I
moved from Classic ASP.) Are there any code samples that might help me
understand this a little more? I see the basics but I'm not sure what to do
in the overridden methods.

In the LoadPostData method, for example. Do I need to do something to make
the values be populated in the Controls?

Do I have to add the Render method and render my controls with code, or can
I just leave it in the HTML?

I just need a basic example that take a couple of HTML declared controls and
exposes them so that I can access the values in the ASPX Page_Load method.
All the samples I've seen are for another purpose.

Thanks!
Andrea
 
J

Jim Corey

Andrea,

There's a few ways to set properties or call functions on the main page
from a user control. I'm using code like:

Dim myParent As MainReport

'The page is the HTTPHandler
myParent = CType(HttpContext.Current.Handler, MainReport)
myParent.myProperty = "T"
myParent.myFunction()


where MainReport.aspx is the page.

I have one page and I load one of many controls at a time
and this is working well for me.

HTH,
Jim
 
A

Andrea Williams

Set properties, yes. But if my property for a textbox in the User Control
looks like this:

public string Login
{
get{return txtLogin.Text;}
set{txtLogin.Text = value;}
}

And I try to get a value out of it from the ASPX Page_Load method, the value
is blank. This is b/c by default the Page_Load of the ASPX loads before the
UserControl loads, or at least that's what I'm assuming Since when stepping
through the code I get all the way through Page_Load for the ASPX and then
it pop over to the Page_Load in the User Control, where at that point, I can
see that now the Properties return values.

So if what you propose would fix my problem and allow me to access the
properties from the User Control in the Page_Load method of the ASPX page,
I'm not seeing it. Can you explain a little more?

Thanks!
Andrea
 
J

Jim Corey

No, my example would not help in the way you describe.
If you were reacting to a button on the user control you would be ok, since
the value would be there by the time the button_click event got fired (
after the page_load events for
the page and user control.

Plus I don't know if you're property is going to work, since there would
have to
be some way to persist 'value' across postbacks.

You may want to check some of the sample apps at www.asp.net / starter kits.
The time tracker app uses user controls and you may see a pattern that fits
what you're trying to do.

Jim
 
A

Andrea Williams

I have, actually, that's why I think that I should be able to do it this
way.

For an example, look at this URL:

http://www.dotnetjunkies.com/quicks...spplus/samples/webforms/pagelets/Pagelet5.src

When you see the source of the User Control, they are exposing the
properties as I would like to.

Later....

I just made a few modification and ran the debugger again... I don't know
why I wasn't seeing the values populated, but I am now. I'm very confused.
Maybe a caching issue the last time I ran it? Guessing...

I could go back to a previous version to see, but I'm so happy that it's
working that I don't want to spend any more time on it. If I run into
again, I'll pull back my old version and see if I can find where the problem
was.

I still would like to react to events from the User Control in my ASPX page
and would like some batter instructions and samples on that subject, should
anyone have something.

Thanks again!
Andrea
 

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

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top