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

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.
LVL 7
ABaruhAsked:
Who is Participating?
 
tusharashahConnect With a Mentor Commented:
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
 
ABaruhAuthor Commented:
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
 
jnhorstCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jnhorstConnect With a Mentor Commented:
<<
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
 
jnhorstCommented:
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
 
mycachaCommented:
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
 
jnhorstCommented:
Show me the code...  Also check to see if enableviewstate is true for your ddl.

John
0
All Courses

From novice to tech pro — start learning today.