C# Custom Control Postbacks

Started by
6 comments, last by Instruo 17 years, 1 month ago
Hello everyone, I've been having a hell of a time working with postbacks lately. I've found an enormous amount of information but none that either a.) speak directly to my problem or b.) make sense at all. My project is as follows: I have a custom control I created that simply takes in a variable of x data type and depending on the type, renders a control and label on the screen. For example, if they pass in a string data type that represents someones first name it will say "First Name:" on the label and put an empty text box next to it. Or if it was a datetime data type it would say "Date:" and have a calendar control next to it. etc etc. My problem is, I can not get the control to retain it's value on postback. It always resets no matter what I try. Perhaps I'm not understanding the way it all works (this is the first control I've designed). I've read multiple articles saying to use the RaisePostBackEvent but none of them explain it well enough for me to understand. Can anyone point me in the right direction? You guys are always an awesome help!
I will forever be a student.
Advertisement
Code would be helpful. Assuming you're talking about ASP.Net, can we at least see your contructor/page load and the event handler method's code?
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.
I'm not so sure my constructor or page load would be helpful. This is why I may be misunderstanding how this works. I am attempting to encapsulate everything within the controls class, do I need to incorporate the setting of the controls value via postback data within the page_load event or can it be done from within the class?
I will forever be a student.
My only concern with the constructor/page load is ensuring that things aren't getting re-initialized when you get to the post back. If they are, you can 'hide' them under a:

if (this.IsPostBack)
return;
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.
Ahh, it's always the smallest things. I bet that's it. My only question now is how do I determine if it's a postback from within the control? I know of Page.IsPostBack from the Page itself, but what is the appropriate method to use within a control?
I will forever be a student.
Quote:Original post by jrmiller84
Ahh, it's always the smallest things. I bet that's it. My only question now is how do I determine if it's a postback from within the control? I know of Page.IsPostBack from the Page itself, but what is the appropriate method to use within a control?


The same ... in fact, the reason that code has "Page" in front is so it works in a control. Otherwise it would just be "IsPostBack". Both pages and controls have "Page_Init" "Page_Load", ... events (if you use autowiring) and both have a "Page" property (because Page inheirits from Control).

BTW, you may have a problem in your control though, variables are not automatically persisted. If you write a custom control "MyControl" and have and internal data member "int x", that variable will NOT automatically be saved and reloaded on post-back. You have to write the persisting / loading code. Usually simply by writing and reading to ViewState.
Quote:Original post by Xai
BTW, you may have a problem in your control though, variables are not automatically persisted. If you write a custom control "MyControl" and have and internal data member "int x", that variable will NOT automatically be saved and reloaded on post-back. You have to write the persisting / loading code. Usually simply by writing and reading to ViewState.


I think you may have actually nailed it there. I just realized that it's not a reinitialization that's killing it. The variable IS like you are saying, a member of the class that I believe is not reloading.

So ViewStates persist across postbacks I presume? Similar to php sessions variables?
I will forever be a student.
Similar in functionality, yes. In actuality, ViewState variables are encoded into the page itself. However, that's just an implimentation detail, you can use them in the same way as you would a Session
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.

This topic is closed to new replies.

Advertisement