Custom controls

J

josema

Hi to all,

this is my question....

Webcontrol1
________________________
| |
|initialize component |
| button +=new event..... |
| controls.add(button) |
| |
|event onclick |
| -control.add(webcontrol2) |
----------------------------------

webcontrol2
_____________________________
| |
| InitializeComponent |
| button2 +=new EventHandler...|
| -controls.add(textbox) |
| -controls.add(button2) |
| Onlclick |
| Response.write("hello") |
-----------------------------------------

The problem its that the webcontrol2 when you cick the button2 and the textbox dissapear from the webcontrol1 and dont show the word "hello"

thanks in advance
josema.
 
V

Victor Garcia Aprea [MVP]

Hi Josema

What is happening is that you're creating a child control only in a click event of a button; so if you cause a postback by clicking any other button (or control) than the original button, the code that creates that first button will never execute and will "dissapear"

--
Victor Garcia Apre
Microsoft MVP | ASP.NE
Looking for insights on ASP.NET? Read my blog
http://obies.com/vga/blog.asp
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
 
J

Josema

How could i solve this??, i have to generate some textboxes dinamically (depends of a number), after the pressing of a button..
Josema
 
J

Josema

Thanks Alessandro, for your fast reply..

Could i use a placeholder?... cause i have to load a web custom control..

i cant use this line, cause i dont have a user control...
c1 = LoadControl("webusercontrol2.ascx"

Josem

----- Alessandro Zifiglio wrote: ----

hi Josema, I had posted a workaround a few weeks ago in this forum. Follo
up on that post and see if you are able to resolve. I have tested and i
works nicely. Look at the last post in particular

http://groups.google.com/groups?hl=...APlaceHolder+on+asp.net+page+from+user+contro

Josema said:
How could i solve this??, i have to generate some textboxes dinamicall
(depends of a number), after the pressing of a button..
 
A

Alessandro Zifiglio

if its a custom control just add it like you always did. The placeholder and
usercontrols scenario etc is specific to that particular user who posted his
problem. All you need to do is use that same logic, that is the viewstate
and how its being applied in pageload. If you look at the code, when the
button is being clicked that adds controls to the page, a flag is set in
viewstate, and you use that same flag in the page_load method to add the
control if it had already been added. Also move your logic for adding
controls to the page_load sub and not init, viewstate is available when
page_load fires ;)

Let me know if your having difficulty ;P
 
A

Alessandro Zifiglio

almost forgot -- use a placeholder if you want your controls to go at a
specific location on the page.
 
A

Alessandro Zifiglio

oops, I didnt realize you were adding those controls inside a custom web
control, i was under the impression that this was all going on in your
webform. Should still work, however using a placeholder here is useless.
There is no page_load method too, still you should still be able to use
viewstate and apply that logic. You might want to use the viewstate in your
CreateChildControls method, Viewstate is already available to you at this
time.

so it is :
1. when button is clicked add your child controls to your customwebcontrols
collection.
2. right after you add the controls, set a flag in viewstate --so after a
postback you can use this flag to check and see if these child controls had
been added.
3. in your createchildcontrols method check for this flag in viewstate and
if it is set, then call your method that adds your child controls,
recreating them again after postback.
 
A

Alessandro Zifiglio

You are welcome, josemari.
Postback if your having difficulty ;)
josema said:
Thanks Alessandro, i will test the instructions that you give me...
Thanks for your time, it was very interesting to me, and i see that the
viewstate can be very useful...
 

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

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top