Solved

Dropdown List not grabbing Selected Value when selected

Posted on 2011-02-22
5
311 Views
Last Modified: 2012-05-11
I have several dropdown lists I am using on my form each one of them is supplied by a database table using linq. I have defined my DataValueField and DataTextFields, and they display correct. However when I go to pull the DataValueField when I insert data to the database. The system is passing the first item from the database not the item that was selected.

With the code below. I am expecting "310332" to be inserted however it is inserting "2042356" Which is the first item in the ddl datasource.

Jeff

/// Here is the ddl setup ///
ddlEffectedSystemAdd.DataSource =  ddlSystemValues;
ddlEffectedSystemAdd.DataValueField = "SystemID";
ddlEffectedSystemAdd.DataTextField = "SystemName";
ddlEffectedSystemAdd.DataBind();           

/// And here is where I am calling the ddl in the insert ///
tt.TicketInitiator = ddlTicketInitiatorAdd.SelectedValue;

Open in new window

0
Comment
Question by:woodje
  • 2
  • 2
5 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 34955477
looks like you are bindind the data each time...
you should get & bind data once when page.isPostBack is false...
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 34955494
like this:

void Page_Load(){
   if (Page.IsPostBack){
     getMyData();
   }
}

where getMyData() is a procedure that loads data into drop down... if you do not do this, and always bind data, thenselected item will be lost, and selectedindex will be set to 0, first item every time page loads...
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 34955593
Yes, set the autopostback of the DropDownList to true.

Do this in Form_Load:

ddlTicketInitiatorAdd.AutoPostback=true;

Open in new window

0
 

Author Closing Comment

by:woodje
ID: 34955615
The postback worked perfect. I forgot about that.
0
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 34956491
Which postback worked? You were given 2 approaches.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question about JQuery and asp.net 3 30
Call Controller Action Method from ASPX 2 22
hbo knew my windows software 4 62
ASP Repeater control - onl;y show if flag is true 7 15
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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