首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Visual Web Developer在表格单元格中放置下拉菜单

Visual Web Developer在表格单元格中放置下拉菜单
EN

Stack Overflow用户
提问于 2012-06-08 23:54:45
回答 1查看 244关注 0票数 0

我已经创建了一个显示表格的网页。表格中的大多数单元格已经填充,但其中一些需要用户输入。我想在其中一些表格单元格中放置下拉菜单,这样单元格将填充从菜单中选择的内容,下拉列表将消失。

我已经让它在一个单元格中工作。然而,当我试图让它在第二个单元中工作时,一切都崩溃了。为了找出这个问题,我花了太多的时间,我甚至不确定我现在是如何让第一个问题生效的。:-)

相关代码如下:

代码语言:javascript
复制
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;
    }
}

下面是表中的相关代码片段:

代码语言:javascript
复制
<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。而且,我一次只能显示一个下拉菜单。

任何建议都是值得感谢的。

致以问候。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-02 10:30:07

下面的代码解决了这个问题:

代码语言:javascript
复制
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);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10952172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档