datagrid selectedindexchanged not firing

Discussion in 'ASP .Net' started by kevin@webpageconcepts.com, Jul 26, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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>

    <> wrote in message
    news:...
    > 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
    >
     
    Ken Cox [Microsoft MVP], Jul 26, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: SelectedIndexChanged event not firing

    Kevin Spencer, Aug 1, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    713
    Joe Bleaux
    Aug 2, 2003
  2. Severin
    Replies:
    0
    Views:
    535
    Severin
    Aug 4, 2003
  3. DesignerX
    Replies:
    2
    Views:
    3,889
    Bob Carsto
    Nov 14, 2003
  4. Paul Lacey
    Replies:
    2
    Views:
    2,730
    =?Utf-8?B?TmlrYW5kZXIgJiBNYXJncmlldCBCcnVnZ2VtYW4=
    Feb 17, 2005
  5. John Blair
    Replies:
    6
    Views:
    294
    Alessandro Zifiglio
    Jun 29, 2006
Loading...

Share This Page