Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old January 6th, 2006, 12:45 AM
Authorized User
 
Join Date: Dec 2005
Location: Bangalore, , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to kapi.goel
Default

Peter,

I am having a drop down list on first page, on selecting the value and then a button click, new form is opening (actually both pages are into same aspx file "only thing i am doing i.e. displaying the panel for the new page and hiding a panel for the first page having drop down list") - on this second form I am displaying dynamically created text boxes (creation is happening on button click event of the first page), so now on submission of second form (submit button event) i am trying to access the values entered in dynamically created text boxes.

I hope u got the whole story now.



KG
Reply With Quote
  #12 (permalink)  
Old January 6th, 2006, 08:18 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Yes, and I think I now know what the problem is....

You are dynamically generating the multiple text boxes on the event handler of the first postback. Then you cause another postback with a different button. The problem is that the page maintains viewstate data for control *properties* only. It does NOT maintain the hierarchical structure of the control tree. So when the second postback happens, those textboxes you generated in the event handler from the first postback aren't in the control tree and thus you can not access them.

The solution is to build the page in such a way that you can regenerate the dynamic list on every postback regardless of the event. This usually entails storing enough information in viewstate that you can reconstruct the dynamic controls. In your case, you need to store the number of textboxes created into a viewstate value.

1. Build a single method that constructs the textboxes and puts them where you need them. This method should look at the value in viewstate for the number. (You'll need to check for a null value and handle it gracefully.)
2. In your button click handler that generates the controls, set the viewstate value to the number chosen from the DDL and then call the new method.
3. In the page load event handler, call the method. This will regenerate the control structure on each page hit.

This is the basic process. From your description, it sounds like this basic implementation will cover your needs. (Strange things happen if you don't hide the control that manipulates what will be generated but you shouldn't have that problem with what you described.)

Good luck, and let us know how you do.

-Peter
Reply With Quote
  #13 (permalink)  
Old January 6th, 2006, 11:15 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You need to put the code that generates the controls on the fly in the initialisation event. This will allow the controls to be created and then their viewstate valus applied.

If you try it in page load, viewstate will not be applied to them.

I recently did the same thing, its tricky to get sorted but once you have is extremley flecible.

Cheers

Squatss

Reply With Quote
  #14 (permalink)  
Old January 6th, 2006, 12:24 PM
Authorized User
 
Join Date: Dec 2005
Location: Bangalore, , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to kapi.goel
Default

Hey i am done with the problem, its rectified now.
I simply added the code in page load event, and removed from button click event. As i forgot to tell u, i had only one page (2 forms full with panels), so i avoided the viewstate value. Direclty accessed the DDL value.

tnx a lot peter



KG
Reply With Quote
  #15 (permalink)  
Old January 6th, 2006, 01:18 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by squatss
 If you try it in page load, viewstate will not be applied to them.

That is not correct. Controls added to the page on Page.Load do get their viewstate applied to them. My understanding is that a control added to a page will get its viewstate regardless of when it's added. The important thing is that certain things won't happen based on when the control is loaded. If the control is loaded after the page is done with the RaisePostbackEvents portion of execution then postback events for dynamically added controls won't fire.

-Peter
Reply With Quote
  #16 (permalink)  
Old January 23rd, 2006, 06:45 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It works fine if you add controls at page init but my requirement is to add control on butoon click. I am not getting any solution for this. Thanks in advance.
Reply With Quote
  #17 (permalink)  
Old January 23rd, 2006, 11:32 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by pgurtatta
 It works fine if you add controls at page init but my requirement is to add control on butoon click.

You can do this, but you also have to store something to viewstate so that the next page hit will re-add the dynamically added control on each page lifecycle (even on a pass when you didn't press the button that adds the control).

Usually this means storing some data in viewstate that defines the controls that are being added. Then the page init routine can check that value and add the controls in order to faithfully reproduce what you added by clicking the button.

-Peter
Reply With Quote
  #18 (permalink)  
Old January 30th, 2006, 04:33 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peter,

Thanks, but can you provide me with some sample code for this. Also what if the number of added controls depends upon data from som data source. In that cse if the number of records is large then is it good to use viewstate for that. One more question, after the controls are added how can i access those controls in my code??

Thanks.
Reply With Quote
  #19 (permalink)  
Old February 1st, 2006, 01:10 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

That is one of the drawbacks of have such dynamic pages. Perhaps you could use a standard repeating control (datagrid, datalist, repeater) to handle the creation of the dynamic set of controls. These controls also handle all the viewstate necessary to re-create the controls on subsequent postbacks.

-Peter
Reply With Quote
  #20 (permalink)  
Old February 2nd, 2006, 07:21 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peter,

Since data for me comes from database so i am using repeater control for adding controls dynamically. To achieve this I have included a class which implemets ITemplate interface and has function 'InstantiateIn'. This function is determining which type of ListItem I am adding and based on that it adds controls. Whenever databinding happens for repeater control this function is called and controls get added. But now problem comes with state mgmt.

I am adding TextBox Control and this control and its value does not persist on postback. If I don't bind repeater again on postback it does not show dynamically added controls and if i bind controls are recreated but values lost.

If repeater can persist controls and their values on postback thing will work for me but it doesnot.

Following is the class and function definition for your reference:

public class MyTemplate : System.Web.UI.Page,ITemplate
{
   public void InstantiateIn(System.Web.UI.Control container)
   {
    switch( templateType )
    {
        case ListItemType.Header:
            break;
        case ListItemType.Item:

            TextBox t = new TextBox();
            t.ID = "t" + itemcount;

            container.Controls.Add(t);

            break;

        case ListItemType.AlternatingItem:

            break;
        case ListItemType.Footer:

            break;
    }
    itemcount += 1;
   }
}

And following is the code behind of WebPage:

Repeater1.HeaderTemplate = new MyTemplate(ListItemType.Header);

Repeater1.ItemTemplate = new MyTemplate (ListItemType.Item);

Repeater1.AlternatingItemTemplate = new MyTemplate(ListItemType.AlternatingItem);

Repeater1.FooterTemplate = new MyTemplate(ListItemType.Footer);

Repeater1.DataSource = MydataTable;
Repeater1.DataBind();

Thanks
Parveen
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic button control SKhna ASP.NET 2.0 Basics 1 February 1st, 2008 10:36 AM
Dynamic control events??? feffe Pro VB 6 21 October 21st, 2007 05:47 PM
Dynamic control moving Ramacharyulu C# 2005 3 March 9th, 2007 12:11 AM
Dynamic placing of a control Warbird General .NET 1 July 9th, 2004 02:19 PM
Dynamic Control array carl.dufresne@sita.aero Visual C++ 6 September 3rd, 2003 08:48 AM



All times are GMT -4. The time now is 05:14 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.