1

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

所以我尝试了以下方法:

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

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

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

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

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

4

1 回答 1

0

在您的中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();
}

希望有帮助。

于 2010-05-05T04:56:00.280 回答