Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,
Please help me to find solution for this problem

What i have-
1.
i have a page which have some dropdownlist controls and on selectedIndexChanged event i call a method in which some dynamic controls are generated.

these controls are 2 labes and one text box

2.
on any buttonClick event i want to get labels and textbox value and stored it into database



What My Problem-
i am unable to get values of dynamically generated controls...


My Code is-
On Aspx Page
ASP.NET
<table>
    <tr>
        <td>
            <table>
                <tr>
                    <td>Select Subject</td>
                    <td>
                        <asp:DropDownList ID="ddlSubject" runat="server">
                        </asp:DropDownList>
                    </td>
                    <td>Select Class</td>
                    <td>
                        <asp:DropDownList ID="ddlClass" runat="server" AutoPostBack="true"
                            onselectedindexchanged="ddlClass_SelectedIndexChanged">
                        </asp:DropDownList>
                    </td>
                    <td>Select Section</td>
                    <td>
                        <asp:DropDownList ID="ddlSection" runat="server" AutoPostBack="true"
                            onselectedindexchanged="ddlSection_SelectedIndexChanged">
                        </asp:DropDownList>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<br />
<div id="studentList"  runat="server"></div>
<asp:Button ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" runat="server" />



On Aspx.cs

C#
protected void ddlSection_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindStudents();
    }
    protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindStudents();
    }
    public void bindStudents()
    {
        qry = "select * from Student_info where class='" + ddlClass.SelectedValue.ToString() + "' AND section='" + ddlSection.SelectedValue.ToString() + "'";
        SqlCommand cmd = new SqlCommand(qry, con);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();


        Table table = new Table();
        table.Attributes["class"] = "dynamic_table";
        studentList.Controls.Add(table);

        TableRow trh = new TableRow();

        TableCell tch1 = new TableCell();
        Label lblIdHeader = new Label();
        lblIdHeader.Text = "Student_Id";
        tch1.Controls.Add(lblIdHeader);

        TableCell tch2 = new TableCell();
        Label lblTitleHeader = new Label();
        lblTitleHeader.Text = "Student_Name";
        tch2.Controls.Add(lblTitleHeader);

        TableCell tch3 = new TableCell();
        Label lblMarksHeader = new Label();
        lblMarksHeader.Text = "Marks";
        tch3.Controls.Add(lblMarksHeader);

        trh.Cells.Add(tch1);
        trh.Cells.Add(tch2);
        trh.Cells.Add(tch3);
        table.Rows.Add(trh);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            TableRow tr = new TableRow();

            TableCell td1 = new TableCell();
            Label lblId = new Label();
            //lblId.ID = "lbl_" + ds.Tables[0].Rows[i]["Id"].ToString();
            lblId.ID = "lblId_" + i;
            lblId.ClientIDMode = System.Web.UI.ClientIDMode.Static;
            lblId.Text = ds.Tables[0].Rows[i]["Id"].ToString();
            td1.Controls.Add(lblId);

            TableCell td2 = new TableCell();
            Label lblTitle = new Label();
            lblTitle.Attributes["style"] = "text-transform:uppercase;";
            //lblTitle.ID = "lbl_" + ds.Tables[0].Rows[i]["SName"].ToString();
            lblTitle.ID = "lblName_" + i;
            lblTitle.ClientIDMode = System.Web.UI.ClientIDMode.Static;
            lblTitle.Text = ds.Tables[0].Rows[i]["SName"].ToString();
            td2.Controls.Add(lblTitle);

            TableCell td3 = new TableCell();
            TextBox txtValue = new TextBox();
            //txtValue.ID = "txt_" + ds.Tables[0].Rows[i]["Id"].ToString();
            txtValue.ID = "txtMarks_" + i;
            txtValue.ClientIDMode = System.Web.UI.ClientIDMode.Static;
            td3.Controls.Add(txtValue);
            //studentList.Controls.Add(new LiteralControl("<br />"));
            tr.Cells.Add(td1);
            tr.Cells.Add(td2);
            tr.Cells.Add(td3);
            table.Rows.Add(tr);
        }
        studentList.Controls.Add(table);
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        qry = "select * from Student_info where class='" + ddlClass.SelectedValue.ToString() + "' AND section='" + ddlSection.SelectedValue.ToString() + "'";
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = qry;
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        qry = "insert into result (studentname,studentid," + ddlSubject.SelectedItem.ToString() + ") values(@studentname,@studentid,@marks)";
        cmd.CommandText = qry;
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            Label lblId = (Label)studentList.FindControl("lblId_" + i);
            Label lblName = (Label)studentList.FindControl("lblName_" + i);
            TextBox txtStudMarks = (TextBox)studentList.FindControl("txtMarks_" + i);

            cmd.Parameters.AddWithValue("@studentname", lblName.Text.ToString());
            cmd.Parameters.AddWithValue("@studentid", lblId.Text.ToString());
            cmd.Parameters.AddWithValue("@marks", Convert.ToInt32(txtStudMarks.Text));
            cmd.ExecuteNonQuery();
        }
        con.Close();
        lblmsg.Text = lblmsg.Text + "Success";
    }
Posted
Updated 17-Sep-14 19:19pm
v2
Comments
RAHUL(10217975) 16-Sep-14 7:23am    
Are you able to find control ,means lblId you getting it as Label Control or null??

1. Define the "table" variable as global like "Table table;".
2. In the binding function change the following lines from
C#
Table table = new Table();
table.Attributes["class"] = "dynamic_table";
studentList.Controls.Add(table);


to

C#
table = new Table();
table.ID = "DynamicTable";
table.Attributes["class"] = "dynamic_table";
studentList.Controls.Add(table);


3. Now in your button click get the table from the studentList like

C#
Table table = (Table)studentList.FindControl("DynamicTable");


4. To access the controls in the table
C#
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
   Label lblId = (Label)table.FindControl("lblId_" + i);
}


It has helped me retrieve the value of the control and hope this helps you too.
 
Share this answer
 
v2
Comments
Kriti Sharma 10400841 18-Sep-14 0:44am    
i have change these line of code but still same problem
ChauhanAjay 18-Sep-14 9:39am    
Please check the updated solution hope it helps.
Kriti Sharma 10400841 22-Sep-14 1:53am    
changed the code, still same problem.. on button click exception generated..
System.NullReferenceException: Object reference not set to an instance of an object.

in these lines

Label lblId = (Label)table.FindControl("lblId_" + i);
Label lblName = (Label)table.FindControl("lblName_" + i);
TextBox txtStudMarks = (TextBox)table.FindControl("txtMarks_" + i);
ChauhanAjay 22-Sep-14 3:20am    
Can you kindly add a try catch block in your button click event and check what is the exact error message.

Suggestion:- While coding please don't forget to add try catch block.
Kriti Sharma 10400841 3-Oct-14 6:08am    
i have solve this problem by using viewstate.. but thanx for ur help..
Set Enable view state property true for your dynamic control.

and then use Page.FindControl method where you want to use it.
 
Share this answer
 
Comments
Kriti Sharma 10400841 22-Sep-14 2:08am    
i have try this but still having same exception
studentList.FindControl("conrolid")
and you can type cast the control
 
Share this answer
 
i have solve this problem by using viewstate..
 
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