0

我一直在尝试获取动态创建的下拉列表的 id(在 Page_Load 期间),它们在一个表中,然后在这些下拉列表中找到选定的值,并将这些值存储在一个表或 GridView 上单击按钮。这就是我分配 id 的方式(在 Page_Load 事件中)

         drd.ID = "dbaseid" + rowctr; 
         drd1id[rowctr]=drd1.ID;

rowctr 是为每个下拉列表分配唯一 id 的索引变量。

我如何从 Page_Load 获取 id。我尝试将 id 存储在一个数组中,然后使用会话变量:

         drdid[rowctr]=drd.ID;
         drd1id[rowctr]=drd1.ID;
         Session["drditem"]=drditem;
         Session["drd1item"]=drd1item;

然后尝试在 buttonclick 事件函数中获取 id:

         drdid=(string[])Session["drdid"];           
         drd1id=(string[])Session["drd1id"];
         string[] a =new string [50];
         for(int i =0;i<noodropdowns;i++)
         {
           a=drd1id[i];
           a.selectedindex//doesnt work !!
         }

有没有办法获得真实的身份证然后处理它们?我是 asp.net 的新手,我很抱歉,如果这听起来很菜鸟 .. 在此先感谢。

4

3 回答 3

1

嘿,如果您试图遍历 GridView 中的所有下拉列表并获取下拉列表?

//Add the drop down as following in page load
drd.ID = "dbaseid"; //do not add dynamic id other wise you will not able to find it. 
                    //It client id would be different based upon its position in DOM


//loop over gridview row and get the control as following

foreach (GridViewRow row in grid.Rows)
{
   var ddl = row.FindControl("dbaseid") as DropDown;  
  //do what ever with the drop down 
}
于 2013-09-05T11:37:45.683 回答
0

您提到动态创建的下拉列表位于表格中。我认为 Anand 指的是该表,而不是您希望使用下拉列表的值填充的 GridView。因此,您可以尝试遍历表的行并获取下拉列表 ID。

于 2013-09-05T13:24:20.487 回答
0

找到成功,找到下拉列表并将所选值保存在表格中。每次选择下拉列表中的值时,表格中的值也会更改。这是代码..

     using System.Collections.Generic;
     using System.Linq;
     using System.Web;
     using System.Web.UI;
     using System.Web.UI.WebControls;
     using System.IO;
     using System.Data;
     using System.Data.SqlClient;
     using System.Data.OleDb;
     using System.Collections;
     using System.Web.Security;
     using System.Text;
     using System.Configuration;
     using System.Web.SessionState;
     using System.Windows;

     public partial class Default2 : System.Web.UI.Page
     {
     Table tblRecipients = new Table();
     DropDownList drd = new DropDownList();
     DropDownList drd1 = new DropDownList();
     protected void Page_Init(object sender, EventArgs e)
{
    DataTable dtt = (DataTable)Session["griddata"];
    int n = dtt.Columns.Count;
    string[] drd1item = new string[n];
    string[] excel = new string[n];
    for (int i = 0; i < dtt.Columns.Count; i++)
    {
        excel[i] = dtt.Columns[i].ColumnName;
    }
    Session["exceldata"] = excel;

    ////saving sql database column names in an array variable
    DataTable dtt1 = (DataTable)Session["dbasecolumns"];
    int l = dtt1.Columns.Count;
    string[] drditem = new string[l];
    string[] sqlcolumn = new string[l];
    for (int j = 0; j < dtt1.Columns.Count; j++)
    {
        sqlcolumn[j] = dtt1.Columns[j].ColumnName;
    }
    Session["sqlcolumn"] = sqlcolumn;
    Session["l"] = l;

    //Table Creation
    Table mytable = new Table();
    mytable.Visible = true;
    mytable.GridLines = GridLines.Both;
    TableHeaderRow th = new TableHeaderRow();
    TableHeaderCell thc = new TableHeaderCell();
    TableHeaderCell thc1 = new TableHeaderCell();

    mytable.Rows.Add(th);
    Label lbl1 = new Label();
    Label lbl2 = new Label();

    lbl1.Text = "Database";
    lbl2.Text = "Excel";

    thc.Controls.Add(lbl1);
    thc1.Controls.Add(lbl2);
    th.Cells.Add(thc);
    th.Cells.Add(thc1);


    for (int rowctr = 0; rowctr < sqlcolumn.Length; rowctr++)
    {
        TableCell mycell = new TableCell();
        TableCell mycell1 = new TableCell();

        for (int cellctr = 0; cellctr < 1; cellctr++)
        {
            //dropdown with database columns
            DropDownList drd = new DropDownList();
            drd.Items.Insert(0, new ListItem("--Select--", "0"));
            drd.ID = "dbaseid" + rowctr;
            for (int i = 0; i < sqlcolumn.Length; i++)
            {
                drd.Items.Add(sqlcolumn[i]);
                drditem[i] = sqlcolumn[i];
            }
            // drd.SelectedIndexChanged+=new EventHandler(drd1_SelectedIndexChanged);


            //dropdown with excel columns
            DropDownList drd1 = new DropDownList();

            drd1.ID = "excelid" + rowctr;

            for (int j = 0; j < excel.Length; j++)
            {
                drd1.Items.Add(excel[j]);
                drd1item[j] = excel[j];
            }
            // drd1.SelectedIndexChanged +=new EventHandler (drd1_SelectedIndexChanged);
            //session variable to store dropdown elements in an array




            //Table cells and rows addition 
            TableRow myrow = new TableRow();
            mycell.Controls.Add(drd);
            mycell1.Controls.Add(drd1);
            myrow.Cells.Add(mycell);
            myrow.Cells.Add(mycell1);
            mytable.Rows.Add(myrow);
            mytable.BorderStyle = BorderStyle.Solid;
        }
    }

    DynamicControlsHolder.Controls.Add(mytable);
}

     protected void Button1_Click(object sender, EventArgs e)
{
    Table mytable = new Table();
    mytable.GridLines = GridLines.Both;
    string s;
    foreach (Control ctl in DynamicControlsHolder.Controls)
    {
        if (ctl is Table)
        {
            Table tblnew = ctl as Table;
            {
                foreach (Control ctrl in tblnew.Controls)
                {
                    if (ctrl is TableRow)
                    {
                        TableRow trow = new TableRow();
                        TableRow tblrow = ctrl as TableRow;
                        {

                            foreach (Control cntrl in tblrow.Controls)
                            {
                                if (cntrl is TableCell)
                                {
                                    TableCell tcell = new TableCell();
                                    TableCell tblcell = cntrl as TableCell;
                                    {
                                        foreach (Control cntrol in tblcell.Controls)
                                        {
                                            if (cntrol is DropDownList)
                                            {
                                                DropDownList myddr = cntrol as DropDownList;
                                                if (cntrol != null)
                                                {




                                                    s = myddr.SelectedItem.Text;
                                                    tcell.Text = s;


                                                }
                                            }

                                        }

                                    }
                                    trow.Cells.Add(tcell);
                                }
                            }

                        }

                        mytable.Rows.Add(trow);  
                    }
                }
            }
        }
    }
    DynamicControlsHolder.Controls.Add(mytable);
}

}

于 2013-09-13T10:52:44.517 回答