Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > VS.NET 2002/2003
|
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 28th, 2003, 08:17 PM
Registered User
 
Join Date: Oct 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting error using OnItemCommand

I am trying to retrieve data from a user clicking on a datagrid
button but I am getting the following error:

Method 'Public Sub dgNewUsersOnItemCommand(s As Object, e As System.Web.UI.WebControls.DataListCommandEventArgs )' does not have the same signature as delegate 'Delegate Sub DataGridCommandEventHandler(source As Object, e As System.Web.UI.WebControls.DataGridCommandEventArgs )'.

The Markup looks like this:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="testdatagrid.aspx.vb" Inherits="testsite.testdatagrid"%>

Head and body tags of course

<form id="Form1" method="post" runat="server">
 <asp:DataGrid id="dgNewUsers"OnItemCommand="dgNewUsersOnItemComm and"
  AutoGenerateColumns="False" BorderWidth="0" BorderStyle="None" runat="server">

            <AlternatingItemStyle BackColor="LightSteelBlue"/>
            <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="LightSlateGray"/>

            <Columns>
                <asp:ButtonColumn Text="Approve Subscription"/>
                <asp:BoundColumn DataField="userid" HeaderText="ID"></asp:BoundColumn>
                <asp:BoundColumn DataField="username" HeaderText="User Name"></asp:BoundColumn>
                <asp:BoundColumn DataField="firstname" HeaderText="First Name"></asp:BoundColumn>
                <asp:BoundColumn DataField="middleinitial" HeaderText="Middle Initial"></asp:BoundColumn>
                <asp:BoundColumn DataField="lastname" HeaderText="Last Name"></asp:BoundColumn>
                <asp:BoundColumn DataField="levelidpicked" HeaderText="Level Picked"> </asp:BoundColumn>
                <asp:BoundColumn DataField="lastlogin" HeaderText="Last Login Date"></asp:BoundColumn>
                <asp:BoundColumn DataField="emailaddress" HeaderText="Email"></asp:BoundColumn>
            </Columns>

            </asp:DataGrid>
        </form>

The code that is in the aspx.vb file looks like this:

Imports System.Data.SqlClient

Public Class testdatagrid
    Inherits System.Web.UI.Page

    Dim con As New SqlConnection( connection string)


    Protected WithEvents dgNewUsers As System.Web.UI.WebControls.DataGrid

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            getnewusers()
        End If
    End Sub

    Sub getnewusers()
        Dim cmd As New SqlCommand("exec sp_get_users 1", con)
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet

        Try
            If con.State = ConnectionState.Open Then
                con.Close()
            End If

            da.SelectCommand = cmd
            da.Fill(ds)
            con.Close()
        Catch ex As Exception
            Throw ex
        End Try

        dgNewUsers.DataSource = ds
        dgNewUsers.DataBind()
    End Sub

    Sub dgNewUsersOnItemCommand(ByVal s As Object, ByVal e As DataListCommandEventArgs)
        Response.Write("Helpme")
    End Sub
End Class

When I put the sub dgNewUsersOnItemCommand on the markup page between the
<script language=vb runat=server></script> tags it works like a champ. But I cannot get this to run in the .vb file. I know I am missing something obvious.

TIA,
Cowa


 
Old October 29th, 2003, 11:46 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

When you have event handlers in the code behind, you need to specify what the handler "Handles". Note the "Handles MyBase.Load" on the Page_Load method.

Sub dgNewUsersOnItemCommand(ByVal s As Object, ByVal e As DataListCommandEventArgs) Handles dgNewUsers.ItemCommand

The keyword WithEvents permits this syntax. It makes the ASPX code much cleaner because you don't have to specify the handlers in the control's HTML (you should remove it from the <asp:DataGrid...> tag).

Peter
 
Old October 29th, 2003, 11:52 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Also,

When you specify an event for a handler to handle (using "Handles ...", you'll get the pre-compile warns about your error. I think your actual error has to do with the function signature.

One thing that makes it VERY easy to set up these handlers is to look at the current document (under the document tabs). There you will find two drop downs. On the left you can choose what control you want to work with. Once you have a datagrid on the page and code-behind, you'll be able to choose it. Then in the right drop down, you can choose an even you want to set up a handler for. VS will draw the function with the correct signature to match the event delegate. Then you just need to add the guts of the method.

Peter
 
Old October 29th, 2003, 10:31 PM
Registered User
 
Join Date: Oct 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

:D Thanks planoie!! That worked like a champ.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Query Error & Run-Time Error 3022 DavidWE Access 1 July 31st, 2008 11:17 AM
Ch 4: Parse error: syntax error, unexpected T_SL hanizar77 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 June 23rd, 2008 09:17 PM
[Resolved] Error calling a sp - parameter error snufse .NET Framework 2.0 2 February 12th, 2008 04:46 PM
onsortcomman and onitemcommand s15199d ASP.NET 2.0 Professional 0 October 12th, 2006 10:26 AM
Urgent: Repeater's OnItemCommand Problem gheibia General .NET 1 January 18th, 2006 08:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.