可视化 Web 开发人员将向下浏览菜单放在表格单元格中



我创建了一个显示表格的网页。 表格的大多数单元格都已填充,但其中一些需要用户输入。 我想将下拉菜单放在其中一些表格单元格中,以便该单元格将填充菜单中的选择,并且下拉列表将消失。

我已经让它在一个单元格中工作。 然而,当我试图让它在第二个牢房中工作时,一切都坏了。 我已经四处走动,试图找到问题,我什至不确定我现在如何让第一个工作。:-)

以下是相关代码:

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>

现在我意识到开关控制都是错误的,因为发送方不代表调用方的 ID。 但是我需要一些方法来区分哪个下拉菜单是调用方,以便我知道要替换哪个 HTML。 另外,我一次只能显示一个下拉菜单。

任何建议不胜感激。

问候。

这段代码解决了这个问题:

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

相关内容

  • 没有找到相关文章

最新更新