473,416 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Determining Dropdown Value Problem

I cannot get the correct drop down list value from a drop down I have on my
web form. I get the initial value that was loaded in the list.

It was asked by someone else what the autopostback was set to...it is set to
false.

Can someone show me what I am doing wrong and tell me the correct way? Thank
you.

In the page load event, I am doing the following:

//Code I use to populate the dropdown list.
ddAssignedTo.DataValueField = "PersonnelID";
ddAssignedTo.DataTextField = "DisplayName";
ddAssignedTo.DataSource = Personnel.List();
ddAssignedTo.DataBind();

//Code I use to show the value from the database.
ddAssignedTo.SelectedValue = problem.AddedBy.PersonnelId.ToString();

When I change the drop down to another item in the list, and click my update
button, I get the previous value that was selected on page load.

//Code I use for that is:
ddAssignedTo.SelectedValue

What am I doing wrong...thank you.

Jan 24 '06 #1
2 4523
Mike,
In working with ASP.NET it's important to understand how the event model
works.
When you need an event (such as "SelectedIndexChanged" for a dropdown) to
fire, the control needs to be able to Autopostback. So when a new item is
selected by the user, the page posts back and the SelectedIndexChanged even
is fired in the page processing.

It is in this eventhandler that you would get the new selected Index or
whatever you need.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Mike Collins" wrote:
I cannot get the correct drop down list value from a drop down I have on my
web form. I get the initial value that was loaded in the list.

It was asked by someone else what the autopostback was set to...it is set to
false.

Can someone show me what I am doing wrong and tell me the correct way? Thank
you.

In the page load event, I am doing the following:

//Code I use to populate the dropdown list.
ddAssignedTo.DataValueField = "PersonnelID";
ddAssignedTo.DataTextField = "DisplayName";
ddAssignedTo.DataSource = Personnel.List();
ddAssignedTo.DataBind();

//Code I use to show the value from the database.
ddAssignedTo.SelectedValue = problem.AddedBy.PersonnelId.ToString();

When I change the drop down to another item in the list, and click my update
button, I get the previous value that was selected on page load.

//Code I use for that is:
ddAssignedTo.SelectedValue

What am I doing wrong...thank you.

Jan 24 '06 #2
Thanks for the reply Peter. I believe I've finally stumbled on the answer.

In putting a reference to AutoPostBack, it looks like I misled you into
looking at my problem from a different viewpoint.

When loading a page that included a dropdown list, when I changed that
dropdown to another item in the list, and submitted the page, and then read
the selected item with the following code:
ddAssignedTo.SelectedItem.ToString(), I was getting the last item that was
loaded into the dropdown. I kept looking at the button click event, but in my
page_load I was always populating the dropdown lists. What I just found out
was that the page_load event fires before the button click event. So I was
repopulating the dropdown before I was checking to see what the user had
selected. Looks like I need to review my steps on page_load and ensure that I
am not repopulating my dropdowns when a user clicks update. Below is the
whole code showing the page_load and that I am repopulating the dropdowns
then the button click event runs. Do you agree with what I think I've
seen...any suggestions on how to handle this better? Thanks a bunch for your
help.
public class frmProblem : System.Web.UI.Page
{
#region Private Member Variables

protected System.Web.UI.WebControls.TextBox txtProblem;
protected System.Web.UI.WebControls.Label lblProblem;
protected System.Web.UI.WebControls.TextBox txtFix;
protected System.Web.UI.WebControls.Label lblSolution;
protected System.Web.UI.WebControls.TextBox txtNotes;
protected System.Web.UI.WebControls.Label lblDateReported;
protected System.Web.UI.WebControls.Label lblReportedBy;
protected System.Web.UI.WebControls.DropDownList ddAssignedTo;
protected System.Web.UI.WebControls.Label lblAssignedTo;
protected System.Web.UI.WebControls.TextBox txtTimeSpent;
protected System.Web.UI.WebControls.DropDownList ddKeyArea;
protected System.Web.UI.WebControls.Label lblKeyArea;
protected System.Web.UI.WebControls.Label lblTimeSpent;
protected System.Web.UI.WebControls.DropDownList ddLocation;
protected System.Web.UI.WebControls.Label lblLocation;
protected System.Web.UI.WebControls.Label lblDateFixed;
protected System.Web.UI.WebControls.DropDownList ddStatus;
protected System.Web.UI.WebControls.Label lblStatus;
protected System.Web.UI.WebControls.DropDownList ddProject;
protected System.Web.UI.WebControls.Label lblProject;
protected System.Web.UI.WebControls.TextBox txtStatusDate;
protected System.Web.UI.WebControls.Label lblStatusDate;
protected System.Web.UI.WebControls.Label lblTitle;
protected System.Web.UI.WebControls.TextBox txtTitle;
protected System.Web.UI.WebControls.TextBox txtDateFixed;
protected System.Web.UI.WebControls.TextBox txtDateReported;
protected System.Web.UI.WebControls.DropDownList ddReportedBy;
protected System.Web.UI.WebControls.Button btnUpdate;
protected System.Web.UI.WebControls.Button btnAdd;
protected System.Web.UI.WebControls.Button btnCancel;
protected System.Web.UI.WebControls.Label lblNotes;
protected System.Web.UI.WebControls.DropDownList ddType;
protected System.Web.UI.WebControls.Label lblType;
#endregion

#region Page Load
protected HtmlGenericControl pageTitle = new HtmlGenericControl();

private void Page_Load(object sender, System.EventArgs e)
{
//This will allow the page to close without saving and changes.
this.btnCancel.Attributes.Add("OnClick", "self.close()");

//Populate each drop down, regardless if the user is adding or updating a
problem.
//If they are updating a problem, we will pull the selected value later.
ddLocation.DataSource = Location.List();
ddLocation.DataTextField = "Location";
ddLocation.DataValueField = "LocationID";
ddLocation.DataBind();
ddProject.DataValueField = "ProjectID";
ddProject.DataTextField = "Project";
ddProject.DataSource = Project.List();
ddProject.DataBind();
ddStatus.DataValueField = "StatusID";
ddStatus.DataTextField = "Status";
ddStatus.DataSource = Status.List();
ddStatus.DataBind();
ddAssignedTo.DataValueField = "PersonnelID";
ddAssignedTo.DataTextField = "DisplayName";
ddAssignedTo.DataSource = Personnel.List();
ddAssignedTo.DataBind();
ddReportedBy.DataValueField = "PersonnelID";
ddReportedBy.DataTextField = "DisplayName";
ddReportedBy.DataSource = Personnel.List();
ddReportedBy.DataBind();
ddKeyArea.DataValueField = "KeyAreaID";
ddKeyArea.DataTextField = "KeyArea";
ddKeyArea.DataSource = KeyArea.List();
ddKeyArea.DataBind();
ddType.DataValueField = "TypeID";
ddType.DataTextField = "Type";
ddType.DataSource = Type.List();
ddType.DataBind();

//If a problemID is part of the query string, then the user is intending
to update
//an existing problem. If the problemID is null, the user is adding a new
problem.
if (Request.QueryString["problemID"] != null)
{
if (!Page.IsPostBack)
{
int problemID = Int32.Parse(Request.QueryString["problemID"]);

//Save problemID in the application cache for use when the user saves
the page.
Application.Add("problemID", problemID);

pageTitle.InnerText = "Update Problem";
Problem problem = null;
problem = Problem.Get(problemID);
txtTitle.Text = problem.Title;
txtProblem.Text = problem.ProblemDescription;
txtFix.Text = problem.FixDescription;
txtNotes.Text = problem.Notes;
txtDateReported.Text = problem.DateReported.ToString("MM/dd/yyyy");

//The dropdowns were previously populated. Now that we have determined
//that the user is updating a problem, we need to show the current
values.

ddLocation.SelectedValue = problem.Location.LocationId.ToString();
ddProject.SelectedValue = problem.Project.ProjectId.ToString();
ddStatus.SelectedValue = problem.Status.StatusId.ToString();
ddAssignedTo.SelectedValue = problem.AddedBy.PersonnelId.ToString();
ddReportedBy.SelectedValue = problem.Originator.PersonnelId.ToString();
ddType.SelectedValue = problem.Type.TypeId.ToString();

if (problem.DateFixed != DateTime.MinValue)
txtDateFixed.Text = problem.DateFixed.ToString("MM/dd/yyyy");

if (problem.StatusDate != DateTime.MinValue)
txtStatusDate.Text = problem.StatusDate.ToString("MM/dd/yyyy");

//Show appropriate buttons for an update.
btnUpdate.Visible = true;
btnAdd.Visible = false;
}
}
else
{
//Default to today's date for new problems.
txtDateReported.Text = DateTime.Today.ToString("MM/dd/yyyy");
txtStatusDate.Text = DateTime.Today.ToString("MM/dd/yyyy");
pageTitle.InnerText = "Add Problem";
btnUpdate.Visible = false;
btnAdd.Visible = true;
}
}
#endregion

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
this.ID = "frmProblem";
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

#region Button Events
private void btnAdd_Click(object sender, System.EventArgs e)
{
string dateFixed = txtDateFixed.Text;
string timeSpent = txtTimeSpent.Text;

if (dateFixed == "")
{
dateFixed = Convert.ToDateTime(DateTime.MinValue).ToString();
}

if (timeSpent == "")
{
timeSpent = "0";
}

Problem.Add(int.Parse(ddReportedBy.SelectedValue),
int.Parse(ddAssignedTo.SelectedValue),
int.Parse(ddLocation.SelectedValue),
Convert.ToDateTime(txtDateReported.Text),
Convert.ToDateTime(dateFixed), int.Parse(ddType.SelectedValue),
int.Parse(ddStatus.SelectedValue),
int.Parse(ddProject.SelectedValue), txtTitle.Text, txtProblem.Text,
txtFix.Text, Convert.ToDecimal(timeSpent),
txtNotes.Text);
}

private void btnUpdate_Click(object sender, System.EventArgs e)
{
int problemID = int.Parse(Application.Get("problemID").ToString()) ;
string dateFixed = txtDateFixed.Text;
string timeSpent = txtTimeSpent.Text;

if (dateFixed == "")
{
dateFixed = Convert.ToDateTime(DateTime.MinValue).ToString();
}

if (timeSpent == "")
{
timeSpent = "0";
}

Problem.Update(problemID, int.Parse(ddReportedBy.SelectedItem.Value),
int.Parse(ddAssignedTo.SelectedValue),
int.Parse(ddLocation.SelectedValue), int.Parse(ddStatus.SelectedValue),
int.Parse(ddProject.SelectedValue),
int.Parse(ddType.SelectedValue), Convert.ToDateTime(txtDateReported.Text),
Convert.ToDateTime(dateFixed), txtTitle.Text, txtProblem.Text,
txtFix.Text,
Convert.ToDecimal(timeSpent), txtNotes.Text);
}
#endregion
}

"Peter Bromberg [C# MVP]" wrote:
Mike,
In working with ASP.NET it's important to understand how the event model
works.
When you need an event (such as "SelectedIndexChanged" for a dropdown) to
fire, the control needs to be able to Autopostback. So when a new item is
selected by the user, the page posts back and the SelectedIndexChanged even
is fired in the page processing.

It is in this eventhandler that you would get the new selected Index or
whatever you need.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Mike Collins" wrote:
I cannot get the correct drop down list value from a drop down I have on my
web form. I get the initial value that was loaded in the list.

It was asked by someone else what the autopostback was set to...it is set to
false.

Can someone show me what I am doing wrong and tell me the correct way? Thank
you.

In the page load event, I am doing the following:

//Code I use to populate the dropdown list.
ddAssignedTo.DataValueField = "PersonnelID";
ddAssignedTo.DataTextField = "DisplayName";
ddAssignedTo.DataSource = Personnel.List();
ddAssignedTo.DataBind();

//Code I use to show the value from the database.
ddAssignedTo.SelectedValue = problem.AddedBy.PersonnelId.ToString();

When I change the drop down to another item in the list, and click my update
button, I get the previous value that was selected on page load.

//Code I use for that is:
ddAssignedTo.SelectedValue

What am I doing wrong...thank you.

Jan 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joseph Barron | last post by:
Here is a SIMPLE problem that I'm trying to solve. It works in Netscape 6.2, but IE6 gives ""No such interface supported." Below are page1.htm and page2.htm . In page1.htm, there are two...
0
by: Manish | last post by:
Hi All, I'm having this problem I hope someone can help provide a solution for it :) I've this dropdown list box in a usercontrol which I'm populating from the database (it's viewstate property...
1
by: Mad Scientist Jr | last post by:
I don't know how this is happening, but a dropdown control I have is resetting to the 2nd value on the list anytime a postback occurs. I have no initiation code outside of If Not (IsPostBack) ...
4
by: dchillman | last post by:
string ctrlname = page.Request.Params.Get("__EVENTTARGET"); if (ctrlname != null && ctrlname != string.Empty) { return this.Page.FindControl(ctrlname); } I have a form on which I was trying...
3
by: Mike Collins | last post by:
I'm not feeling too smart right now, but I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. Can someone show...
3
by: devNorway | last post by:
I have been struggling with a problem for days now, and searched for related problems and solutions but had no luck. I have two dropdown listboxes where the first is populated in page load and...
4
by: Greg Scharlemann | last post by:
I'm trying to setup a dyamic dropdown list that displays a number of text fields based on the selected number in the dropdown. The problem I am running into is capturing the data already entered...
13
by: Shutey | last post by:
I have a strange issue with dropdowns. Using php4, mySQL5, Apache 2 on a fast XP pro PC, I have a form which requires 5 dropdowns populated with indentical values. I extract the values using SQL...
2
by: raamay | last post by:
I want to have a dynamic dropdown box whose entries would depend on the selection of an entry in the first dropdown box. BUT the second dropdown box should not reload, only the entries inside should...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.