Solved

SelectedValue from a Dropdown is always defaulting to first item in list, regardless of what I select

Posted on 2004-10-25
1,133 Views
Last Modified: 2010-05-18
I have 3 dropdown lists on my asp.net page and a button called "Output Report".  I either need to post the selections to another aspx page (ideal) or grab the 3 selected values and pass them in a querystring.

I am having difficulty with scenario 1 (post to another page).  Whenever I change the name of the form action from Page1.aspx to Page2.aspx, save and rebuild, when I view the HTML source of Page1, it shows the Form Action going to Page1.aspx.

So what I have been attempting to get around this is put a command on my "Output Report" so that I grab the selections using Select1.SelectedValue, etc. and pass them to Page2 in a querystring, like Response.redirect "Page2.aspx?Select1=" & Select1.SelectedValue......etc.

What is happening is that it ALWAYS grabs the first value of my dropdown lists, regardless of what I select.
0
Question by:ABaruh
    7 Comments
     
    LVL 7

    Author Comment

    by:ABaruh
    I just discovered that I can get it to work by using Request.Form("Select1") instead of Select1.SelectedValue.

    Why is this?  Why can't I use Select1.SelectedValue and have asp.net build the necessary web page correctly?
    0
     
    LVL 18

    Accepted Solution

    by:
    Make sure that you are binding your DropDownList inside !IsPostBack

    Sub Page_Load
        If Not IsPostBack
              'Bind Your DropDownList here        
        End If
    End Sub

    [C#]
    void Page_Load() {
        if (!IsPostBack) {
                 //Bind your DropDownList here
        }
    }

    -tushar
    0
     
    LVL 10

    Expert Comment

    by:jnhorst
    You cannot change the action attribute of the <form runat="server"> tag of an aspx page.  Doing a Response.Redirect() with the info in the query string is the way to go on that.

    I suspect the problem is that you are calling DataBind on your ddls when the page load regardless of whether the page is being posted back or not.  You want to do this in the page load routine: (pseudocode)

    If Page.IsPostBack Then
         ' get data for the ddls.
         ' call DataBind on each ddl
    End If

    You did not specify if you collect the data when the third ddl is selected or with a button.  Either way, in either the selectedindexchanged event of the last ddl or the click event of a button, the postback will happen and Page_Load will fire first.  If you do not discrimiate between postbacks and non postbacks (initial loads) in Page_Load, when you call DataBind on the ddls, they revert back to their initial selection.  After Page_Load first, the event routine fires, in which you can do your Response.Redirect with the data in the querystring.

    John
    0
     
    LVL 10

    Assisted Solution

    by:jnhorst
    <<
    I just discovered that I can get it to work by using Request.Form("Select1") instead of Select1.SelectedValue.
    Why is this?  Why can't I use Select1.SelectedValue and have asp.net build the necessary web page correctly?
    >>

    Because when you post back, Request.Form("Select1") has what the value was when the form was received by the server (before Page_Load was ececuted).  Then Page_Load fired, you called DataBind and the value was reset.  Then your event routine fired and you got the data from the reset control.

    John
    0
     
    LVL 10

    Expert Comment

    by:jnhorst
    Mistake in my original post:

    If Page.IsPostBack Then
         ' get data for the ddls.
         ' call DataBind on each ddl
    End If

    Should be

    If Not Page.IsPostBack Then
         ' get data for the ddls.
         ' call DataBind on each ddl
    End If

    John
    0
     

    Expert Comment

    by:mycacha
    I already do what in this solution, but problem stil persist. I have been put !postback, still same.....
    but my record is 27000, is that a concern?
    0
     
    LVL 10

    Expert Comment

    by:jnhorst
    Show me the code...  Also check to see if enableviewstate is true for your ddl.

    John
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now