Solved

HELP! ViewState / OnInit() Dyanamic Textboxes

Posted on 2004-09-04
10
936 Views
Last Modified: 2008-02-07
QUESTION: How can I maintain the viewstate of a panel that contains dynamically created formfields?

SCENARIO:

My Page is laid out like this (psuedo code) :

Page_Load()
{
  if (!IsPostBack){
   ...Connect to DB and get Categories
   ...Bind them to a DropDownList
  }
}
------ In the Form:
<asp:DropDownList ID="Category" AutoPostBack="true" OnSelectedIndexChanged="ShowSubcategories" runat="server"/>
-----------

void ShowSubcategories()
{
...Connect to DB and get all Subcategories related to the selected Category
...Bind them to a second DropDownList
}

------ In the Form:
<asp:DropDownList ID="Subcategory" AutoPostBack="true" OnSelectedIndexChanged="ShowPanel" runat="server"/>
-----------

void ShowPanel()
{
...Connect to DB
...Get however many detailIDs there are associated with the selected Subcategory and create dynamic texboxes.
...Add these to a asp:Panel inside part of a Form

  while (dr3.Read())
    {
     TextBox tBox=new TextBox();
     tBox.ID="frm"+dr3[0].ToString();
     tBox.CssClass="textfield_grey";
     tBox.Columns=35;
   
     form1.Controls.Add(tBox);
     myPanel.Controls.Add(tBox);
   }
}

-------- Finally in the Form:--------

<asp:Button ID="btnSubmit" Text="Submit" OnClick="GetPanelText" runat="server"/>
------------------------------------

void GetPanelText()
{
 ...this is where I am trying to see if the textboxes are still there with Response.Write something but I don't get output because I need to do something with ViewState.

   foreach (Control ctl in myPanel.Controls)
   {    
     Response.Write("yes");
    }
  }
}
0
Comment
Question by:champ_010
  • 5
  • 4
10 Comments
 
LVL 9

Expert Comment

by:glsac
ID: 11983040
It is more of a pain that ya think...I tried this awhile back, and found this control very helpful (it is free):

http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11983398

Bottom lines, while ASP.NET page maintains any server control (with viewstate enable) which is embedded in design time, it does not do the same with any control that is created dynamically. Therefore it's your job to maintain or recreate and re-assign to a container/page.

Page_Load()
{
  if (!IsPostBack){
   ...Connect to DB and get Categories
   ...Bind them to a DropDownList
  }
  else
  {
   ...here again call the ShowPanel method
   ShowPanel();
  }
}

HTH
0
 
LVL 1

Author Comment

by:champ_010
ID: 11983465
Thanks--I tried that yesterday and today after reading your posting but I get this error:

CS1501: No overload for method 'ShowPanel' takes '0' arguments.

What's missing?
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11983576

It means in the ShowPanel method has been defined one or more parameters, although it's not showing in your first comment. I guess you have modified the function. Try to pass sufficient parameter into it.
0
 
LVL 1

Author Comment

by:champ_010
ID: 11984709
Hi, yes, ShowPanel is like this:

ShowPanel(object Sender, EventArgs e)
{

}

So in the Page_Load do I put that in as well?  It doesn't work either...???
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 20

Expert Comment

by:ihenry
ID: 11984726
well, it turns out to be an event instead. when this event is being triggered?
0
 
LVL 1

Author Comment

by:champ_010
ID: 11985638
In the DropDownList: <asp:DropDownList ID="Subcategory" AutoPostBack="true" OnSelectedIndexChanged="ShowPanel" runat="server"/> which is populated after a previous dropdown was selected.
0
 
LVL 1

Author Comment

by:champ_010
ID: 11985665
void Page_Load(object Sender,EventArgs e)
{
   if(!IsPostBack)
    {
      ...Connect to DB get CategoryNames to populate first dropdown
      ...OnSelectedIndexChanged=ShowSubs
     }else{
            //How to do ShowSubs() again--with parameters
     }
}

void ShowSubs(object Sender, EventArgs e)
{
    ...Connet to DB get SubcategoryNames based on CategoryName to populate second dropdown
    ...OnSelectedIndexChanged=ShowPanel
}

ShowPanel(object Sender, EventArgs e)
{
   ...Connect to DB
   ...Get however many detailIDs there are associated with the selected Subcategory and create dynamic texboxes.
   ...Add these to a asp:Panel inside part of a Form

}
0
 
LVL 20

Accepted Solution

by:
ihenry earned 300 total points
ID: 11988127
Well, it turns out not to be so simple. As I said in my previous post about the "Bottom lines...",  use that as a guidelines everytime you create controls dynamically. In your case, after btnSubmit is pressed you need something so that the page would know it was posted back by the button. And another more important thing, the textboxes have to be re-created before the GetPanelText method is being triggered.

Separate the implementation of ShowPanel event to a new method, lets call it PopulatePanel() and follow this pseudo-code:

private bool isButtonPressed = false;

void Page_Load(object Sender,EventArgs e)
{
   if(!IsPostBack)
    {
      ...Connect to DB get CategoryNames to populate first dropdown
      ...OnSelectedIndexChanged=ShowSubs
     }else {
            // do nothing
     }
}

ShowPanel(object Sender, EventArgs e)
{
     PopulatePanel();
}

void GetPanelText(object Sender, EventArgs e)
{
     PopulatePanel();
     isButtonPressed = true;
}

protected override void OnPreRender( EventArgs e )
{
     if ( isButtonPressed == true ) {
           foreach (Control ctl in myPanel.Controls) {    
                  Response.Write("yes");
           }
     }
     base.OnPreRender(e);
}

void PopulatePanel()
{
   ...Connect to DB
   ...Get however many detailIDs there are associated with the selected Subcategory and create dynamic texboxes.
   ...Add these to a asp:Panel inside part of a Form
}
0
 
LVL 1

Author Comment

by:champ_010
ID: 11994098
Ugh, looks like I'll have to learn what the PreRender stuff is.  I don't know what this does:

protected override void OnPreRender( EventArgs e )
{
     if ( isButtonPressed == true ) {
          foreach (Control ctl in myPanel.Controls) {    
                Response.Write("yes");
          }
     }
     base.OnPreRender(e);
}

In any case, you have answered my question.  I've built the page in a different way and it's working so I won't be trying out your code suggestions right away but it definately gives me an idea on what to do and what I need to learn still.  Do you think the viewstate problem for dynamic controls is every going to be 'fixed' or improved?

Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now