Link to home
Start Free TrialLog in
Avatar of ABaruh
ABaruh

asked on

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.
Avatar of ABaruh
ABaruh

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of tusharashah
tusharashah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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?
Show me the code...  Also check to see if enableviewstate is true for your ddl.

John