Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Control dissappearing

Status
Not open for further replies.

Zarcom

Programmer
May 7, 2002
1,275
CA
Oi I seem to be having a few problems of late.
I am starting to use alot of user controls in my code, however I keep getting some odd errors. I am hoping one of you out there can help me out on this.

I am programmatically adding a user control to my page. The control enscapulates a searching function. The user enters some data and is given a list to pick from. Once the user picks the record they want, I set a variable that is available through a property. Now on a button event on the main page I want to grab that value.

The problem is that when the user activates the search button on the user control, the entire control no longer exists. I followed the debugger and the post back returns to the main page goes through the Page_Init and Page_Load Functions and that is it. The variable CustomerSearch1 is declared that the top of the page with the other variables
Protected WithEvents CustomerSearch1 As RoamingForestCaCe.CustomerSearch

I am adding the control in the page load with the following code.
If Not IsPostBack Then
CustomerSearch1 = LoadControl("UserControls/CustomerSearch.ascx")
CustomerSearch1.EnableViewState = True
Panel1.Controls.Add(CustomerSearch1)
End If


I also tried to simply drag the control onto the designer. Things worked perfectly then except when I went to grab the data from the controls property. I had declared a variable at the top along with the other server controls like so.
Protected WithEvents CustomerSearch1 As RoamingForestCaCe.CustomerSearch
It doesn't seem to mesh up at all though when I try to grab the data from the main page.
str &= CustomerSearch1.CustID I don't get the data out that was in that variable after I select it in the UC. Am I going to have to loop through the controls collection to get my control and manipulate it?

Bah! Home time talk to you folks tomorrow. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Your problem is that you are loading the control after the viewstate has been loaded.

That happens prior to page load.

So, load the control in the page_init event, and your problems should disappear.

Check out our usergroup presentation on dynamic controls & events at:
Go to "past meetings" and download Stuart's code & tutorial.

When you load them dynamically like that, they have to be reloaded each and every time the page posts back. So when you want viewstate to be persisted over that postback, then it HAS to be loaded prior to that event.

Now, please note one more thing. Dynamically loaded controls have a serious problem with VB. VB seems to have a bug when it comes to their events. They sometimes won't fire at all... other times they won't fire the first time you try to fire them... and other times, they fire just fine. The behavior is sporatic and non-repeatable (a programmer's worst nightmare).

I approached him about this at the meeting, and he confirmed my dismay, but offered hope:

Use C#

C# doesn't have these problems, and according to his code & demonstration, doesn't even require the control to be loaded prior to viewstate. He & I both are unable to explain this behavior, other than to say that in order to appease the VB community, VB underwent some last minute tweaks that added some levels of abstraction between it & the framework (much to the dismay of microsoft & many other early adopters of .net). Apparently, when you get into some more advanced features of the framework (such as dynamic controls & events), these abstractions begin to breakdown and cause unexpected behavior.

C#, on the other hand, went through no such transformation, and is exactly as the designers envisioned it, and talks directly to the framework. That's the best I can explain it (and it's admittedly not very eloquent), but my advice to you is to jump ship and learn C#. If not for this reason alone, then let me espouse a few others:

1. Overloaded Operators
2. Handling of null
3. More elegant syntax
4. C programmers are worth more than VB developers (it's true, and we all know it)

Add to that the simplicity of making the switch, and it's a slam dunk. The classes are the same. The design patters are the same. The only thing that's different is the syntax. You can even use your existing class libraries that have been built in VB.

Ok, Mark, I'm now stepping down off my soapbox, and apologize for hijacking your thread. Hope my initial advice on your problem helps. It's just that this is one of the very issues that's making me make the switch, and would like to see others in this forum come along for the ride. I mean... hey... who am I going to go to for advice when I hit brick walls if everyone in here's talking VB? ;-)

-p
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
You know what Paul, I have actually been considering switching for a while. I just need to find a good point to make the break. A bloody hell! That was just shot down. I was just told by my boss that basically there is no way we are going to switch languages. Things must stay in VB.NET. However, I do plan on learning C# on my own anyway. Anyway, changing the code into the page_init doesn't solve it completely. Page_init is run everytime there is a post_back and so loads the control every time. If I include a Not IsPostBack then the control still dissappears. Thing is I need...(thinking) to grab that set value out of the control. ...(thinking)... Due to the nature of the web and such between postbacks the server does not hold things in memory does it? If I want to save a certain value between postbacks I am going to have to use either viewstate/hidden text box/session aren't I? Hmm brain is working here. I am going through those documents from your usergroup presentation now and will post back when done. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Mark, please note this statement that I made earlier:
When you load them dynamically like that, they have to be reloaded each and every time the page posts back. So when you want viewstate to be persisted over that postback, then it HAS to be loaded prior to that event.


That means that you can't do an if ispostback condition to load it. It means that you load it every time... no matter what. The reason that you have to do it in init is so that when viewstate is loaded, it will return the values of your dynamic control to the state that the user left them, thereby allowing you to retrieve them via properties you set up.

Just do it w/o the if ispostback, and then expose your values through properties, and grab them out as you need them (in or after page_load). It'll work.

-paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Right I was kinda just typing my ideas out as I thought of them. The property thing however, doesn't seem to be working.

I was simply setting a private variable in the user control class once the client selects which Customer they want. Then in the main page to get that Customer id I accessed a readonly property to the value. Thing is that the variable always comes back as blank. If I understand how things are working here that is because that variable isn't being stored anywhere between postbacks. What your saying is that if I followed what you said (and I have) then it should be stored in the viewstate, correct?

On another note have you ever used event bubbling? Once I have this property thing going I would like the main page to recieve an event when the customer is chosen so that it can fill itself with relevent data without more client interaction. Should keep me busy for a while anyway. I'll post once I find out more That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Got the event Bubbling to work and it actually solved my empty variable dilemma. I think I was correct in guessing that your variables in the user control class are not automatically saved into the view state. Since the variable is set on the same event that I wanted to bubble up to the parent, I simply grabbed it from the property in the OnBubbleEvent of the parent (my main page).

Heh heh if anyone wants to know more about Event Bubbling they can check it out here or ask me. I am guru! That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Variables are never saved over postback... unless you specifically put them there via the viewstate bag.

What is saved is the state of your controls, which you can return values on via your properties

public readonly property getRadioChecked as integer
get
dim output as integer
if rdo1.checked then
output = 1
elseif rdo2.checked then
output = 2
end if
return output
end get
end property
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top