Solved

Asp.Net: datagrid event is not fired

Posted on 2004-10-28
576 Views
Last Modified: 2010-05-18
Hello experts,
in my Asp.Net (vb.net) web application
I'm using in one web page the following definitions and code:
---
<asp:datagrid id="dgTest" style="Z-INDEX: 103; LEFT: 20px; POSITION: absolute; TOP: 442px"
      runat="server" Width="869px" Height="10px" Font-Size="10pt"
                OnItemCommand="showTestDetails"
      AutoGenerateColumns="False" GridLines="Vertical" HeaderStyle-BackColor="Red" HeaderStyle-ForeColor="White"
      HeaderStyle-Font-Bold="True" HeaderStyle-Font-Name="Verdana" AlternatingItemStyle-BackColor="#dddddd"
      ItemStyle-Font-Name="Verdana" ShowFooter="True"
                DataKeyField="MyKey">

      <FooterStyle Font-Names="Verdana" Font-Bold="True" ForeColor="White" BackColor="Red"></FooterStyle>
      <AlternatingItemStyle BackColor="#DDDDDD"></AlternatingItemStyle>
      <ItemStyle Font-Names="Verdana"></ItemStyle>
      <HeaderStyle Font-Names="Verdana" Font-Bold="True" ForeColor="White" BackColor="Red"></HeaderStyle>

      <Columns>
            <asp:ButtonColumn Text="Close" ButtonType="PushButton" HeaderText="Close"
                                        CommandName="btnClose"></asp:ButtonColumn>
            <asp:BoundColumn DataField="myKey" ReadOnly="True" HeaderText="Key">
                  <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                  <ItemStyle HorizontalAlign="Center" Width="15%"></ItemStyle>
            </asp:BoundColumn>
      </Columns>
</asp:datagrid>

    Private Sub dgTest_ItemCommand(ByVal source As Object, _
              ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgTest.ItemCommand
        Dim itemCell0 As TableCell = e.Item.Cells(0)    ' myKey
        If e.CommandName = "btnClose" Then
            'Me.txtBoxInfo.Text = "You clicked one of the 'Close' buttons!"
        End If
    End Sub

    Sub showTestDetails(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        Dim itemCell0 As TableCell = e.Item.Cells(0)    ' myKey
        If e.CommandName = "btnClose" Then
            'Me.txtBoxInfo.Text = "You clicked one of the 'Close' buttons!"
        End If
    End Sub
---
Clicking the close button 'btnClose' none of the above's routines receives the control.
Why does this happen?
If anyone knows a solution please supply appropriate [snippet] information.

   Thank you very much!

     HStrix
0
Question by:HStrix
    6 Comments
     
    LVL 28

    Expert Comment

    by:mmarinov
    Hi HStrix,

    first of all may be there is a conflict between the 2 subs
    try to remove dgTest_ItemCommand and if there is a addhandler
    second, are you binding the datagrid within if not ispostback then

    Regards!
    B..M
    0
     

    Author Comment

    by:HStrix
    Thank mmarinov,
    - I changed dgTest_ItemCommand  to XXXdgTest_ItemCommand. => result: no change
    - The datagrid is always filled in Page_Load using a subroutine.
    0
     
    LVL 28

    Accepted Solution

    by:
    HStrix,

    if this subroutine is not calling like this

    Sub Page_load
        If Not IsPostBack Then
            call the sub
        End If
    End Sub

    then you will not be able to get the command because when a post back occurs the asp.net modified the state of the datagrid and it does not know that a control was hitted

    B..M
    0
     

    Author Comment

    by:HStrix
    The datagrid is filled as follows:

    Dim objConn As New OleDbConnection
    Dim objAdapter As New System.Data.OleDb.OleDbDataAdapter
    Dim objDataset As New DataSet
    Dim strSQL As String = "Select * From mytable;"
            objAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand(strSQL, objConn)
            ' Fill the dataset.
            objAdapter.Fill(objDataset)
            ' Create a new view.
            Dim objView As New DataView(objDataset.Tables(0))
            ' Set up the data grid and bind the data.
            dgTest.DataSource = objView
            dgTest.DataBind()

    The datagrid is properly filled.
    0
     

    Author Comment

    by:HStrix
    Thank you very much mmarinov,
    that was exactly the problem.
    It is now working.

        HStrix

       
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    HStrix,

    I'm glad that can help

    B..M
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now