I have user control which contains two text boxes and two dropdown list. I am loading this control first time on page_load
event programatically. After that on each button click event of "Add New" button, controls are added dynamically but when this happens, previously loaded controls are showing all server controls(which are inside user control) empty. How can i retain the state of these controls?
My user control code is as shown below.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="qualificationControl.ascx.cs" Inherits="qualificationControl" %>
<style type="text/css">
.RowHeight
{
height: 30px;
}
</style>
<table width="100%">
<tr>
<td class="RowHeight" width="20%">
Course Name</td>
<td width="20%">
<asp:DropDownList ID="courseList" runat="server" Width="100px">
</asp:DropDownList>
</td>
<td width="20%">
Year of Passing</td>
<td width="*">
<asp:DropDownList ID="yearList" runat="server" Width="100px">
<asp:ListItem Value="0">2005</asp:ListItem>
<asp:ListItem Value="1">2006</asp:ListItem>
<asp:ListItem Value="2">2007</asp:ListItem>
<asp:ListItem Value="3">2008</asp:ListItem>
<asp:ListItem Value="4">2009</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="RowHeight" width="20%">
Percentage</td>
<td colspan="3">
<asp:TextBox ID="percentageBox" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="RowHeight" width="20%">
Instutitute Name</td>
<td colspan="3">
<asp:TextBox ID="InstiNameBox" runat="server" Width="350px"></asp:TextBox>
</td>
</tr>
</table>
and that of aspx.cs page is as below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
public partial class AppplicationForm2 : System.Web.UI.Page
{
Control qualificationControl;
UserControl usrqualificationControl = new UserControl();
int CtrlID = 0;
protected void Page_Load(object sender, EventArgs e)
{
qualificationControl = usrqualificationControl.LoadControl("~/control/qualificationControl.ascx");
if (!IsPostBack)
{
ArrayList CtrlList = new ArrayList();
qualificationControl.ID = CtrlID.ToString();
UserCtrlHolder.Controls.Add(qualificationControl);
CtrlList.Add(qualificationControl);
Session.Add("qualiControl", CtrlList);
}
//else
}
protected void addQualificationBtn_Click(object sender, EventArgs e)
{
ArrayList CtrlList = null;
ArrayList CourseList = new ArrayList();
ArrayList YearList = new ArrayList();
ArrayList percentageList = new ArrayList();
ArrayList InstituteList = new ArrayList();
if (Session["qualiControl"] != null)
{
CtrlList = (ArrayList)Session["qualicontrol"];
}
qualificationControl.ID = CtrlList.Count.ToString();
CtrlList.Add(qualificationControl);
for (int i = 0; i < CtrlList.Count; i++)
{
UserCtrlHolder.Controls.Add((Control)CtrlList[i]);
}
}
private void RestoreUserControl(ArrayList CourseList,ArrayList YearList,ArrayList PercentageList,ArrayList InstiNameList,ArrayList CtrlList)
{
for (int CtrlCnt = 0; CtrlCnt < CtrlList.Count - 1; CtrlCnt++)
{
Control userControl = (Control)UserCtrlHolder.FindControl(CtrlID.ToString());
DropDownList dlCourseList = (DropDownList)userControl.FindControl("courseList");
DropDownList dlYearList = (DropDownList)userControl.FindControl("yearList");
TextBox percentageBox = (TextBox)userControl.FindControl("percentageBox");
TextBox InstiNameBox = (TextBox)userControl.FindControl("InstiNameBox");
dlCourseList.ClearSelection();
dlYearList.ClearSelection();
dlCourseList.Items.FindByText(CourseList[CtrlCnt].ToString()).Selected = true;
dlYearList.Items.FindByText(CourseList[CtrlCnt].ToString()).Selected = true;
percentageBox.Text = PercentageList[CtrlCnt].ToString();
InstiNameBox.Text = PercentageList[CtrlCnt].ToString();
}
}
}