Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 599
  • Last Modified:

Child control state being lost on postback

I've created a custom control.

In the  load event of the control, I'm adding another control into a div that is present in the page.

So it's something like this:

MyControl
ControlX
DivX - present in the page

i.e. In MyControl Load Event
I'm doing DivX.controls.Add(ObjControlX);

Now ControlX contains a textbox.

On click of submit button I want the value of the textbox. Right now it is coming as blank.

How do I do it.
0
informaniac
Asked:
informaniac
  • 8
  • 6
2 Solutions
 
Jon500Commented:
Can you use (objType)ControlX.FindControl("name of your control")? You have to cast the return value to the object type of your control...
0
 
informaniacAuthor Commented:
Yeah I'm doing the same thing but it is returning as null.

In the load event of the control I did DivX.Controls.Count and it is returning me 1, which is a literal control
0
 
Jon500Commented:
What type of object is DivX?
Can you show your code?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
informaniacAuthor Commented:
DivX is a panel Control. Showing the code is a bit difficult.


0
 
Jon500Commented:
It could be a lot of things...

1. Set a break point on your Page_Load event's first line and examine the Page object in the Debug watch window. Look for your user control, then drill down to the text box and see if it has a value.

2. Are you using try blocks with no catch? Could you be suppressing a runtime error?

3. Could you be inadvertently clearing the textbox after postback?

Regards,
Jon
0
 
informaniacAuthor Commented:
1. It's not a user control. It's a custom control. While posting back In the page load event the ControlX is not loaded in the page.

2. That's not the case. No error is being suppresed

3. I'm not clearing the textbox.


However I checked the Request.Form variables, I'm getting the textbox there.
0
 
Jon500Commented:
OK--so the data returns in postback. Now let's see why it is not in your object...

Other thoughts:

You have implemented the IPostBackDataHandler interface?
Are you managing the viewstate?

Might this help you solve this?
http://www.aspfree.com/c/a/ASP.NET/ASPNET-Custom-Server-Controls-Cute-ASPNET-TextBox-Control/

0
 
informaniacAuthor Commented:
No Viewstate is disabled in my page. How would IPostBackDataHandler Help?
I'm not handling it right now.
0
 
Jon500Commented:
When you access the .Text property of your textbox custom control, you are access the control's public "getter" (get) property to obtain its value and its "setter" (set) property to assign its value. I wonder how you have implemented these properties in your control. Often Viewstate is used by the setter and by the getter.

You need not implement IPostBackDataHandler. I just wanted to know whether to factor that into the issue.

Regards,
Jon
0
 
informaniacAuthor Commented:
Seems to be a problem when there are child controls in another custom control u are using.

I added those childcontrols onto my page and they work fine.

i.e.

MyControl - My control
ControlX - Contains txtbox.
DivX - Panel

If I Added txtbox directly onto MyControl it worked fine.
0
 
Jon500Commented:
I suppose I feel it is unfair to post a question for 500 points, give very poor detail as to what is happening, refuse to provide any source code whatsoever, go back-and-forth for 10 messages, then reveal something critical at the last moment (that the text box was not in his custom control but was instead in a panel), then try to close the question and effective award 0 points. I would have preferred a request by the author to reduce the question value by 50% and award half the points just for trying to understand what his scenario was. It is quite possible that no others chimed in not because they had no answer but because they saw that I was trying to ascertain facts about the scenario and was doing/asking what they might have done had they participated in the question.

The article I provided in my #26302698 response provides a working tutorial of a custom control that contains a text box. The author at that time could have reviewed that helpful link and could have stated to me that there is no text box in his custom control. Knowing this would have led me to an answer or the author could have implemented the article's suggestion, which detailed how to build a custom control containing a text box and respond to its events and value.

In EE, an Expert might not always be able to give the author a specific answer, but I think most authors understand this and award points for the effort given to help. Experts receive no money or other compensation for their efforts and rely on points as recognition of effort. To give 0 here is simply not fair, in my opinion.

Regards,
Jon500
0
 
Jon500Commented:
My objection is being dismissed?

Regards,
Jon
0
 
Jon500Commented:
Oppsed to closure.
0
 
informaniacAuthor Commented:
Take all the points you want.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now