cannot get value of dropdownlist from postback

K

Kevin Blount

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>
 
J

Jon Paal

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 said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top