Solved

getting the selected value for an asp.net 3.5 dropdownlist

Posted on 2010-08-18
7
380 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
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 400 total points
ID: 33472319
Place the item fill / databound code in the page load like below:
if (!IsPostBack)
{
     //databind / fill method goes here
}
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 21

Assisted Solution

by:Tapan Pattanaik
Tapan Pattanaik earned 100 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 400 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Web site error 3 36
ck editor is taking too much time to be loaded in asp 1 16
REXEX help Part 2 2 22
vb.net winforms sizing/resolution? 4 29
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 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