Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Shwetha here,

Based on Number selected in Dropdownlist(1,2,3).I am creating the table row, table cell and Textbox and a Label in that table cell.i am succeeded in creating those dynamic controls based on number selected in Dropdownlist.But i failed in reading the values or data of those controls on submission (btnSubmit_Click()) of data..

please help me in this regard..i have posted my code also

aspx code:

ASP.NET
<asp:Content ID="Content1" ContentPlaceHolderID="ContentRequestor" runat="Server">

<table width="95%" border="0" cellpadding="0" cellspacing="0" class="form_approaval">

<tr>
<td>
<asp:Label ID="lblParentName" runat="server" Font-Size="12px" Font-Names="segoe UI"
Text="Parent Name"></asp:Label>
</td>
<td style="height: 24px; width:10%">
<asp:TextBox ID="txtParentName" CssClass="search_bg" runat="server" Enabled="false"></asp:TextBox>
</td>
</tr>
    
<tr>
<td>
<asp:Label ID="lblNumberofKids" runat="server" Text="Number of Kids Participating"
Font-Size="12px" Font-Names="segoe UI"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlNumberofKids" runat="server" CssClass="select" Width="160px" AutoPostBack="true" OnSelectedIndexChanged="ddlNumberofKids_SelectedIndexChanged">
<asp:ListItem Text="----Please Select----" Value="0" Selected="True"></asp:ListItem>
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
                
<asp:Panel ID="pnl" runat="server"></asp:Panel>
              
<table width="95%" border="0" cellpadding="0" cellspacing="0" class="form_approaval">

<tr>
<td>
<asp:Label ID="lblPhoneNum" runat="server" Text="Phone Number" Font-Size="12px" Font-Names="segoe UI"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtPhoneNum" CssClass="search_bg" runat="server" MaxLength="10"></asp:TextBox>
</td>
</tr>
               
<tr>
<td>
<asp:Button CssClass="Page_Button" ID="btnSubmit" Text="SUBMIT" runat="server" OnClick="btnSubmit_Click" />
</td>
</tr>
</table>
</asp:Content>




code Behind:

SummerExpressRegistrationDO ObjSummer = new SummerExpressRegistrationDO();
    SummerExpressRegistrationManager ObjMgr = new SummerExpressRegistrationManager();


  protected void ddlNumberofKids_SelectedIndexChanged(object sender, EventArgs e)
    {
        Table tbl = new Table();
        TableRow tr = new TableRow();
        TableCell tc;
        Label lbl;
        TextBox txt;
        tbl.CssClass = "form_approaval";
        tbl.Width = Unit.Percentage(96);
        int max = int.Parse(ddlNumberofKids.SelectedValue.ToString());
        for (int i = 1; i <= max; i++)
        {
            Session["Identity"] = i;
            TableRow tr1 = GenerateTable(tbl, tr, out tc, out lbl, out txt);
            tbl.Rows.Add(tr1);
            pnl.Controls.Add(tbl);
        }

    }


    private TableRow GenerateTable(Table tbl, TableRow tr, out TableCell tc, out Label lbl, out TextBox txt)
    {

        int Identity = int.Parse(Session["Identity"].ToString());
        tr = new TableRow();
        tc = new TableCell();
        lbl = new Label();
        tc.Width = Unit.Percentage(20);
        lbl.Text = "Kids Age";
        tc.Controls.Add(lbl);
        tr.Cells.Add(tc);
        tbl.Rows.Add(tr);

        tc = new TableCell();
        txt = new TextBox();
        txt.Width = Unit.Pixel(160);
        txt.ID = "txtAge" + Identity;
        tc.Controls.Add(txt);
        tr.Cells.Add(tc);
        tbl.Rows.Add(tr);


        tc = new TableCell();
        lbl = new Label();
        tc.Width = Unit.Percentage(15);
        lbl.Text = "Kids Name";
        tc.Controls.Add(lbl);
        tr.Cells.Add(tc);
        tbl.Rows.Add(tr);


        tc = new TableCell();
        txt = new TextBox();
        txt.Width = Unit.Pixel(160);
        txt.ID = "txtName" + Identity;
        tc.Controls.Add(txt);
        tr.Cells.Add(tc);

        return tr;
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int Identity = int.Parse(Session["Identity"].ToString());
        ObjSummer.Emp_ID = Session["LoginId"].ToString();

        if (ObjMgr.CheckUserForSummerExpressRegistration(ObjSummer).ToString() == "Exists")
        {
            Response.Redirect("Sorry.aspx");
        }
        else
        {
            ObjSummer.Emp_ID = Session["LoginId"].ToString();
            ObjSummer.Number_of_Kids = int.Parse(ddlNumberofKids.SelectedValue.ToString());
            if (Identity == 1)
            {
                int txtage1 = "txtAge" + Identity;
                ObjSummer.Kid1_Age = int.Parse(txtage1.Text);
                ObjSummer.Kid1_Name = ("txtName" + Identity).Text;
            }
            else if (Identity == 2)
            {
                ObjSummer.Kid1_Age = int.Parse("txtAge" + Identity).Text;
                ObjSummer.Kid1_Name = ("txtName" + Identity).Text;
                ObjSummer.Kid2_Age = int.Parse("txtAge" + Identity).Text;
                ObjSummer.Kid2_Name = ("txtName" + Identity).Text;
            }
            else if (Identity == 3)
            {
                ObjSummer.Kid1_Age = int.Parse("txtAge" + Identity).Text;
                ObjSummer.Kid1_Name = ("txtName" + Identity).Text;
                ObjSummer.Kid2_Age = int.Parse("txtAge" + Identity).Text;
                ObjSummer.Kid2_Name = ("txtName" + Identity).Text;
                ObjSummer.Kid3_Age = int.Parse("txtAge" + Identity).Text;
                ObjSummer.Kid3_Name = ("txtName" + Identity).Text;
            }
            ObjSummer.Emp_Phonenum = long.Parse(txtPhoneNum).Text;
            ObjMgr.InsertSummerExpressRegistration(ObjSummer);

            //On successfull insertion of User data into Database, Page is redirected to ThankYou.aspx
            Response.Redirect("ThankYou.aspx", false);
        }
    }
}
Posted

1 solution

You need to review the basics

int txtage1 = "txtAge" + Identity;

This will not work. You can't assign a string to an int

int.Parse("txtAge" + Identity)

Text cannot be parsed to a valid int

("txtName" + Identity).Text;

What are you trying to do? Get a control? It doesn't work this way. Use FindControl.

There are so many things wrong with this I would suggest going back to simple learning exercises until you understand the concepts and tools.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900