EnableViewState And DROPDOWNLIST

T

Tom

<%@ Page Language="vb" AutoEventWireup="false"
EnableViewState="false" .... %>

<FORM id="frmMyForm" method="post" runat="server">
1) Inline Items (In aspx File) :
<ASP:DROPDOWNLIST id="drpStatesOne" runat="server"
enableviewstate="false">
<ASP:LISTITEM value="1">A</ASP:LISTITEM>
<ASP:LISTITEM value="2">B</ASP:LISTITEM>
<ASP:LISTITEM value="3">C</ASP:LISTITEM>
<ASP:LISTITEM value="4">D</ASP:LISTITEM>
</ASP:DROPDOWNLIST>
<BR>
2) Items added in Page_Init Event :
<ASP:DROPDOWNLIST id="drpStatesTwo" runat="server"
enableviewstate="false">
</ASP:DROPDOWNLIST>
<BR>
3) Items added in Page_Load Event :
<ASP:DROPDOWNLIST id="drpStatesThree" runat="server"
enableviewstate="false">
</ASP:DROPDOWNLIST>
<BR>
<INPUT type=submit value="Check It">
</FORM>

Code Behind ....

Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
With drpStatesTwo
.Items.Add("A")
.Items.Add("B")
.Items.Add("C")
.Items.Add("D")
End With
End Sub

Protected drpStatesOne As
System.Web.UI.WebControls.DropDownList
Protected drpStatesTwo As
System.Web.UI.WebControls.DropDownList
Protected drpStatesThree As
System.Web.UI.WebControls.DropDownList
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
With drpStatesThree
.Items.Add("A")
.Items.Add("B")
.Items.Add("C")
.Items.Add("D")
End With
End Sub
 
T

Tom

the problem ....
(sorry in last post i just put code)
Every where the EnableViewState property is set
to "false", but still the state (The Item user selects
remain selected even after submitting the form) for first
and second dropdown list.
Expert comments needed.
Regards,
 
Joined
Sep 24, 2008
Messages
3
Reaction score
0
Your Page_Load will get fired before your event. i.e your event is the last method to get fired.

So when a post back occurs your DropdownList is reset in Page_Load every time. By the time your event gets fired the selected index has not been changed since the DropdownList constructor was called.

what you need to do is when binding put the if(!Page.IsPostBack) conditional around around your databinding code.

However with ViewState disabled your evet will still not get fired and if it does your DropdownList will not regester a SelectedIndex change.

the best option is to overload your dropdownlist, intercept it at the RaisePostBack event stage to capture the selectedVale when the dropdownlist selected index is changed.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top