0
votes

I have user control placed in my .aspx page. User controls are added dynamically on button click event. My problem is, after post back, I can retrieve server controls placed inside user control but cant retrieve their values.

Here is the code of my user control's .ascx page

   <%@ Control Language="C#" AutoEventWireup="true"   odeFile="qualificationControl.ascx.cs" Inherits="qualificationControl" %>
                    <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>

Here is the code of my .axcs.cs page

    using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Web;
     using System.Web.UI;
     using System.Web.UI.WebControls;
     using System.Data;

    public partial class qualificationControl : System.Web.UI.UserControl
    {
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            using (DataOperation oDo = new DataOperation())
            {
                DataTable dt = oDo.DropDownList("select * from tblqualificationMaster");
                foreach (DataRow row in dt.Rows)
                {
                    courseList.Items.Add(new ListItem(row[1].ToString(), row[0].ToString()));
                }
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}

Code of my .aspx page

<%@ Page Title="Application Form Level2" Language="C#" MasterPageFile="~/AppMaster.master" AutoEventWireup="true" CodeFile="AppplicationForm2.aspx.cs" Inherits="AppplicationForm2" %>

<%@ Register src="Control/qualificationControl.ascx" tagname="qualificationControl" tagprefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<table width="100%">
    <tr>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td class="SubTitle">
            Education details:</td>
    </tr>
    <tr>
        <td runat="server" id="tdQualificationn">

            <%--<uc1:qualificationControl ID="qualificationControl1" runat="server" />--%>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                   <asp:PlaceHolder ID="UserCtrlHolder" runat="server"></asp:PlaceHolder>
                </ContentTemplate>

           <%-- <Triggers> <asp:AsyncPostBackTrigger ControlID="addQualificationBtn" /></Triggers>--%></asp:UpdatePanel>
        </td>
    </tr>
    <tr>
        <td align="center">
            <asp:Button ID="addQualificationBtn" runat="server" 
                Text="Add More Qualifications" Height="40px" 
                onclick="addQualificationBtn_Click" />
        </td>
    </tr>
</table>

</asp:Content>

Code of my .aspx.cs page

    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;
    ArrayList CtrlList = new ArrayList();
    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);
            Session.Add("ControlHolder", UserCtrlHolder);
        }
    }
    protected void addQualificationBtn_Click(object sender, EventArgs e)
    {
        RememeberOldValues();
        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]);
        }
    }    
    public void RememeberOldValues()
    {
        try
        {
            if (Session["ControlHolder"] != null)
                {
                    ArrayList CourseList = new ArrayList();
                    ArrayList YearList = new ArrayList();
                    ArrayList percentageList = new ArrayList();
                    ArrayList InstituteList = new ArrayList();
                    ArrayList CtrlList = (ArrayList)Session["qualicontrol"];
                    PlaceHolder PlaceHolder = (PlaceHolder)Session["ControlHolder"];
                    for (int intListCnt = 0; intListCnt < CtrlList.Count; intListCnt++)
                    {
                        Control userControl = (Control)PlaceHolder.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");

                        CourseList.Add(dlCourseList.SelectedValue);
                        YearList.Add(dlYearList.SelectedValue);
                        percentageList.Add(percentageBox.Text);
                        InstituteList.Add(InstiNameBox.Text);
                    }
                }
        }
        catch (Exception ex)
        {
           throw;
        }
    }
}

Please guide me how can I retrieve those values.

1

1 Answers

1
votes

You have to find particular control from user control

((DropDownList)qualificationControl1.FindControl("yearList")).SelectedValue