【问题标题】:ASP.NET gridview - how to add dynamically populated dropdown to dynamically bound gridviewASP.NET gridview - 如何将动态填充的下拉列表添加到动态绑定的 gridview
【发布时间】:2017-06-30 01:36:40
【问题描述】:

我已经四处寻找有关我的问题的答案,但我没有找到任何结论性的答案。我想要以下内容: 我有一个带有 GridView 的 asp.net 表单,它没有绑定到数据源,因此没有预定义的列。我使用来自 SQL Server 的数据动态填充 gridview:

gvComponentLocks.DataSource = getComponentsAndLocks(worksPermitID);
//Note getComponentsAndLocks encapsulates the database query and returns a DataTable
gvComponentLocks.DataBind();

现在我想在 GridView 的一个特定列中有一个 DropDownList。此 DropDownList 应动态填充值(这里我认为 ...Item.Add 是适当的方法)。 我最大的问题是如何在单元格中创建 DropDownLists 而不能在网页标记中将它们静态定义为 asp:TemplateField?

回答我的问题的另一种方法是如何使用来自数据源的数据动态填充静态定义的 GridView(具有静态定义的 DropDownList 控件) - 无需将 GridView 静态绑定到数据源。

【问题讨论】:

  • 其实很简单。在您的 html 中尝试数据源控件并将其绑定到那里。只需将 datasourceId 提供给您的下拉列表并完成

标签: c# asp.net gridview drop-down-menu dynamic-programming


【解决方案1】:

假设 GridView(带有静态定义的 DropDownList 控件)

 <asp:DropDownList ID="DropDownList1" runat="server">
                                                </asp:DropDownList>

在侧面

  protected void gvComponentLocks_RowDataBound(object sender, GridViewRowEventArgs e)
   if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {

做一些类似...

DropDownList DropDownList1 = (DropDownList)e.Row.FindControl("DropDownList1");
 DropDownList1.DataSource=SomeDropDownList1ItemCollection
 DropDownList1.Bind();

或者从 ListItemCollection 返回,最好...

 gvComponentLocks.Items.AddRange(LoadList().Cast<ListItem>().ToArray());

在哪里

public ListItemCollection LoadList()
            {

    ListItemCollection Items = new ListItemCollection();
                Items.Add(new ListItem("Choose from list...", ""));
                Items.Add(new ListItem("Text","Value")

动态下拉列表: 在网格的模板中放置一个 PlaceHolder 控件。在后面的代码中创建一个具有适当 id 的 DropDownList 控件并添加到 PlaceHolder 控件中。

 DropDownList DropDownList1= new DropDownList();
DropDownList1.ID=... 
etc
YourPlaceHolderControl.Controls.Add(DropDownList1)

您必须在回发时重新构建这个动态 DropDownList 并重新填充它。

【讨论】:

  • 谢谢 没什么,我仍然不知道如何将数据从服务器绑定到这个静态定义的 GV。你对我有什么提示吗?我昨天尝试了这种“静态布局”方法,但不知何故未能在设计时定义的 GV 中显示数据。
【解决方案2】:

您可以在GridView的RowDataBound事件中动态创建DropDownList。

protected void gvComponentLocks_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //create a new dropdownlsit
        DropDownList ddl = new DropDownList();

        //bind the source and define the values
        ddl.DataSource = getComponentsAndLocks(worksPermitID);
        ddl.DataTextField = "columnA";
        ddl.DataValueField = "columnB";
        ddl.DataBind();

        //add the dropdownlist to the gridview in column 1
        e.Row.Cells[1].Controls.Add(ddl);
    }
}

您还必须做的唯一事情是将 GridView 的 DataBBinding 放在 IsPostBack 检查之外。否则,您将在 PostBack 后丢失 DropDowns。

【讨论】:

    猜你喜欢
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2010-09-19
    相关资源
    最近更新 更多