从一个DropDownList分配给TD表中的值 [英] assign to a td table the value from a dropdownlist

查看:68
本文介绍了从一个DropDownList分配给TD表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个asp页面这样创造了一个循环表

I am creating a table in a loop in an asp page like this:

   foreach (ListItem item in check.Items)
            {
                if (item.Selected)
                {

                    DropDownList drop = new DropDownList();

                    drop.Items.Add("1");
                    drop.Items.Add("2");

                    drop.DataValueField = "1";
                    drop.DataValueField = "2";





                    TableRow row = new TableRow();
                    TableCell celula = new TableCell();
                    celula.Style.Add("width", "200px");
                    celula.Style.Add("background-color", "red");

                    celula.RowSpan = 2;
                    celula.Text = item.Value;
                    TableCell celula1 = new TableCell();
                    celula1.Style.Add("background-color", "green");
                    celula1.Style.Add("width", "200px");

                    celula1.RowSpan = 2;

                    TableCell celula2 = new TableCell();
                    celula2.Style.Add("width", "200px");
                    celula2.Style.Add("background-color", "blue");
                    celula2.Text = "unu";
                    TableRow row1 = new TableRow();
                    TableCell cel = new TableCell();
                    cel.Text = "lalala";
                    cel.Style.Add("background-color", "brown");




                    celula1.Controls.Add(drop);
                    row.Cells.Add(celula);
                    row.Cells.Add(celula1);
                    row.Cells.Add(celula2);
                    row1.Cells.Add(cel);
                    this.tabel.Rows.Add(row);
                    this.tabel.Rows.Add(row1);



                }
            }

有关我检查每一个值,一个新行与选中的复选框,一个DropDownList和2个细胞的价值创造。现在,我要为我创造,使每个something.So的DropDownList如果我选择1第一行的东西会出现,如果我选择1另一行别的东西就会出现。我做了一个的SelectedIndexChanged但如果我从第一行选择1,它会显示为两行。我如何可以单独为参考我在循环创建的每个DropDownList的?我使用Asp.Net Web应用程序用C#

For each value I check, a new row is created with the value of the checked checkbox, a dropdownlist and 2 more cells. Now I want for each dropdownlist that I create to make something.So if I choose 1 for the first row something will appear and if I choose 1 for another row something else will appear. I made a selectedindexchanged but if I choose 1 from the first row it will show up for both rows. How can I refer separately for each dropdownlist that I create in the loop?? I'm using Asp.Net web application with c#

推荐答案

修改您的code如下。

Modify your code as below.

protected void Page_Load(object sender, EventArgs e)
{        
    LoadData();
}   

定义LoadData函数如下

Define LoadData function as below

 private void LoadData()
 {
     foreach (ListItem item in check.Items)
     {
         if (item.Selected)
         {

             DropDownList drop = new DropDownList();

             drop.Items.Add("1");
             drop.Items.Add("2");

             drop.DataValueField = "1";
             drop.DataValueField = "2";
             drop.AutoPostBack = true;

             // assign id property to dropdown.

             drop.ID = "ddl_" + item.value.ToString();

             // add event handled for dynamically generated dropdown      
             drop.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);



             TableRow row = new TableRow();
             TableCell celula = new TableCell();

             // assing ID property to cell.

             celula.ID = "celula_" + item.value.ToString();
             celula.Style.Add("width", "200px");
             celula.Style.Add("background-color", "red");

             celula.RowSpan = 2;
             celula.Text = "item " + item.Value.ToString();
             TableCell celula1 = new TableCell();
             celula1.Style.Add("background-color", "green");
             celula1.Style.Add("width", "200px");

             celula1.RowSpan = 2;

             TableCell celula2 = new TableCell();
             celula2.Style.Add("width", "200px");
             celula2.Style.Add("background-color", "blue");
             celula2.Text = "unu";
             TableRow row1 = new TableRow();
             TableCell cel = new TableCell();
             cel.Text = "lalala";
             cel.Style.Add("background-color", "brown");




             celula1.Controls.Add(drop);
             row.Cells.Add(celula);
             row.Cells.Add(celula1);
             row.Cells.Add(celula2);
             row1.Cells.Add(cel);
             this.tabel.Rows.Add(row);
             this.tabel.Rows.Add(row1);
         }
     }

    }

处理事件,如下

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList dd = (DropDownList)sender;

    // you can get dropdown from table row over here.
    // process with it as per your requirement.

    string strdropdownID = ((System.Web.UI.Control)(dd)).ID;

    string id = strdropdownID.Split('_')[1].ToString();
    string tblcID = "celula_" + id.ToString();
    TableCell celula = (TableCell)tabel.FindControl(tblcID);
    celula.RowSpan = Convert.ToInt32(dd.SelectedValue);
}

希望这将适用于你......快乐编码...

Hope this will works for you...happy coding...

这篇关于从一个DropDownList分配给TD表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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