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");
}
}
}
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=
}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
}