Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created on web page using asp.net.After selecting particular student(name),from that student's id ,I am fetching all subject offered to that student from database.User can append/write marks got/achieved to that student(Taking input from user).After clicking on button,want to save data. Here I have created dynamical control but I unable to access it on button click event.How to do that?

What I have tried:

Code ASPX:

<asp:Panel ID="PnlStudent" runat="server" Visible="false">
                    <asp:Table ID="tblStudent" runat="server" Width="600px">
                        <asp:TableRow>
                            <asp:TableCell Height=" 32px" ForeColor="#32417a" BackColor="#e5e5e5" Width="156px">
                                Subject
                            </asp:TableCell>
                            <asp:TableCell Height=" 32px" ForeColor="#32417a" BackColor="#e5e5e5" Width="156px">
                                Marks Optined
                            </asp:TableCell>
                        </asp:TableRow>
                    </asp:Table>
                </asp:Panel>


Code in Cs file :

protected void ddlStudent_SelectedIndexChanged(object sender, EventArgs e)
    {
           // fetch from Student table


      if (dsStudent != null)
                {
                    DataTable dt = dsStudent.Tables["Student"];
                    if (dt.Rows.Count > 0)
                    {
                        PnlStudent.Visible = true;
                        foreach (DataRow dr in dt.Rows)
                        {
                            TableRow NewRow1 = new TableRow();

                            TableCell NewCell1 = new TableCell();
                            NewCell1.Width = Unit.Pixel(156);
                            NewCell1.Height = Unit.Pixel(32);
                            NewCell1.ForeColor = System.Drawing.Color.LightSlateGray;
                            NewCell1.BackColor = System.Drawing.Color.LightGray;

                            Label newLable1 = new Label();
                            newLable1.Text = dr["Subject"].ToString();


                            NewCell1.Controls.Add(newLable1);
                            NewRow1.Cells.Add(NewCell1);

                            TableCell NewCell2 = new TableCell();
                            NewCell2.Height = Unit.Pixel(32);
                            NewCell2.BackColor = System.Drawing.Color.LightGray;

                            TextBox txtBox1 = new TextBox();

                            NewCell2.Controls.Add(txtBox1);
                            NewRow1.Cells.Add(NewCell2);
                            tblStudent.Rows.Add(NewRow1);
                        }


                    }
                }
}


protected void BtnSave_Click(object sender, EventArgs e)
    {
}
Posted
Updated 28-Aug-17 2:38am

1 solution

Hi,

To do what you wish, first type a unique identification for each component that you add dynamically, like this:

C#
PnlStudent.Visible = true;
int index_row = 0;
foreach (DataRow dr in dt.Rows)
{
    TableRow NewRow1 = new TableRow();

    TableCell NewCell1 = new TableCell();
    NewCell1.Width = Unit.Pixel(156);
    NewCell1.Height = Unit.Pixel(32);
    NewCell1.ForeColor = System.Drawing.Color.LightSlateGray;
    NewCell1.BackColor = System.Drawing.Color.LightGray;

    Label newLable1 = new Label();
                    
    //type id in the label component
    newLable1.ID = "mylabel_" + index_row.ToString();
    
    newLable1.Text = dr["Subject"].ToString();
    NewCell1.Controls.Add(newLable1);
    NewRow1.Cells.Add(NewCell1);

    TableCell NewCell2 = new TableCell();
    NewCell2.Height = Unit.Pixel(32);
    NewCell2.BackColor = System.Drawing.Color.LightGray;

    TextBox txtBox1 = new TextBox();
    
    //type id in the textbox component 
    txtBox1.ID = "mytext_" + index_row.ToString();

    NewCell2.Controls.Add(txtBox1);
    NewRow1.Cells.Add(NewCell2);
    tblStudent.Rows.Add(NewRow1);

    index_row++;
}


To read the data in the component, do this:

C#
protected void BtnSave_Click(object sender, EventArgs e)
{
    string valuemytextbox, valuemylabel;
    int index_row = 0;
    bool header = true; // the first line is header 
    
    //read each line
    foreach (TableRow row in tblStudent.Rows)
    {
        // the first line is header
        if (header)
        {
            header = false;
        }
        else
        {
            //find components in the row
            valuemytextbox = ((TextBox)row.FindControl("mytext_" + index_row.ToString())).Text;
            valuemylabel = ((Label)row.FindControl("mylabel_" + index_row.ToString())).Text;

            //salve data in database
            ....

            index_row++;
        }
    }
}
 
Share this answer
 
v2

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