By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,300 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,300 IT Pros & Developers. It's quick & easy.

datagrid selectedindexchanged not firing

P: n/a
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

Jul 26 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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>

<ke***@webpageconcepts.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
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

Jul 26 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.