【问题标题】:Display DropDownlist when click on specific cell asp.net table单击特定单元格 asp.net 表时显示 DropDownlist
【发布时间】:2017-04-12 11:53:57
【问题描述】:

我试图弄清楚如何在单击位于该单元格上的“显示 DropDownlist”按钮后立即在表格中的特定单元格上显示 dropDownList。

这是后面的代码,现在它在每一行的最后一个单元格显示 dropDownList。我想让它仅在单击按钮时出现。

while (rdr.Read())
                {
                    TableRow tRow = new TableRow();
                    myTable.Rows.Add(tRow);
                    for (int i = 0; i <= 4; i++)
                    {
                        // Create a new cell and add it to the row.
                        TableCell tCell = new TableCell();
                        if (i == 4)
                        { 
                            tCell.Controls.Add(SM_List());  //Adding the dropdownlist             
                            tRow.Cells.Add(tCell);
                            continue;
                        }

                            tCell.Text = rdr.GetString(i);
                            tCell.Attributes.Add("onmouseover", "this.style.cursor = 'pointer'; this.style.backgroundImage = ''; ");
                            tCell.Attributes.Add("onClick", "getData()");
                            tRow.Cells.Add(tCell);

                    }
                    /* iterate once per row */
                }

我想添加这段代码,所以它首先是一个按钮,而不是一个下拉列表:

Button bt = new Button();
bt.Text = "Switch";
bt.Click += new EventHandler(DropDownList_Show);
tCell.Controls.Add(bt);

但我不确定如何在按钮所在的确切单元格中显示 DropDownList。而且我想在下拉列表中选择值时执行一些操作。 能否请您帮忙,我感到有点失落。

【问题讨论】:

  • 是否要创建动态按钮控件
  • 如果在客户端完成会很容易。只需将此作为参数传递,然后使用 .html("dropdown html") 将内容放在该单元格中
  • 如果您使用 GridView 或 DataGrid 并使用 ItemTemplate 在其中显示控件,例如按钮和下拉列表,则处理起来会更容易。

标签: c# asp.net


【解决方案1】:

您可以通过在 ASP.NET 中使用 GridView 轻松解决此问题。 假设 GridView 在 ASPX 页面中声明如下。

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" />
        <asp:BoundField DataField="Token" />
        <asp:BoundField DataField="Secret" />
        <asp:ButtonField CommandName="ShowDropDown" Text="Show" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:DropDownList ID="dropDownList" runat="server" Visible="false">
                    <asp:ListItem Text="Valid" Value ="1"></asp:ListItem>
                    <asp:ListItem Text="Invalie" Value ="2"></asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

以下是填充 GridView 的代码中的方法。

private void BindGridView()
{

    var tokens = new List<AccessToken>();
    using (var conn = new SqlConnection("Server=somedbserver;Database=somedatabase;User Id=someuser;Password=somepassword;"))
    {
        using (var command = new SqlCommand())
        {
            command.Connection = conn;
            command.CommandText = "SELECT Id, Token, Secret FROM Tokens";

            command.CommandType = System.Data.CommandType.Text;
            conn.Open();

            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var token = new AccessToken();
                    token.Id = reader.GetInt32(0);
                    token.Token = reader.GetString(1);
                    token.Secret = reader.GetString(2);
                    tokens.Add(token);
                }
            }
        }
    }
    GridView1.DataSource = tokens;
    GridView1.DataBind();
}

我在 Page_Load 中调用了这个方法。

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        BindGridView();
    }
}

以下是 GridView 的 RowCommand 事件的事件处理程序,它将在单击的按钮旁边的列中显示下拉列表。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName == "ShowDropDown")
    {
        var row = GridView1.Rows[Convert.ToInt32(e.CommandArgument)];

        //Using Cell[4] coz the Dropdownlist is in 5th column of the row.
        //You need to replace 4 with appropriate column index here.
        //Also replace "dropDownList" with the ID assigned to the dropdown list in ASPX.
        var ddl = (DropDownList)row.Cells[4].FindControl("dropDownList");

        if(ddl != null)
        {
            ddl.Visible = true;
        }
    }
}

如果您遵循此方法,您将能够解决您的问题。

【讨论】:

    猜你喜欢
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多