| | |
Dropdown list in ASP.NET - How to get value selected
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2006
Posts: 8
Reputation:
Solved Threads: 0
I am sure this must be very simple, but unforunately I can't figure it out.
I have the following code which populates the dropdown lists from the database. This works fine. Now, when the user selects a particular employee name from the list, I want to get the Employee Id of the selected employee. i.e. I need to get the "Eid" value. I can do this if the EId is loaded in the dropdown list, but in this case only the employee name is displayed. Please let me know howI can get the Eid value even though it is not loaded in the dropdown list.
I would really appreciate your help
Thanks
- Artee
Please see code below
I have the following code which populates the dropdown lists from the database. This works fine. Now, when the user selects a particular employee name from the list, I want to get the Employee Id of the selected employee. i.e. I need to get the "Eid" value. I can do this if the EId is loaded in the dropdown list, but in this case only the employee name is displayed. Please let me know howI can get the Eid value even though it is not loaded in the dropdown list.
I would really appreciate your help
Thanks
- Artee
Please see code below
ASP.NET Syntax (Toggle Plain Text)
Dim strConn As String strConn = ConfigurationSettings.AppSettings("connectionString") Dim myConn As New SqlConnection(strConn) Try Dim strSQL As String cboEmpList.Items.Clear() strSQL = "select distinct EId, LName, FName, Mi " & _ " from EmpQualMain " & _ " where BrCode = '" & gBranchCode & "'" & _ " order by Lname, FName" Dim myCommand As New SqlCommand(strSQL, myConn) myConn.Open() Dim myRead As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.Default) While myRead.Read cboEmpList.Items.Add(myRead.GetValue(myRead.GetOrdinal("LName")) + ", " + myRead.GetValue(myRead.GetOrdinal("FName")) + " " + myRead.GetValue(myRead.GetOrdinal("Mi"))) cboEmpList.DataValueField = myRead.GetValue(myRead.GetOrdinal("EId")) End While myRead.Close() Catch exc1 As Exception Response.Write(exc1.Message) Finally If Not (myConn Is Nothing) Then If myConn.State = System.Data.ConnectionState.Open Then myConn.Close() End If End Try
cboEmpList.DataValueField = myRead.GetValue(myRead.GetOrdinal("EId"))
that line is only useful if you are databinding in your case use
that line is only useful if you are databinding in your case use
ASP.NET Syntax (Toggle Plain Text)
cboEmpList.Items.Add(New ListItem(myRead.GetValue(myRead.GetOrdinal("EId")), myRead.GetOrdinal("FName")) + " " + myRead.GetValue(myRead.GetOrdinal("Mi")))
Last edited by sedgey; Feb 10th, 2007 at 4:34 pm.
•
•
Join Date: Dec 2007
Posts: 17
Reputation:
Solved Threads: 0
•
•
•
•
cboEmpList.DataValueField = myRead.GetValue(myRead.GetOrdinal("EId"))
that line is only useful if you are databinding in your case use
ASP.NET Syntax (Toggle Plain Text)
cboEmpList.Items.Add(New ListItem(myRead.GetValue(myRead.GetOrdinal("EId")), myRead.GetOrdinal("FName")) + " " + myRead.GetValue(myRead.GetOrdinal("Mi")))
cboEmpList.DataValueField = myRead.GetValue(myRead.GetOrdinal("EId"));
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
Isn't it just easier to bind your employee id as the Value of the Dropdownlist item, and their name as the Text of the Dropdownlist item.
You can receive the name of the dropdownlist by:
dropdownlistname.SelectedItem.Text
And the value of the dropdownlist selected item by:
dropdownlistname.SelectedItem.Value
So a simple:
Dim strEid As String = dropdownlistname.SelectedItem.Value
should do the trick as long as the item's value is the Eid.
You can receive the name of the dropdownlist by:
dropdownlistname.SelectedItem.Text
And the value of the dropdownlist selected item by:
dropdownlistname.SelectedItem.Value
So a simple:
Dim strEid As String = dropdownlistname.SelectedItem.Value
should do the trick as long as the item's value is the Eid.
•
•
Join Date: Dec 2007
Posts: 17
Reputation:
Solved Threads: 0
•
•
•
•
Isn't it just easier to bind your employee id as the Value of the Dropdownlist item, and their name as the Text of the Dropdownlist item.
You can receive the name of the dropdownlist by:
dropdownlistname.SelectedItem.Text
And the value of the dropdownlist selected item by:
dropdownlistname.SelectedItem.Value
So a simple:
Dim strEid As String = dropdownlistname.SelectedItem.Value
should do the trick as long as the item's value is the Eid.
![]() |
Similar Threads
- Updated : Simple ASP.Net Login Page (ASP.NET)
- Error in loading items into a Dropdown list box in an ASP .net Page (ASP.NET)
- Dynamic dropdown list (ASP.NET)
- Retrieving Multiple Columns into Dropdown List (ASP.NET)
- ASP.NET, referring to a DataSet within script (ASP)
- Populating & Retrieving Data in a listbox : ASP.NET (w/ VB.NET) (ASP.NET)
Other Threads in the ASP.NET Forum
- Previous Thread: Help Me ! (ASP.NET)
- Next Thread: Download File
| Thread Tools | Search this Thread |
.net 2.0 activexcontrol advice ajax alltypeofvideos appliances asp asp.net bc30451 beginner bottomasp.net browser button c# c#gridviewcolumn cac checkbox commonfunctions compatible confirmationcodegeneration content courier css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dynamically edit fileuploader fill flash formatdecimal forms formview gridview gudi homeedition iframe iis javascript jquery listbox microsoft mono mouse mssql multistepregistration news numerical objects opera panelmasterpagebuttoncontrols radio ratings redirect registration relationaldatabases reportemail rotatepage schoolproject search security serializesmo.table sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 suse textbox tracking treeview unauthorized validatedate validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webapplications webarchitecture webdevelopemnt webprogramming webservice xml xsl youareanotmemberofthedebuggerusers






