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
}