datagrid selectedindexchanged not firing

K

kevin

Hi, I'm working with VS 2005 and Framework 2.0

I have a datagrid with a link and the selectedindexchanged will not
fire. The page posts back but does nothing.

Protected Sub DataGrid1_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
DataGrid1.SelectedIndexChanged

Label2.Text = "what the H"


End Sub

label2 fills with nothing.

I can get ItemCommand to fire

Protected Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.ItemCommand

Label2.Text = "this shows"

End sub

but with itemcommand I can't get

datagrid1.selecteditem.cells(0) when I hit the link in the grid to
pass the data

I get: Object reference not set to an instance of an object. error

any help would be appreciated. Thanks Kevin
 
K

Ken Cox [Microsoft MVP]

Hi Kevin,

Here's a sample that shows both events firing.

Make sure you are checking ispostback in your Load event?

Let us know what you find?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

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

<script runat="server">
Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Protected Sub DataGrid1_SelectedIndexChanged _
(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles DataGrid1.SelectedIndexChanged
Label2.Text = "what the H"
End Sub

Protected Sub DataGrid1_ItemCommand _
(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.ItemCommand
Label2.Text = "this shows the ItemCommand"
End Sub

Function CreateDataSource() As System.Data.DataTable
Dim dt As New System.Data.DataTable
Dim dr As System.Data.DataRow
dt.Columns.Add(New System.Data.DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New System.Data.DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New System.Data.DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New System.Data.DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>SelectedIndexChanged Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datagrid id="DataGrid1" runat="server" >
<columns>
<asp:editcommandcolumn canceltext="Cancel" edittext="Edit"
headertext="Edit item" updatetext="Update">
<itemstyle wrap="False" />
<headerstyle wrap="False" />
</asp:editcommandcolumn>
<asp:buttoncolumn commandname="Select"
text="Select"></asp:buttoncolumn>
</columns>
</asp:datagrid><br />
<asp:label id="Label2" runat="server" text="Label"></asp:label>
</div>
</form>
</body>
</html>
 

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,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top