472,805 Members | 926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 4442
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.