Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Problem with the State of a DropDownList

Hi,
I have got a problem to keep the state of a DropDownList.
My page is organized this way:
- 1 MasterPage with :
- 1 DropDownList
- 1 Menu
- 1 Content
- X pages displayed into the Content when clicking on the menu
When I am clicking on the menu, the PostBack refresh the DropDownList to the SelectedIndex 0.
I want to use the selected value in the ContentPages.
I have tried with the ViewState and with cookies with no success (I guess I am using it the wrong way).
Many thanks for your help.

-- Olivier --
[567 byte] By [destroy] at [2007-11-11 8:08:43]
# 1 Re: Problem with the State of a DropDownList
A dropdownlist should automatically remember its selected value across postbacks. Do you have code to populate the list in your Page_Load or other event? If so, you should check the Page.IsPostBack property and only load the list if the property is False.
Phil Weber at 2007-11-11 23:13:23 >
# 2 Re: Problem with the State of a DropDownList
Hi Phil Weber,
When I am doing PostBack via ButtonClick or SelectedIndexChanged, the state stays . It is when I am doing the PostBack with the menu that the state is refreshed.
What is wrong?
Thanks.
-- Olivier --
destroy at 2007-11-11 23:14:23 >
# 3 Re: Problem with the State of a DropDownList
Here is my code,

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>MY PAGE</title>
</head>
<body>
<form id="form1" runat="server">

<!-- Zone : Header -->
<div class="header">
<!-- Zone : Direct Access -->
<div class="directaccess">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="DB1" DataTextField="Name" DataValueField="ID" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="DB1" runat="server" ConnectionString="<%$ ConnectionStrings:csDB1 %>"
SelectCommand="SELECT [ID], [Name] FROM [Server]"></asp:SqlDataSource>
</div>
</div>
<div class="page">
<div id="details" style="right: 0px; top: 0px">
<div id="menu">
<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1" BackColor="#F7F6F3" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="1em" ForeColor="#7C6F57" Orientation="Horizontal" StaticSubMenuIndent="10px">
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicHoverStyle BackColor="#7C6F57" ForeColor="White" />
<DynamicMenuStyle BackColor="#F7F6F3" />
<StaticSelectedStyle BackColor="#5D7B9D" />
<DynamicSelectedStyle BackColor="#5D7B9D" />
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<StaticHoverStyle BackColor="#7C6F57" ForeColor="White" />
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" />
</div>
<div id="content">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
</div>
</form>
</body>
</html>

Many thanks.

-- Olivier --
destroy at 2007-11-11 23:15:29 >