网格视图保管箱

发布于 2024-08-31 00:18:36 字数 450 浏览 4 评论 0原文

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

所以我尝试了以下操作:

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

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

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

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

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

I'm trying to use a GridView to display a list of components in ASP.NET. I'm trying to make it editable at the same time. One of the columns is a string that should be selected out of a list when the user edits the row.

So I've tried the following:

  1. Convert the BoundField row to an ItemTemplate
  2. Add a dropbox to the template window in the gridview
  3. bound the selecteditem to the string

At this point, I get an error because the list items haven't been set up in the dropbox. So I guess the two things I'm wondering are:

  1. How do I assign the items in the dropbox to a dynamically created list of options?
  2. How do I make the dropbox only appear when the row is being edited?

Ok so I've discovered the "EditItemTemplate" field in visual studio, that answers #2.

And now I've discovered that the dropbox has a datasource field which can be linked to a property in the data object, and that holds the options list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2024-09-07 00:18:36

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

希望有帮助。

In your DropDownList you can assign a OnDataBinding event and then us the event to fill your DropDownList with custom data.

Example:

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

Then in your code behind implement the 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();
}

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文