【问题标题】:gridview dropbox网格视图保管箱
【发布时间】:2010-05-04 22:14:28
【问题描述】:

我正在尝试使用 GridView 来显示 ASP.NET 中的组件列表。我正在尝试使其同时可编辑。其中一列是用户编辑行时应从列表中选择的字符串。

所以我尝试了以下方法:

  1. 将 BoundField 行转换为 ItemTemplate
  2. 在gridview的模板窗口中添加一个dropbox
  3. 将选定项绑定到字符串

此时,我收到错误消息,因为尚未在 Dropbox 中设置列​​表项。所以我想我想知道的两件事是:

  1. 如何将 Dropbox 中的项目分配给动态创建的选项列表?
  2. 如何让 Dropbox 仅在编辑行时出现?

好的,所以我在 Visual Studio 中发现了“EditItemTemplate”字段,它回答了 #2。

现在我发现 Dropbox 有一个数据源字段,它可以链接到数据对象中的属性,并且保存选项列表。

【问题讨论】:

  • 向我们展示您的 GridView 代码和后面的代码。
  • 所以我对数据绑定和 ASP 还很陌生...我真的想了解如何在设计器中构建这些报告,而不是修复错误。我的代码隐藏实际上只有我绑定的数据(工作)。标记生成的作品。我从概念上不知道如何在 Visual Studio 设计器中绑定 Dropbox listitem 选项。

标签: c# asp.net gridview dropbox


【解决方案1】:

在您的DropDownList 中,您可以分配一个OnDataBinding 事件,然后我们使用该事件来填充您的DropDownList 自定义数据。

例子:

<Columns>
    <asp:TemplateField>
        <EditItemTemplate>
            <asp:DropDownList ID="yourDropDownList" runat="server"
                DataTextField="YourTextFieldName" DataValueField="YourValueFieldName"
                OnDataBinding="yourDropDownList_DataBinding"></asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField>
</Columns>

然后在你的代码后面实现 OnDataBinding:

protected void yourDropDownList_DataBinding(object sender, System.EventArgs e) 
{ 
    DropDownList ddl = (DropDownList)(sender);
    // GetMyDropDownListData should return cached data so your not hitting your DB
    // each time. You can customize the data for each row here. Use the Eval command
    // to access the current rows databound values.
    ddl.DataSource = GetMyDropDownListData();
    ddl.DataBind();  // Now all the options will be loaded

    // Set the current field's selected value
    ddl.SelectedValue = Eval("YourSelectedValueFieldName").ToString();
}

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-28
    • 2018-05-18
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多