2012-06-08 36 views
0

我創建了一個顯示錶格的網頁。表中的大部分單元格都已填充,但其中一些單元需要用戶輸入。我想在這些表格單元格中放置下拉菜單,這樣單元格將填充選擇菜單,下拉列表將消失。Visual Web Developer在桌子單元格中放置Drowdown菜單

我已經得到了這個在單個單元格中工作。但是,當我試圖讓它在第二個單元中工作時,一切都破裂了。我曾試圖發現這個問題,我甚至不確定我是否記得現在第一個人的工作。 :-)

下面是相關代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 

    // Create a DropDownList control. 
    DropDownList DropList = new DropDownList(); 

    // Set the properties for the DropDownList control. 
    DropList.ID = "TrendList"; 
    DropList.AutoPostBack = true; 

    // Manually register the event-handling method for the 
    // SelectedIndexChanged event. 
    DropList.SelectedIndexChanged += new EventHandler(this.Selection_Change); 

    // Because the DropDownList control is created dynamically each 
    // time the page is loaded, the data must be bound to the 
    // control each time the page is refreshed. 

    // Specify the data source and field names for the Text and 
    // Value properties of the items (ListItem objects) in the 
    // DropDownList control. 
    DropList.DataSource = CreateDataSource(); 
    DropList.DataTextField = "ColorTextField"; 
    DropList.DataValueField = "ColorValueField"; 

    // Bind the data to the control. 
    DropList.DataBind(); 

    // Set the default selected item when the page is first loaded. 
    if (!IsPostBack) 
    { 
     DropList.SelectedIndex = 0; 
    } 

    // Add the DropDownList control to the Controls collection of 
    // the PlaceHolder control. 
    p1.Controls.Add(DropList); 
    p2.Controls.Add(DropList); 
} 

ICollection CreateDataSource() 
{ 

    // Create a table to store data for the DropDownList control. 
    DataTable dt = new DataTable(); 

    // Define the columns of the table. 
    dt.Columns.Add(new DataColumn("ColorTextField", typeof(String))); 
    dt.Columns.Add(new DataColumn("ColorValueField", typeof(String))); 

    // Populate the table with sample values. 
    dt.Rows.Add(CreateRow("GreenUp", "GreenUp", dt)); 
    dt.Rows.Add(CreateRow("GreenFlat", "GreenFlat", dt)); 
    dt.Rows.Add(CreateRow("GreenDown", "GreenDown", dt)); 
    dt.Rows.Add(CreateRow("YellowUp", "YellowUp", dt)); 
    dt.Rows.Add(CreateRow("YellowFlat", "YellowFlat", dt)); 
    dt.Rows.Add(CreateRow("YellowDown", "YellowDown", dt)); 
    dt.Rows.Add(CreateRow("RedUp", "RedUp", dt)); 
    dt.Rows.Add(CreateRow("RedFlat", "RedFlat", dt)); 
    dt.Rows.Add(CreateRow("RedDown", "RedDown", dt)); 
    dt.Rows.Add(CreateRow("ClearUp", "ClearUp", dt)); 
    dt.Rows.Add(CreateRow("ClearFlat", "ClearFlat", dt)); 
    dt.Rows.Add(CreateRow("ClearDown", "ClearDown", dt)); 

    // Create a DataView from the DataTable to act as the data source 
    // for the DropDownList control. 
    DataView dv = new DataView(dt); 
    return dv; 

} 

DataRow CreateRow(String Text, String Value, DataTable dt) 
{ 

    // Create a DataRow using the DataTable defined in the 
    // CreateDataSource method. 
    DataRow dr = dt.NewRow(); 

    // This DataRow contains the ColorTextField and ColorValueField 
    // fields, as defined in the CreateDataSource method. Set the 
    // fields with the appropriate value. Remember that column 0 
    // is defined as ColorTextField, and column 1 is defined as 
    // ColorValueField. 
    dr[0] = Text; 
    dr[1] = Value; 

    return dr; 
} 

void Selection_Change(Object sender, EventArgs e) 
{ 
    // Retrieve the DropDownList control from the Controls 
    // collection of the PlaceHolder control. 
    DropDownList DropList1 = (DropDownList)p1.FindControl("TrendList"); 
    DropDownList DropList2 = (DropDownList)p2.FindControl("TrendList"); 
    switch (sender.ToString()) 
    { 
     case "p1": 
      s1.InnerHtml = DropList1.SelectedItem.Value; 
      break; 
     case "p2": 
      s2.InnerHtml = DropList2.SelectedItem.Value; 
      break; 
    } 
} 

,這裏是從表中的相關片段:

<td><span id="s1" runat="server"><asp:PlaceHolder ID="p1" runat="server"></asp:PlaceHolder></span> 
<td><span id="s2" runat="server"><asp:PlaceHolder ID="p2" runat="server"></asp:PlaceHolder></span> 

現在我認識到開關控制是完全錯誤的,因爲發送者不代表來電者的身份。但我需要一些方法來區分哪個下拉菜單是調用者,所以我知道要替換哪個HTML。另外,我一次只能顯示一個下拉菜單。

任何意見表示讚賞。

問候。

回答

0

此代碼解決了這個問題:

public void EditTable() 
{ 
    ICollection trends = CreateDataSource(); 
    for (int x = 1; x <= 27; x++) 
    { 
     DropDownList ddl = new DropDownList(); 
     string index = x.ToString(); 
     ddl.ID = "TrendList" + index; 
     ddl.AutoPostBack = true; 
     ddl.SelectedIndexChanged += new EventHandler(this.Selection_Change); 
     ddl.DataSource = trends; 
     ddl.DataTextField = "TrendTextField"; 
     ddl.DataValueField = "TrendValueField"; 
     ddl.DataBind(); 
     if (!IsPostBack) 
     { 
      ddl.SelectedIndex = 0; 
     } 
     HtmlGenericControl span = (HtmlGenericControl)form1.FindControl("s" + index); 
     PlaceHolder placeHolder = (PlaceHolder)span.FindControl("p" + index); 
     if (placeHolder != null) 
     { 
      placeHolder.Controls.Add(ddl); 
     } 
    } 
}