Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add An Option Manually In A DropDownList

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
IN
I am using the following code to populate a DropDownList with the records existing in a database (the DropDownList gets populated with a list of different company names):

<script language=&quot;VB&quot; runat=&quot;server&quot;>
Sub Page_Load(obj As Object,ea As EventArgs)
If Not(Page.IsPostBack) Then
Dim objDS As DataSet
Dim objConn As SQLConnection
Dim objDapter As SQLDataAdapter

objConn=New SQLConnection(&quot;Server=(local).........&quot;)
objDapter=New SQLDataAdapter(&quot;SELECT CName,CCode,CName,CCode FROM Company&quot;,objConn)

objDS=new DataSet()
objDapter.Fill(objDS,&quot;Comp&quot;)

cname.DataSource=objDS.Tables(&quot;Comp&quot;).DefaultView
cname.DataBind()
End If
End Sub
</script>
<form runat=&quot;server&quot;>
<asp:DropDownList id=&quot;cname&quot; AutoPostBack=&quot;true&quot; DataTextField=&quot;CName&quot; DataValueField=&quot;CCode&quot; OnSelectedIndexChanged=&quot;selectEName&quot; runat=&quot;server&quot;>
</asp:DropDownList>
</form>

Now, as expected, the DropDownList will be populated with the different company names.Even the 1st option that will be displayed in the DropDownList will be a company name. The HTML source code when viewed will be something like this:

<select.....>
<option value=&quot;C1&quot;>Company1</option>
<option value=&quot;C2&quot;>Company2</option>
<option value=&quot;C3&quot;>Company3</option>
<option value=&quot;C4&quot;>Company4</option>
</select>

However, I want that the 1st option in the DropDownList should be something like &quot;Select Your Company&quot; & then only the different company names should be listed. For eg. the HTML source code should be something like this:

<select.....>
<option>Select Your Company</option>
<option value=&quot;C1&quot;>Company1</option>
<option value=&quot;C2&quot;>Company2</option>
<option value=&quot;C3&quot;>Company3</option>
<option value=&quot;C4&quot;>Company4</option>
</select>

How do I do this in ASP.NET?

Thanks,

Arpan
 
cname.DataSource=objDS.Tables(&quot;Comp&quot;).DefaultView
cname.DataBind()
dim li as new listItem(&quot;0&quot;,&quot;Select Your Company&quot;)
cName.items.insert(0,li)
penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top