简体   繁体   English

如何在一个ComboBox中使用两个DataTextField和DataValueField

[英]How to use two DataTextField and DataValueField in one ComboBox

Using: C# | 使用:C#| asp.net asp.net

What I want to do is take one ComboBox and one Button. 我想做的就是拿一个ComboBox和一个Button。 I want the ComboBox to read two different datasets for DataValueField and populate the drop down list with whichever DataValueField isn't empty. 我希望ComboBox读取两个不同的DataValueField数据集,并使用不为空的DataValueField填充下拉列表。

The two datasets will have different DataTextFields so I need the ComboBox to also check and populate the ComboBox with the DataTextField that isn't empty. 这两个数据集将具有不同的DataTextField,因此我需要ComboBox还要使用不为空的DataTextField检查并填充ComboBox。 Is this possible? 这可能吗?

So far I have the ComboBox populating the DataValueField for one of the datasets but not both. 到目前为止,我有ComboBox填充了其中一个数据集的DataValueField,但没有两个都填充。 I'm not sure if I should use OnSelectedIndexChanged or something different or if this is even possible? 我不确定是否应该使用OnSelectedIndexChanged或其他名称,或者甚至可以使用? Any advice will be welcomed! 任何建议都将受到欢迎! Thanks. 谢谢。

.aspx .aspx

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

.aspx.cs .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);
                }
            }
        }

You can use a model class and can bind your combobox with that. 您可以使用模型类,并可以将其与组合框绑定。 Assign value to the model according to the availability of data in the two dataset.. This way is easy and won't lead to much complications. 根据两个数据集中数据的可用性为模型分配值。这种方法很简单,不会带来很多麻烦。 Simple way and code will also be manageable. 简单的方法和代码也将易于管理。

Hope fully it will do the need full. 完全希望它将满足全部需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DropDownList的DataTextField和DataValueField属性 - DataTextField and DataValueField properties of DropDownList CheckboxList忽略DataValueField和DataTextField - CheckboxList ignores DataValueField and DataTextField DropDownList:DataTextField,DataValueField ... DataEnabledField? - DropDownList: DataTextField, DataValueField… DataEnabledField? 从SQL(C#)加载到ComboBox时无法设置Datatextfield和dataValuefield - Unable To Set Datatextfield and dataValuefield When Loading To ComboBox From SQL (C#) 使用DataTextField和DataValueField从另一个列表复制下拉列表 - Copy dropdownlist from another list with DataTextField and DataValueField 使用除dataValueField / dataTextField之外的第三个值将ViewModel绑定到DropDownListFor - Bind a ViewModel to a DropDownListFor with a third value besides dataValueField/dataTextField 虽然DataTextField已检索,但DataValueField未检索值 - DataValueField didn't retrieve value ,although DataTextField retrieved 使用 linq 查询将值存储在下拉列表的 DataValueField 和 DataTextField 中 - Storing values in the DataValueField and DataTextField of a dropdownlist using a linq query DataValueField 中有两个值? - two values in DataValueField? 我如何能够在单选按钮列表的DataTextField中给出两个字符串? - How i am able to give two strings in DataTextField of radiobuttonlist?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM