?
Solved

getting the selected value for an asp.net 3.5 dropdownlist

Posted on 2010-08-18
7
Medium Priority
?
384 Views
Last Modified: 2013-11-27
I have a list of items in an asp.net drop down list. but when i select the selectedvalue is always the first item in the list.

Help
0
Comment
Question by:Anthony Matovu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 27

Expert Comment

by:nmarun
ID: 33472294
Hello MatovuAnthony,

Please post some code as to what exactly you're doing.


Regards,

Arun
0
 
LVL 14

Expert Comment

by:Dhanasekaran Sengodan
ID: 33472295
check your Databind to dropdownlist, whether Databind twice. can you post sample code?
0
 
LVL 18

Assisted Solution

by:Ajay Sharma
Ajay Sharma earned 1600 total points
ID: 33472319
Place the item fill / databound code in the page load like below:
if (!IsPostBack)
{
     //databind / fill method goes here
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Assisted Solution

by:Tapan Pattanaik
Tapan Pattanaik earned 400 total points
ID: 33472339
0
 
LVL 1

Author Comment

by:Anthony Matovu
ID: 33472366


The code i am using is below.

but at line xxxxx the dr1.selectedvalue is always = 0

Thanks

Anthony

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        z.Load(MapPath("~/FieldLabels.xml"))
        Dim labs As XmlNodeList = z.SelectNodes("/questions/section/question[@qid='q36']/answer")
        dr1.Items.Clear()
        dr1.Items.Add(New ListItem(" ", 0))
     
        For Each lab As XmlNode In labs
            Me.dr1.Items.Add(New ListItem(lab.InnerText, lab.Attributes.ItemOf(0).Value))
        Next
  End Sub
      

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim uacDb As New System.Data.SqlClient.SqlConnection
        Dim dcmd As New System.Data.SqlClient.SqlCommand
        Dim tsql As String
        Dim tstr As String
        Try
            uacDb = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("dbase").ToString)
            uacDb.Open()

            tstr = "INSERT INTO [qn36] (bseqno,"
            tsql = " values(" & Session("agency") & ","

           xxxxxx
 If Not dr1.SelectedValue = "0" Then
                tsql = Trim(tsql) & dr1.SelectedValue & ","
                tstr = Trim(tstr) & "[qn36c1]" & ","
                If Trim(qn36c10a.Text & "").Length > 0 Then : tsql = Trim(tsql) & qn36c10a.Text & "," : tstr = Trim(tstr) & "[qn36c10]" & "," : End If
                If Trim(qn36c9a.Text & "").Length > 0 Then : tsql = Trim(tsql) & qn36c9a.Text & "," : tstr = Trim(tstr) & "[qn36c9]" & "," : End If
                If Trim(qn36c8a.Text & "").Length > 0 Then : tsql = Trim(tsql) & qn36c8a.Text & "," : tstr = Trim(tstr) & "[qn36c8]" & "," : End If
    End If
0
 
LVL 18

Accepted Solution

by:
Ajay Sharma earned 1600 total points
ID: 33472388
If Not Page.IsPostBack Then
z.Load(MapPath("~/FieldLabels.xml"))
        Dim labs As XmlNodeList = z.SelectNodes("/questions/section/question[@qid='q36']/answer")
        dr1.Items.Clear()
        dr1.Items.Add(New ListItem(" ", 0))
     
        For Each lab As XmlNode In labs
            Me.dr1.Items.Add(New ListItem(lab.InnerText, lab.Attributes.ItemOf(0).Value))
        Next
 
End If
0
 
LVL 14

Expert Comment

by:Dhanasekaran Sengodan
ID: 33472398
Here i attached modified code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

if not ispostback then
        z.Load(MapPath("~/FieldLabels.xml"))
        Dim labs As XmlNodeList = z.SelectNodes("/questions/section/question[@qid='q36']/answer")
        dr1.Items.Clear()
        dr1.Items.Add(New ListItem(" ", 0))
     
        For Each lab As XmlNode In labs
            Me.dr1.Items.Add(New ListItem(lab.InnerText, lab.Attributes.ItemOf(0).Value))
        Next
End if
  End Sub

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question