简体   繁体   中英

How to use two DataTextField and DataValueField in one ComboBox

Using: C# | asp.net

What I want to do is take one ComboBox and one Button. I want the ComboBox to read two different datasets for DataValueField and populate the drop down list with whichever DataValueField isn't empty.

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. Is this possible?

So far I have the ComboBox populating the DataValueField for one of the datasets but not both. I'm not sure if I should use OnSelectedIndexChanged or something different or if this is even possible? Any advice will be welcomed! Thanks.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM