2013-09-05 67 views
0

我一直在試圖獲取動態創建的下拉列表(Page_Load期間)的ID(位於表中),然後在這些下拉列表中找到所選值並存儲這些值上按鈕click.This表或GridView的是如何分配的ID(在Page_Load事件)我如何獲取動態創建的Web控件的ID

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

是否有一種方式來獲得真正的ID和然後在他們身上工作? 我新來的asp.net,我很抱歉,如果它聽起來noob ish .. 在此先感謝。

+1

爲什麼不使用他們的'SelectedIndexChanged'事件,然後通過'((DropDownList)sender).Id'獲取ID。如果您不將'AutoPostBack'設置爲'true',您甚至可以使用此事件。它會觸發每個更改的下拉菜單。 –

+0

您好蒂姆感謝您的答覆隊友。但我怎麼能使用'SelectedIndexChanged',當我無法獲得id使用它。下拉菜單是在頁面載入中創建的,並且同時給出ID。我需要在按鈕單擊時找到所選項目,以便我可以保存該數據。 – rawatdeepesh

+0

@awatdeepesh:您必須以編程方式添加'SelectedIndexChanged'事件處理程序:'dynDll.SelectedIndexChanged + = OnDropDownListSelectedIndexChanged;'其中'OnDropDownListSelectedIndexChanged'是您的類中處理它的方法。 –

回答

0

實測成功,發現下拉列表和在表中選擇從下拉的值被它是在作爲well.Here是代碼表改變。每次保存所選擇的值..

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

}

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 
} 
+0

感謝您的回覆阿南德。但是我還沒有準備好gridview。我需要從下拉列表中找到所選項目(我已經在Page_load中顯示了下拉列表),並將這些選定的值用作另一個事件中的表格或GridView標題。 – rawatdeepesh

0

您提到動態創建的下拉列表位於表中。我認爲Anand指的是那張表格,而不是你希望用下拉列表的值填充的GridView。因此,您可以嘗試循環查看錶格的行並獲取下拉列表ID。

相關問題