DropDownList problem - argh

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Schmid

    DropDownList problem - argh


    When testing for the value of my DropDownList.Se lectedItem.Text - it always
    returns the Text of the first element of the list. It is a databound
    control, and I have a breakpoint in my code to test the value. I am trying
    to insert the Text from this control into a field in a database, and it
    perpetually inserts the text from the first item of the list instead of the
    selected item.

    What the heck could I be missing here?



  • Martin Schmid

    #2
    Re: DropDownList problem - argh

    I noticed that OE stripped the attachments... here they are pasted as text:

    WebForm1.aspx
    <%@ Page language="c#" Codebehind="Web Form1.aspx.cs" AutoEventWireup ="false"
    Inherits="DropL ist.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1 </title>
    <meta name="GENERATOR " Content="Micros oft Visual Studio 7.0">
    <meta name="CODE_LANG UAGE" Content="C#">
    <meta name="vs_defaul tClientScript" content="JavaSc ript">
    <meta name="vs_target Schema"
    content="http://schemas.microso ft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <asp:DropDownLi st id=DropDownList 1 runat="server" DataSource="<%# arrTest
    %>">
    </asp:DropDownLis t>
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
    <asp:Literal id="Literal1" runat="server"> </asp:Literal>
    </form>
    </body>
    </HTML>


    WebForm1.aspx.c s

    using System;
    using System.Collecti ons;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.Sess ionState;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.H tmlControls;

    namespace DropList
    {
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.P age
    {
    protected System.Web.UI.W ebControls.Butt on Button1;
    protected System.Web.UI.W ebControls.Lite ral Literal1;
    protected System.Web.UI.W ebControls.Drop DownList DropDownList1;

    public string[] arrTest={"One", "Two","Thre e"};

    private void Page_Load(objec t sender, System.EventArg s e)
    {
    // Put user code to initialize the page here
    DropDownList1.D ataBind();

    }

    #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.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
    this.Load += new System.EventHan dler(this.Page_ Load);

    }
    #endregion

    private void Button1_Click(o bject sender, System.EventArg s e)
    {
    Literal1.Text=D ropDownList1.Se lectedItem.Text ;
    }
    }
    }


    --
    Thanks,
    Martin Schmid, EIT, CCSA, MCDBA, MCSE
    "Martin Schmid" <martinschmid@s bcglobal.net.no spam> wrote in message
    news:OvBmFj9mDH A.2200@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Here sample code.
    >
    > Whenever I select and item from the list, the literal always updates to
    > display the first item in the list, i.e., 'One'.
    >
    > What am I doing wrong?
    >
    >
    >
    >
    > --
    > Thanks,
    > Martin Schmid, EIT, CCSA, MCDBA, MCSE
    > "Martin Schmid" <martinschmid@s bcglobal.net.no spam> wrote in message
    > news:upLTBS9mDH A.2488@TK2MSFTN GP12.phx.gbl...[color=green]
    > >
    > > When testing for the value of my DropDownList.Se lectedItem.Text - it[/color]
    > always[color=green]
    > > returns the Text of the first element of the list. It is a databound
    > > control, and I have a breakpoint in my code to test the value. I am[/color]
    > trying[color=green]
    > > to insert the Text from this control into a field in a database, and it
    > > perpetually inserts the text from the first item of the list instead of[/color]
    > the[color=green]
    > > selected item.
    > >
    > > What the heck could I be missing here?
    > >
    > >
    > >[/color]
    >
    >
    >[/color]


    Comment

    • Martin Schmid

      #3
      Re: DropDownList problem - argh

      I found a solution... but I don't yet understand it... can someone shed some
      light?

      I moved the DropDownList.Da taBind(); expression into :
      if(!IsPostBack)
      {
      drpMyDropDownLi st.DataBind();
      }

      Now, where I have my break clause to test the value of
      drpMyDropDownLi st.SelectedItem .Text, it has the selected item value as
      expected.


      --
      Thanks,
      Martin Schmid, EIT, CCSA, MCDBA, MCSE
      "Martin Schmid" <martinschmid@s bcglobal.net.no spam> wrote in message
      news:upLTBS9mDH A.2488@TK2MSFTN GP12.phx.gbl...[color=blue]
      >
      > When testing for the value of my DropDownList.Se lectedItem.Text - it[/color]
      always[color=blue]
      > returns the Text of the first element of the list. It is a databound
      > control, and I have a breakpoint in my code to test the value. I am[/color]
      trying[color=blue]
      > to insert the Text from this control into a field in a database, and it
      > perpetually inserts the text from the first item of the list instead of[/color]
      the[color=blue]
      > selected item.
      >
      > What the heck could I be missing here?
      >
      >
      >[/color]


      Comment

      • Peter Koen

        #4
        Re: DropDownList problem - argh

        "Martin Schmid" <martinschmid@s bcglobal.net.no spam> wrote in
        news:equ8Qf#mDH A.2080@TK2MSFTN GP10.phx.gbl:
        [color=blue]
        > I found a solution... but I don't yet understand it... can someone
        > shed some light?
        >
        > I moved the DropDownList.Da taBind(); expression into :
        > if(!IsPostBack)
        > {
        > drpMyDropDownLi st.DataBind();
        > }
        >
        > Now, where I have my break clause to test the value of
        > drpMyDropDownLi st.SelectedItem .Text, it has the selected item value as
        > expected.
        >
        >[/color]

        If you have the DataBind outside of the !IsPostBack then it is done
        everytime when the Page class is instantiated BEFORE any other events of
        that page ocure. Even bevore a change event. You would be overwriting the
        SelectedItem.Te xt to its original value.

        Now you do the Bind just once when the page is displayed for the first time
        and in the postback it is not newly binded. therefore the selectedItem will
        have the text that's been selected on the webpage.

        --
        best regards

        Peter Koen
        -----------------------------------
        MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS

        Comment

        • David

          #5
          Re: DropDownList problem - argh

          In article <uezGAp9mDHA.16 56@tk2msftngp13 .phx.gbl>, Martin Schmid wrote:[color=blue]
          > I noticed that OE stripped the attachments... here they are pasted as text:[/color]
          <snip>[color=blue]
          >
          > private void Page_Load(objec t sender, System.EventArg s e)
          > {
          > // Put user code to initialize the page here
          > DropDownList1.D ataBind();
          >
          > }[/color]

          This should be

          private void Page_Load(objec t sender, System.EventArg s e)
          {
          if(!IsPostBack)
          DropDownList1.D ataBind();
          }

          --------------

          What's happening is that when the page is posted back, Page_Load runs
          *before* the event handlers. Your original Page_load code is rebinding
          the DropDownList which reinstates the original values, and then the
          Button1 event handler is picking up those original values.




          --
          David
          dfoster at
          hotpop dot com

          Comment

          • Martin Schmid

            #6
            Re: DropDownList problem - argh

            Sort of what I was concluding. Leave it to Microsoft Press to publish crap
            code. 'Developing Web Applications with Microsoft VB.NET and VC#.NET' has
            an example in which this is the issue..

            --
            Thanks,
            Martin Schmid, EIT, CCSA, MCDBA, MCSE
            "Peter Koen" <koen-newsreply&snusn u.at> wrote in message
            news:Oo3p6l%23m DHA.1960@TK2MSF TNGP12.phx.gbl. ..[color=blue]
            > "Martin Schmid" <martinschmid@s bcglobal.net.no spam> wrote in
            > news:equ8Qf#mDH A.2080@TK2MSFTN GP10.phx.gbl:
            >[color=green]
            > > I found a solution... but I don't yet understand it... can someone
            > > shed some light?
            > >
            > > I moved the DropDownList.Da taBind(); expression into :
            > > if(!IsPostBack)
            > > {
            > > drpMyDropDownLi st.DataBind();
            > > }
            > >
            > > Now, where I have my break clause to test the value of
            > > drpMyDropDownLi st.SelectedItem .Text, it has the selected item value as
            > > expected.
            > >
            > >[/color]
            >
            > If you have the DataBind outside of the !IsPostBack then it is done
            > everytime when the Page class is instantiated BEFORE any other events of
            > that page ocure. Even bevore a change event. You would be overwriting the
            > SelectedItem.Te xt to its original value.
            >
            > Now you do the Bind just once when the page is displayed for the first[/color]
            time[color=blue]
            > and in the postback it is not newly binded. therefore the selectedItem[/color]
            will[color=blue]
            > have the text that's been selected on the webpage.
            >
            > --
            > best regards
            >
            > Peter Koen
            > -----------------------------------
            > MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
            > http://www.kema.at[/color]


            Comment

            • Peter Koen

              #7
              Re: DropDownList problem - argh

              "Martin Schmid" <martinschmid@s bcglobal.net.no spam> wrote in
              news:#kapwrBnDH A.1948@TK2MSFTN GP12.phx.gbl:
              [color=blue]
              > Sort of what I was concluding. Leave it to Microsoft Press to publish
              > crap code. 'Developing Web Applications with Microsoft VB.NET and
              > VC#.NET' has an example in which this is the issue..
              >[/color]

              This is not the first MS Press Book where I found fatal errors. Also
              ADO.NET Step By Step has very stupid errors, showing that the writers
              didn't even bother about testing the code they publish.

              Well, it's a bad situation, everyone is thinking only about time-to-market
              and no more about quality of books. Wrox died this year in march so my
              favourite publisher is also gone...

              --
              best regards

              Peter Koen
              -----------------------------------
              MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS

              Comment

              Working...