【问题标题】:How to use two DataTextField and DataValueField in one ComboBox如何在一个 ComboBox 中使用两个 DataTextField 和 DataValueField
【发布时间】:2013-09-19 16:23:48
【问题描述】:

使用:C# | asp.net

我想要做的是拿一个 ComboBox 和一个 Button。我希望 ComboBox 为 DataValueField 读取两个不同的数据集,并使用不为空的 DataValueField 填充下拉列表。

这两个数据集将具有不同的 DataTextField,因此我需要 ComboBox 来检查并使用不为空的 DataTextField 填充 ComboBox。这可能吗?

到目前为止,我有 ComboBox 为其中一个数据集填充 DataValueField,但不是同时填充这两个数据集。我不确定我是否应该使用 OnSelectedIndexChanged 或不同的东西,或者这是否可能?任何建议都将受到欢迎!谢谢。

.aspx

<asp:ComboBox ID="ComboBox1" runat="server" AutoPostBack="True"
        DataSourceID="SQLserver" 
          DataTextField="Text" DataValueField="Value" MaxLength="0" 
          style="display: inline;">
</asp:ComboBox>

.aspx.cs

 protected void Button1_Click (object sender, System.EventArgs e)
          {
            if (ComboBox1.SelectedIndex > -1)
           {
                bool img = true;
                string Path = "URL" + ComboBox1.SelectedItem.Value;

                if (img == true)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + Path + "');", true);
                }
            }
        }

【问题讨论】:

  • 为什么不将两个数据集连接起来,然后绑定到ComboBox?
  • 我不希望选择显示数据集是否为空。我希望组合框填充两者之一,而不是两者。 @加里森
  • 但是如果其中一个数据集是空的,您可以使用代码隐藏来检查它,然后绑定另一个。您不能将两个数据集添加到一个组合框,但您可以通过编程方式确定要绑定到哪个数据集。

标签: c# asp.net .net button combobox


【解决方案1】:

您可以使用模型类并将您的组合框与它绑定。根据两个数据集中数据的可用性为模型赋值。这种方式很简单,不会导致 很多并发症。简单的方式和代码也将易于管理。

完全希望它能满足需求。

【讨论】:

    猜你喜欢
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多