How to add a value using comboBoxName.Items.Insert

  • Thread starter Ruchika Chadha via .NET 247
  • Start date
R

Ruchika Chadha via .NET 247

(Type your message here)

Hi guys,
I am new to ASP .NET. I have used the following code to add values from a database to a combobox:-

Dim index As Integer = 0
For Each dRows In dSet.Tables(tableName).Rows
comboBoxName.Items.Insert(index, dRows.Item(columnName))
index += 1
Next

The above gives me the following HTML output:-
<select name="cbox" id="cbo_cbo" style="width:152px;">
<option value="ABC">ABC</option>
<option value="DEFG">DEFG</option>
</select>

Is there a way to have different values for - "Text" and "Value" in the combobox. For example - how can i produce
<select name="cbox" id="cbo_cbo" style="width:152px;">
<option value="1">ABC</option>
<option value="2">DEFG</option>
</select>

Thanks in advance!!!
cheers
Ruchika (Richi)
 
C

Curt_C [MVP]

few things.
1) look at the asp:DropDownList
2) use a dataset and bind to it


--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com


Ruchika Chadha via .NET 247 said:
(Type your message here)

Hi guys,
I am new to ASP .NET. I have used the following code to add values from a database to a combobox:-

Dim index As Integer = 0
For Each dRows In dSet.Tables(tableName).Rows
comboBoxName.Items.Insert(index, dRows.Item(columnName))
index += 1
Next

The above gives me the following HTML output:-
<select name="cbox" id="cbo_cbo" style="width:152px;">
<option value="ABC">ABC</option>
<option value="DEFG">DEFG</option>
</select>

Is there a way to have different values for - "Text" and "Value" in the
combobox. For example - how can i produce
 
D

Dale

When you use the DropDownList, create a ListItem and add the ListItem:

ListItem li = new ListItem("myText", "myValue");
MyDDL.Items.Add(li);

or

MyDDL.Items.Add(new ListItem("myText", "myValue"));

The C# code should be easily translatable to VB.NET.

Dale Preston
MCAD, MCSE, MCDBA
Curt_C said:
few things.
1) look at the asp:DropDownList
2) use a dataset and bind to it


--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com


Ruchika Chadha via .NET 247 said:
(Type your message here)

Hi guys,
I am new to ASP .NET. I have used the following code to add values from
a
database to a combobox:-
Dim index As Integer = 0
For Each dRows In dSet.Tables(tableName).Rows
comboBoxName.Items.Insert(index, dRows.Item(columnName))
index += 1
Next

The above gives me the following HTML output:-
<select name="cbox" id="cbo_cbo" style="width:152px;">
<option value="ABC">ABC</option>
<option value="DEFG">DEFG</option>
</select>

Is there a way to have different values for - "Text" and "Value" in the
combobox. For example - how can i produce
 
Joined
Aug 20, 2008
Messages
1
Reaction score
0
One more thing to remember

In .NET 2003, please also note that you need to turn on AppendDataBoundItems in the properties or this will not work. This drove me nuts for two days. Good Luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top