动态创建表格和下拉列表 [英] Dynamic creation of Table and dropdown list

查看:82
本文介绍了动态创建表格和下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



使用C#代码,我需要通过代码动态创建表并将其放置在占位符中,还需要动态创建下拉框并将该下拉列表放入表中.

根据我在表的下拉列表中选择的值,我需要在表中再添加一行并在其中插入一个下拉列表.

我可以做的直到插入第一个下拉列表.当我在第一个下拉列表中选择某项时,总表消失了.由于回发,表消失了.

请告诉我回发后如何还原表中所有行的值.
代码

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

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;
            
        }

推荐答案

嗨btejaswaroop,

我认为您需要更改代码,使用ajax调用并使用ajax在客户端处理所有单击和选定的索引更改事件,而不必每次都回发整个页面.在代码中存储时,仅提交页面的一部分以进行操作,并在viewstate中存储表.

我不知道您的问题就不能给您代码,信息或示例.

请给我详细信息将解决您的问题


谢谢
纳恩·查瓦达(Nayan Chavada)
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


这篇关于动态创建表格和下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆