Is EnableViewState required for Events?

P

Paul W

I have a ListBox Webcontrol with an OnSelectedIndexChanged event (and
AutoPostBack=true). If I disable EnableViewState for this control, when I
change the listbox index, the form does a postback but the event does not
fire. If I enable ViewState for the control, the event fires as expected.
Why?

Thanks,

Paul.
 
T

Teemu Keiski

Hi,

Are you sure that you have the event wired correctly? Disabling ViewState
should cause it so that event is raised every time because ViewState is used
to compare the original value from previous request and the posted
(selected) value. And because ViewState is disabled, they differ every time.

Are the items in ListBox added dynamically in code or are they specified
declaratively in aspx?
 
P

Paul W

Yes - it is wired correctly - if the ONLY thing I change is EnableViewState
for the listbox, then this makes the difference.

The items in the listbox are added using Items.Add in VB. I have simplified
the page to its bare minimum (see below) and it still demostrates this
bahavior.

I checked the js it generates and it is correctly
"onchange="__doPostBack('LstDatesCourts','')" . From within Page_load I
checked Request.form("__EventTarget") and it was correctly "LstDatesCourts".

Thanks for any info, I'm stumped.

Paul.
--------------
Public Class ChooseReservationGroupEditTESTT
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents LstDatesCourts As System.Web.UI.WebControls.ListBox

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
ShowList()
End If
End Sub

Sub ShowList()
Dim n As Integer
For n = 1 To 6
LstDatesCourts.Items.Add(n.ToString())
LstDatesCourts.Items(LstDatesCourts.Items.Count - 1).Value =
n.ToString
Next
End Sub

Private Sub LstDatesCourts_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles LstDatesCourts.SelectedIndexChanged
Response.Write("Event was fired")
End Sub
End Class
---------------------------------
Imports System.Data.SqlClient
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="ChooseReservationGroupEditTESTT.aspx.vb"
Inherits="ScheduleA.ChooseReservationGroupEditTESTT" enableViewState="True"
EnableSessionState="False"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>ChooseReservationGroupEdit</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language="javascript">
<!--


//-->
</script>
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="439" border="1"
style="WIDTH: 439px; HEIGHT: 440px"
class="body2" align="center">
<TR>
<TD style="HEIGHT: 64px">
<DIV id="Label1" style="DISPLAY: inline; WIDTH: 408px; HEIGHT: 33px"
runat="server" ms_positioning="FlowLayout"
class="body1">Label</DIV>
</TD>
</TR>
<TR>
<TD style="HEIGHT: 231px">
<P align="center">
<asp:listbox id="LstDatesCourts" runat="server" Width="383px"
Height="200px" AutoPostBack="True"></asp:listbox></P>
</TD>
</TR>
<TR>
<TD style="HEIGHT: 65px">Do you want to edit:
<TABLE id="Table2" style="WIDTH: 424px; HEIGHT: 32px" cellSpacing="1"
cellPadding="1" width="424"
border="0">

</TABLE>
</TD>
</TR>
<TR>
<TD align="left" style="HEIGHT: 70px">
<DIV style="DISPLAY: inline; WIDTH: 424px; FONT-STYLE: italic; HEIGHT:
19px; TEXT-ALIGN: center"
ms_positioning="FlowLayout">Tip: You can click on any of the above
dates to
edit a different booking in this group</DIV>
</TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
 
T

Teemu Keiski

Ah, I see it now.

It's because you add the Items in code and have ViewState disabled, indeed
(I assumed you had items in aspx because you disabled viewstate). Point is
that those ListItems are kept in ViewState when they are added via code
(only the posted one is sent along with the form) and on postback they don't
exist when the selected vs. previous selected check is done. The check finds
out that the selected index is -1 (viewstate is disabled so it returns
always -1) and it finds out which from the items in Items collection would
be the selected one. Since Items do not exist due to ViewState being
disabled, that returns -1 also as the index. And because they (-1 as
Selectedindex, -1 as the posted one) equal, event won't be raised. It would
be raised only when selection and previously selected indexes differ.

On the other hand, as I described in my previous post, if you'd had Items
declared in aspx, then it would change so that the event is raised on every
postback, since SelectedIndex is always -1 but there comes different index
via the post check. So essentially, the way to get the event to work as you
expect it to work (only when selection really changes), is to enable view
state.
 

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,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top