Sign in to follow this  
jrmiller84

C# Custom Control Postbacks

Recommended Posts

jrmiller84    118
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!

Share this post


Link to post
Share on other sites
Instruo    373
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?

Share this post


Link to post
Share on other sites
jrmiller84    118
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?

Share this post


Link to post
Share on other sites
Instruo    373
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;

Share this post


Link to post
Share on other sites
jrmiller84    118
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?

Share this post


Link to post
Share on other sites
Xai    1848
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.

Share this post


Link to post
Share on other sites
jrmiller84    118
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?

Share this post


Link to post
Share on other sites
Instruo    373
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this