Hello,
I am working through the Sitepoint book “Build Your Own ASP.NET 3.5 Web Site”. In chapters 10 and 11 the book covers the DataList and GridView.
My project will load data correctly into the list and grid; however, the events are not firing, for example the SelectedIndexChanged event. As far as I can tell no list/grid events are being fired in the code-behind file (the page load event and data binding does occur).
There are some similar posts in these forums and on the internet concerning this. The answers are generally one of these:
1 - Ensure that View State is enabled for the list/grid and up the page heirarchy.
2 - set AutoEventWireup=“true” for the page.
3 - ensure that data binding is done in the Page Load event and is only done when the Page.IsPostBack property is false.
4 - explicitly add an event handler for the list/grid in the aspx (OnSelectedIndexChanged=“Grid_SelectedIndexChanged”). I am using VB so this should not be necessary.
5 - set the autopostback property of the list/grid to true. This property is not available in my project, and I am assuming that the autopostback property is part of an older version of ASP.NET. The book does not refer to it.
Having tried all of these, the events are still not firing. If anyone has suggestions they would be appreciated!
Here is some of the aspx code:
<body enableviewstate=“true”>
<form id=“form1” runat=“server” enableviewstate=“true”>
<div enableviewstate=“true”>
<h1>Address Book</h1>
<asp:GridView ID=“Grid” runat=“server” AutoGenerateColumns=“False”
enableviewstate=“true” OnSelectedIndexChanged=“Grid_SelectedIndexChanged” >
<Columns>
<asp:BoundField DataField=“Name” HeaderText=“Name” />
<asp:BoundField DataField=“City” HeaderText=“City” />
<asp:ButtonField CommandName=“Select” Text=“Select” />
</Columns>
</asp:GridView>
<br />
<asp:Label ID=“SelectedNameLabel” runat=“server” />
</div>
</form>
</body>
</html>
Here is some of the code-behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGrid()
End If
End Sub
Protected Sub Grid_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Grid.SelectedIndexChanged
Dim nIndex As Integer = Grid.SelectedIndex
Dim oRow As GridViewRow = Grid.Rows(nIndex)
Dim sName As String = oRow.Cells(0).Text
SelectedNameLabel.Text = "You Selected " & sName
End Sub
Thank you for your help!