Determining Dropdown Value Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Collins

    #1

    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.Da taValueField = "PersonnelI D";
    ddAssignedTo.Da taTextField = "DisplayNam e";
    ddAssignedTo.Da taSource = Personnel.List( );
    ddAssignedTo.Da taBind();

    //Code I use to show the value from the database.
    ddAssignedTo.Se lectedValue = problem.AddedBy .PersonnelId.To String();

    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.Se lectedValue

    What am I doing wrong...thank you.

  • Peter Bromberg [C# MVP]

    #2
    RE: Determining Dropdown Value Problem

    Mike,
    In working with ASP.NET it's important to understand how the event model
    works.
    When you need an event (such as "SelectedIndexC hanged" 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 SelectedIndexCh anged 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:

    UnBlog:





    "Mike Collins" wrote:
    [color=blue]
    > 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.Da taValueField = "PersonnelI D";
    > ddAssignedTo.Da taTextField = "DisplayNam e";
    > ddAssignedTo.Da taSource = Personnel.List( );
    > ddAssignedTo.Da taBind();
    >
    > //Code I use to show the value from the database.
    > ddAssignedTo.Se lectedValue = problem.AddedBy .PersonnelId.To String();
    >
    > 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.Se lectedValue
    >
    > What am I doing wrong...thank you.
    >[/color]

    Comment

    • Mike Collins

      #3
      RE: Determining Dropdown Value Problem

      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.Se lectedItem.ToSt ring(), 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.P age
      {
      #region Private Member Variables

      protected System.Web.UI.W ebControls.Text Box txtProblem;
      protected System.Web.UI.W ebControls.Labe l lblProblem;
      protected System.Web.UI.W ebControls.Text Box txtFix;
      protected System.Web.UI.W ebControls.Labe l lblSolution;
      protected System.Web.UI.W ebControls.Text Box txtNotes;
      protected System.Web.UI.W ebControls.Labe l lblDateReported ;
      protected System.Web.UI.W ebControls.Labe l lblReportedBy;
      protected System.Web.UI.W ebControls.Drop DownList ddAssignedTo;
      protected System.Web.UI.W ebControls.Labe l lblAssignedTo;
      protected System.Web.UI.W ebControls.Text Box txtTimeSpent;
      protected System.Web.UI.W ebControls.Drop DownList ddKeyArea;
      protected System.Web.UI.W ebControls.Labe l lblKeyArea;
      protected System.Web.UI.W ebControls.Labe l lblTimeSpent;
      protected System.Web.UI.W ebControls.Drop DownList ddLocation;
      protected System.Web.UI.W ebControls.Labe l lblLocation;
      protected System.Web.UI.W ebControls.Labe l lblDateFixed;
      protected System.Web.UI.W ebControls.Drop DownList ddStatus;
      protected System.Web.UI.W ebControls.Labe l lblStatus;
      protected System.Web.UI.W ebControls.Drop DownList ddProject;
      protected System.Web.UI.W ebControls.Labe l lblProject;
      protected System.Web.UI.W ebControls.Text Box txtStatusDate;
      protected System.Web.UI.W ebControls.Labe l lblStatusDate;
      protected System.Web.UI.W ebControls.Labe l lblTitle;
      protected System.Web.UI.W ebControls.Text Box txtTitle;
      protected System.Web.UI.W ebControls.Text Box txtDateFixed;
      protected System.Web.UI.W ebControls.Text Box txtDateReported ;
      protected System.Web.UI.W ebControls.Drop DownList ddReportedBy;
      protected System.Web.UI.W ebControls.Butt on btnUpdate;
      protected System.Web.UI.W ebControls.Butt on btnAdd;
      protected System.Web.UI.W ebControls.Butt on btnCancel;
      protected System.Web.UI.W ebControls.Labe l lblNotes;
      protected System.Web.UI.W ebControls.Drop DownList ddType;
      protected System.Web.UI.W ebControls.Labe l lblType;
      #endregion

      #region Page Load
      protected HtmlGenericCont rol pageTitle = new HtmlGenericCont rol();

      private void Page_Load(objec t sender, System.EventArg s 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.Data Source = Location.List() ;
      ddLocation.Data TextField = "Location";
      ddLocation.Data ValueField = "LocationID ";
      ddLocation.Data Bind();
      ddProject.DataV alueField = "ProjectID" ;
      ddProject.DataT extField = "Project";
      ddProject.DataS ource = Project.List();
      ddProject.DataB ind();
      ddStatus.DataVa lueField = "StatusID";
      ddStatus.DataTe xtField = "Status";
      ddStatus.DataSo urce = Status.List();
      ddStatus.DataBi nd();
      ddAssignedTo.Da taValueField = "PersonnelI D";
      ddAssignedTo.Da taTextField = "DisplayNam e";
      ddAssignedTo.Da taSource = Personnel.List( );
      ddAssignedTo.Da taBind();
      ddReportedBy.Da taValueField = "PersonnelI D";
      ddReportedBy.Da taTextField = "DisplayNam e";
      ddReportedBy.Da taSource = Personnel.List( );
      ddReportedBy.Da taBind();
      ddKeyArea.DataV alueField = "KeyAreaID" ;
      ddKeyArea.DataT extField = "KeyArea";
      ddKeyArea.DataS ource = KeyArea.List();
      ddKeyArea.DataB ind();
      ddType.DataValu eField = "TypeID";
      ddType.DataText Field = "Type";
      ddType.DataSour ce = 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.QueryS tring["problemID"] != null)
      {
      if (!Page.IsPostBa ck)
      {
      int problemID = Int32.Parse(Req uest.QueryStrin g["problemID"]);

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

      pageTitle.Inner Text = "Update Problem";
      Problem problem = null;
      problem = Problem.Get(pro blemID);
      txtTitle.Text = problem.Title;
      txtProblem.Text = problem.Problem Description;
      txtFix.Text = problem.FixDesc ription;
      txtNotes.Text = problem.Notes;
      txtDateReported .Text = problem.DateRep orted.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.Sele ctedValue = problem.Locatio n.LocationId.To String();
      ddProject.Selec tedValue = problem.Project .ProjectId.ToSt ring();
      ddStatus.Select edValue = problem.Status. StatusId.ToStri ng();
      ddAssignedTo.Se lectedValue = problem.AddedBy .PersonnelId.To String();
      ddReportedBy.Se lectedValue = problem.Origina tor.PersonnelId .ToString();
      ddType.Selected Value = problem.Type.Ty peId.ToString() ;

      if (problem.DateFi xed != DateTime.MinVal ue)
      txtDateFixed.Te xt = problem.DateFix ed.ToString("MM/dd/yyyy");

      if (problem.Status Date != DateTime.MinVal ue)
      txtStatusDate.T ext = problem.StatusD ate.ToString("M M/dd/yyyy");

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

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

      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeCompo nent()
      {
      this.btnAdd.Cli ck += new System.EventHan dler(this.btnAd d_Click);
      this.btnUpdate. Click += new System.EventHan dler(this.btnUp date_Click);
      this.ID = "frmProblem ";
      this.Load += new System.EventHan dler(this.Page_ Load);

      }
      #endregion

      #region Button Events
      private void btnAdd_Click(ob ject sender, System.EventArg s e)
      {
      string dateFixed = txtDateFixed.Te xt;
      string timeSpent = txtTimeSpent.Te xt;

      if (dateFixed == "")
      {
      dateFixed = Convert.ToDateT ime(DateTime.Mi nValue).ToStrin g();
      }

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

      Problem.Add(int .Parse(ddReport edBy.SelectedVa lue),
      int.Parse(ddAss ignedTo.Selecte dValue),
      int.Parse(ddLoc ation.SelectedV alue),
      Convert.ToDateT ime(txtDateRepo rted.Text),
      Convert.ToDateT ime(dateFixed), int.Parse(ddTyp e.SelectedValue ),
      int.Parse(ddSta tus.SelectedVal ue),
      int.Parse(ddPro ject.SelectedVa lue), txtTitle.Text, txtProblem.Text ,
      txtFix.Text, Convert.ToDecim al(timeSpent),
      txtNotes.Text);
      }

      private void btnUpdate_Click (object sender, System.EventArg s e)
      {
      int problemID = int.Parse(Appli cation.Get("pro blemID").ToStri ng());
      string dateFixed = txtDateFixed.Te xt;
      string timeSpent = txtTimeSpent.Te xt;

      if (dateFixed == "")
      {
      dateFixed = Convert.ToDateT ime(DateTime.Mi nValue).ToStrin g();
      }

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

      Problem.Update( problemID, int.Parse(ddRep ortedBy.Selecte dItem.Value),
      int.Parse(ddAss ignedTo.Selecte dValue),
      int.Parse(ddLoc ation.SelectedV alue), int.Parse(ddSta tus.SelectedVal ue),
      int.Parse(ddPro ject.SelectedVa lue),
      int.Parse(ddTyp e.SelectedValue ), Convert.ToDateT ime(txtDateRepo rted.Text),
      Convert.ToDateT ime(dateFixed), txtTitle.Text, txtProblem.Text ,
      txtFix.Text,
      Convert.ToDecim al(timeSpent), txtNotes.Text);
      }
      #endregion
      }





      "Peter Bromberg [C# MVP]" wrote:
      [color=blue]
      > Mike,
      > In working with ASP.NET it's important to understand how the event model
      > works.
      > When you need an event (such as "SelectedIndexC hanged" 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 SelectedIndexCh anged 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:
      >[color=green]
      > > 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.Da taValueField = "PersonnelI D";
      > > ddAssignedTo.Da taTextField = "DisplayNam e";
      > > ddAssignedTo.Da taSource = Personnel.List( );
      > > ddAssignedTo.Da taBind();
      > >
      > > //Code I use to show the value from the database.
      > > ddAssignedTo.Se lectedValue = problem.AddedBy .PersonnelId.To String();
      > >
      > > 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.Se lectedValue
      > >
      > > What am I doing wrong...thank you.
      > >[/color][/color]

      Comment

      Working...