Click here to Skip to main content
16,019,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Using C# code, i need to Create a Table dynamically by code and place it in placeholder and i also need to create dropdown box dynamically and place that dropdown list into the table.

Based on the value i select in the Drop down list in table, i need to add one more row into table and insert one more dropdown list into it.

I am able to do till insertion of first dropdown list. when i select some item in the first dropdown list, total table is disappearing. Table is vanishing because of postback.

Please tell me how to restore the values of all rows in table after post back.
Code
C#
public void CreateTable()
        {
            tbl = new Table();
            tbl.BorderColor = System.Drawing.Color.Black;
            PlaceHolder1.Controls.Add(tbl);
            
                TableRow tr = new TableRow();
                tr.BackColor = System.Drawing.Color.Orange;
                tr.ID = "row0";
                TableCell tc1 = new TableCell();
                TableCell tc2 = new TableCell();
                //creating lable
                System.Web.UI.WebControls.Label lable1 = new System.Web.UI.WebControls.Label();
                System.Web.UI.WebControls.Label lable2 = new System.Web.UI.WebControls.Label();
                lable1.Text = "Questions";
                lable1.BackColor = System.Drawing.Color.Orange;
                lable2.Text = "Answers";
                lable2.BackColor = System.Drawing.Color.Orange;
                // Add the control to the TableCell
                tc1.Controls.Add(lable1);
                tc2.Controls.Add(lable2);
                // Add the TableCell to the TableRow
                tr.Cells.Add(tc1);
                tr.Cells.Add(tc2);
                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            
        }

        public void AddRow(string s1,string[] s2)
        {
            TableRow tr = new TableRow();
            tr.ID = "row" + Rows.ToString();
            TableCell tc1 = new TableCell();
            tc1.ID = "tc1" + Rows.ToString();
            TableCell tc2 = new TableCell();
            tc2.ID = "tc2" + Rows.ToString();
            //creating dropdown list
            DropDownList ddl = new DropDownList();
            ddl.ID = "list" + Rows.ToString();
            ddl.Items.Add("Select Answer");
            for (int i = 0; i < s2.Length; i++)
            {
                ddl.Items.Add(s2[i]);
            }
            ddl.SelectedIndexChanged += new EventHandler(dl_SelectedIndexChanged);
            ddl.AutoPostBack = true;
            ddl.EnableViewState = true;
            //creating lable
            System.Web.UI.WebControls.Label lable1 = new System.Web.UI.WebControls.Label();
            lable1.Text = s1;
            lable1.ID = "lable" + Rows.ToString();
            // Add the control to the TableCell
            tc1.Controls.Add(lable1);
            tc2.Controls.Add(ddl);
            // Add the TableCell to the TableRow
            tr.Cells.Add(tc1);
            tr.Cells.Add(tc2);
            // Add the TableRow to the Table
            tbl.Rows.Add(tr);
        }

        protected void dl_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl9 = (DropDownList)sender;
            
            int quesid = 0;
            string questxt = "";
            int ansid = 0;
            string anstxt = ddl9.SelectedItem.Text;
            DataRow[] dr = dt4.Select("anstxt='" + anstxt + "'");
            foreach (DataRow row in dr)
            {
                quesid = Convert.ToInt32(row["quesid"]);
                ansid = Convert.ToInt32(row["ansid"]);
                questxt = row["questxt"].ToString();
                //questxt=row[
            }
            dt3.Rows.Add(quesid, questxt, ansid, anstxt);
            foreach (DataRow row in dt2.Rows)
                if (Convert.ToInt32(row["quesid"]) == quesid)
                    row["son"] = "s";
            //ListBox1.Items.Add("Ans: " + anstxt);
            //TextBox1.Text = "";
            //DropDownList1.Items.Clear();
            foreach (DataRow row in dt1.Rows)
            {
                if (Convert.ToInt32(row["ansid"]) == ansid)
                {
                    row["status"] = 1;
                    //MessageBox.Show("row status changed to" + row["status"].ToString());
                }
            }

            ViewState["CECdt1"] = dt1;
            ViewState["CECdt3"] = dt3;
            int i = GetQuestion(dt1, dt2);
            GetAnswers(i);
            ViewState["CECtbl"] = PlaceHolder1;
            
        }
Posted
v2
Comments
Can you please post your codes ?
btejaswaroop 24-Nov-12 9:58am    
public void CreateTable()
{
tbl = new Table();
tbl.BorderColor = System.Drawing.Color.Black;
PlaceHolder1.Controls.Add(tbl);

TableRow tr = new TableRow();
tr.BackColor = System.Drawing.Color.Orange;
tr.ID = "row0";
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
//creating lable
System.Web.UI.WebControls.Label lable1 = new System.Web.UI.WebControls.Label();
System.Web.UI.WebControls.Label lable2 = new System.Web.UI.WebControls.Label();
lable1.Text = "Questions";
lable1.BackColor = System.Drawing.Color.Orange;
lable2.Text = "Answers";
lable2.BackColor = System.Drawing.Color.Orange;
// Add the control to the TableCell
tc1.Controls.Add(lable1);
tc2.Controls.Add(lable2);
// Add the TableCell to the TableRow
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
// Add the TableRow to the Table
tbl.Rows.Add(tr);

}

public void AddRow(string s1,string[] s2)
{
TableRow tr = new TableRow();
tr.ID = "row" + Rows.ToString();
TableCell tc1 = new TableCell();
tc1.ID = "tc1" + Rows.ToString();
TableCell tc2 = new TableCell();
tc2.ID = "tc2" + Rows.ToString();
//creating dropdown list
DropDownList ddl = new DropDownList();
ddl.ID = "list" + Rows.ToString();
ddl.Items.Add("Select Answer");
for (int i = 0; i < s2.Length; i++)
{
ddl.Items.Add(s2[i]);
}
ddl.SelectedIndexChanged += new EventHandler(dl_SelectedIndexChanged);
ddl.AutoPostBack = true;
ddl.EnableViewState = true;
//creating lable
System.Web.UI.WebControls.Label lable1 = new System.Web.UI.WebControls.Label();
lable1.Text = s1;
lable1.ID = "lable" + Rows.ToString();
// Add the control to the TableCell
tc1.Controls.Add(lable1);
tc2.Controls.Add(ddl);
// Add the TableCell to the TableRow
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
// Add the TableRow to the Table
tbl.Rows.Add(tr);
}

protected void dl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl9 = (DropDownList)sender;

int quesid = 0;
string questxt = "";
int ansid = 0;
string anstxt = ddl9.SelectedItem.Text;
DataRow[] dr = dt4.Select("anstxt='" + anstxt + "'");
foreach (DataRow row in dr)
{
quesid = Convert.ToInt32(row["quesid"]);
ansid = Convert.ToInt32(row["ansid"]);
questxt = row["questxt"].ToString();
//questxt=row[
}
dt3.Rows.Add(quesid, questxt, ansid, anstxt);
foreach (DataRow row in dt2.Rows)
if (Convert.ToInt32(row["quesid"]) == quesid)
row["son"] = "s";
//ListBox1.Items.Add("Ans: " + anstxt);
//TextBox1.Text = "";
//DropDownList1.Items.Clear();
foreach (DataRow row in dt1.Rows)
{
if (Convert.ToInt32(row["ansid"]) == ansid)
{
row["status"] = 1;
//MessageBox.Show("row status changed to" + row["status"].ToString());
}
}

ViewState["CECdt1"] = dt1;
ViewState["CECdt3"] = dt3;
int i = GetQuestion(dt1, dt2);
GetAnswers(i);
ViewState["CECtbl"] = PlaceHolder1;

}

c
btejaswaroop 24-Nov-12 10:04am    
Hi again its me,

here create table is called in page load to to create header row first, and GetAnswers(i) method will call AddRow(s1,s2) method to display the values from database in table, in new row. when i refresh the page, table is disappearing. please help me to overcome it.
Abhishek Pant 24-Nov-12 11:14am    
have a look at this http://www.youtube.com/watch?v=2UDRm-hU0Jw
btejaswaroop 24-Nov-12 12:42pm    
from this video, i am not able to get exactly what i want. Please cnd you give me some code, to get values of my table once postback is called.

1 solution

Hi btejaswaroop,

I think you need to change your code, Use ajax call and handle all click and selected index change event in client side using ajax without postbacking whole page every time. submit only partial portion of page for operation and store tables in viewstate as you store in you code.

I can't give you code or information or example without knowing your problem.

Please give me detail will solve you'r problem


Thanks
Nayan Chavada
 
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