cannot get value of dropdownlist from postback

Discussion in 'ASP .Net' started by Kevin Blount, Sep 6, 2006.

  1. Kevin Blount

    Kevin Blount Guest

    I have a test script right now that I'm fighting with. The idea is to
    "simply" have an aspx page with 3 panels, to show 3 "different" forms
    and then a 4th panel to show the results of processing these forms.

    One of the panels will contain DropDownLists, who's items are
    dynamically added in terms of text and values. For example, one of these
    lists will be languages. I want to list presented in the language of the
    person viewing it, i.e. if they are visiting out French website, they
    see "Fran├žais", but if they are on our UK website they see "French". The
    value will always be "French".

    The problem I'm find is that when the panel with the DropDownList is not
    visible, then I cannot access the .Text property of the list. In the
    code below, I have a DropDownList called "testList", and right at the
    end of the code I do a Response.Write to show testList.Text; but this
    will only work when panel3.Visible = true.

    Naturally this isn't going to work, as if my 3 forms (in panels 1, 2 &
    3) are going to work properly, they must be hidden until required, and
    once the form fields on panel3 are completed, they should all be hidden.


    Is there any way to change the code below to make this work even if the
    panel holding the DropDownList is hidden?

    Many thanks


    Here's the code:

    <body>
    <script runat="server">
    public string usernameString = string.Empty;

    private void Page_Load(object sender, EventArgs e)
    {
    usernameString = Request["username"];

    hideAllPanels();

    if (!IsPostBack)
    {
    panel1.Visible = true;
    }
    }

    private void button_click(object sender,
    System.Web.UI.WebControls.CommandEventArgs e)
    {
    if (e.CommandName == "show_panel2")
    {
    panel2.Visible = true;
    }
    else if(e.CommandName == "show_panel3")
    {
    panel3.Visible = true;
    }
    else if(e.CommandName == "show_panel4")
    {
    panel3.Visible = false;
    panel4.Visible = true;
    }
    }

    private void hideAllPanels() {
    panel1.Visible = false;
    panel2.Visible = false;
    panel3.Visible = false;
    panel4.Visible = false;
    }
    </script>
    <%

    %>
    <form action="" method="post" name="pageform" id="pageform" runat="server">
    <asp:panel ID="panel1" runat="server" Wrap="true">
    <h2>Panel 1</h2>
    <asp:TextBox ID="username" runat="server" />
    <asp:RequiredFieldValidator id="RequiredFieldValidator1"
    CssClass="smallFontRed" runat="server" Height="8px" ErrorMessage="This
    field is required"
    ControlToValidate="username"></asp:RequiredFieldValidator>
    <asp:Button ID="show_panel2" CommandName="show_panel2"
    OnCommand="button_click" runat="server" Text="Next >" />
    </asp:panel>


    <asp:panel ID="panel2" runat="server" Wrap="true">
    <h2>Panel 2</h2>
    <asp:CheckBox ID="textCheckBox" runat="server" Value="as test"
    Text="booyah" />
    <asp:Button ID="show_panel3" CommandName="show_panel3"
    OnCommand="button_click" runat="server" Text="Next >" />
    </asp:panel>

    <asp:panel ID="panel3" runat="server" Wrap="true">
    <h2>Panel 3</h2>
    <%
    ListItem languageSelectItem = new ListItem("TextToAppear","ValueToUse");
    textList.Items.Add(languageSelectItem);
    %>
    <asp:DropDownList ID="textList" runat="server"
    EnableViewState="false"></asp:DropDownList>
    <asp:Button ID="show_panel4" CommandName="show_panel4"
    OnCommand="button_click" runat="server" Text="Finish" />
    </asp:panel>

    <asp:panel ID="panel4" runat="server" Wrap="true">
    <%
    Response.Write(username.Text + " - " + textCheckBox.Text + " - " +
    textList.Text);
    %>
    </asp:panel>
    </form>

    </body>
     
    Kevin Blount, Sep 6, 2006
    #1
    1. Advertising

  2. Kevin Blount

    Jon Paal Guest

    perhaps you could use an event handler to pick up the droplist value selected and assign it to a page level variable then you could
    reference that variable instead of the droplist for later usage.


    "Kevin Blount" <> wrote in message news:...
    >I have a test script right now that I'm fighting with. The idea is to "simply" have an aspx page with 3 panels, to show 3
    >"different" forms and then a 4th panel to show the results of processing these forms.
    >
    > One of the panels will contain DropDownLists, who's items are dynamically added in terms of text and values. For example, one of
    > these lists will be languages. I want to list presented in the language of the person viewing it, i.e. if they are visiting out
    > French website, they see "Fran├žais", but if they are on our UK website they see "French". The value will always be "French".
    >
    > The problem I'm find is that when the panel with the DropDownList is not visible, then I cannot access the .Text property of the
    > list. In the code below, I have a DropDownList called "testList", and right at the end of the code I do a Response.Write to show
    > testList.Text; but this will only work when panel3.Visible = true.
    >
    > Naturally this isn't going to work, as if my 3 forms (in panels 1, 2 & 3) are going to work properly, they must be hidden until
    > required, and once the form fields on panel3 are completed, they should all be hidden.
    >
    >
    > Is there any way to change the code below to make this work even if the panel holding the DropDownList is hidden?
    >
    > Many thanks
    >
    >
    > Here's the code:
    >
    > <body>
    > <script runat="server">
    > public string usernameString = string.Empty;
    >
    > private void Page_Load(object sender, EventArgs e)
    > {
    > usernameString = Request["username"];
    >
    > hideAllPanels();
    >
    > if (!IsPostBack)
    > {
    > panel1.Visible = true;
    > }
    > }
    >
    > private void button_click(object sender, System.Web.UI.WebControls.CommandEventArgs e)
    > {
    > if (e.CommandName == "show_panel2")
    > {
    > panel2.Visible = true;
    > }
    > else if(e.CommandName == "show_panel3")
    > {
    > panel3.Visible = true;
    > }
    > else if(e.CommandName == "show_panel4")
    > {
    > panel3.Visible = false;
    > panel4.Visible = true;
    > }
    > }
    > private void hideAllPanels() {
    > panel1.Visible = false;
    > panel2.Visible = false;
    > panel3.Visible = false;
    > panel4.Visible = false;
    > }
    > </script>
    > <%
    >
    > %>
    > <form action="" method="post" name="pageform" id="pageform" runat="server">
    > <asp:panel ID="panel1" runat="server" Wrap="true">
    > <h2>Panel 1</h2>
    > <asp:TextBox ID="username" runat="server" />
    > <asp:RequiredFieldValidator id="RequiredFieldValidator1" CssClass="smallFontRed" runat="server" Height="8px" ErrorMessage="This
    > field is required" ControlToValidate="username"></asp:RequiredFieldValidator>
    > <asp:Button ID="show_panel2" CommandName="show_panel2" OnCommand="button_click" runat="server" Text="Next >" />
    > </asp:panel>
    >
    >
    > <asp:panel ID="panel2" runat="server" Wrap="true">
    > <h2>Panel 2</h2>
    > <asp:CheckBox ID="textCheckBox" runat="server" Value="as test" Text="booyah" />
    > <asp:Button ID="show_panel3" CommandName="show_panel3" OnCommand="button_click" runat="server" Text="Next >" />
    > </asp:panel>
    >
    > <asp:panel ID="panel3" runat="server" Wrap="true">
    > <h2>Panel 3</h2>
    > <%
    > ListItem languageSelectItem = new ListItem("TextToAppear","ValueToUse");
    > textList.Items.Add(languageSelectItem);
    > %>
    > <asp:DropDownList ID="textList" runat="server" EnableViewState="false"></asp:DropDownList>
    > <asp:Button ID="show_panel4" CommandName="show_panel4" OnCommand="button_click" runat="server" Text="Finish" />
    > </asp:panel>
    >
    > <asp:panel ID="panel4" runat="server" Wrap="true">
    > <%
    > Response.Write(username.Text + " - " + textCheckBox.Text + " - " + textList.Text);
    > %>
    > </asp:panel>
    > </form>
    >
    > </body>
     
    Jon Paal, Sep 7, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Celine
    Replies:
    3
    Views:
    19,045
    James Thomas
    Sep 3, 2004
  2. John Blair
    Replies:
    2
    Views:
    7,874
    John Blair
    Aug 2, 2005
  3. =?Utf-8?B?R2VvZmZyZXkgdmFuIGRlbiBPdWRlbg==?=

    Dropdownlist value is same as text after postback and in HTML (2.0

    =?Utf-8?B?R2VvZmZyZXkgdmFuIGRlbiBPdWRlbg==?=, Mar 12, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    874
    =?Utf-8?B?RWx0b24gVw==?=
    Mar 13, 2006
  4. Replies:
    0
    Views:
    593
  5. Felix Chan

    ListBox inside a WebControl cannot get postback value.

    Felix Chan, Sep 30, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    271
    rcarboni
    Oct 1, 2003
Loading...

Share This Page