Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-25
7
Medium Priority
?
1,187 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
Comment
Question by:ABaruh
[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 7

Author Comment

by:ABaruh
ID: 12405882
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:
tusharashah earned 1200 total points
ID: 12405900
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
ID: 12405918
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Assisted Solution

by:jnhorst
jnhorst earned 800 total points
ID: 12405937
<<
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
ID: 12405950
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
ID: 20333073
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
ID: 20335650
Show me the code...  Also check to see if enableviewstate is true for your ddl.

John
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

604 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