Custom Button Event in a ListView

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 18326 Views - Last Post: 15 May 2010 - 11:07 AM Rate Topic: -----

#1 KCross  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 03-March 10

Custom Button Event in a ListView

Posted 04 March 2010 - 04:48 PM

Hi all...

I'm hoping this question isn't as complicated as I'm assuming it is...

I have a ListView that has one-way data binding to a SQL DB. I have put a button control in the <EditItemTemplate> section. The button CommandName is set to Update.

From what I understand, I need to modify the event to basically say, on a Button Click event, if CommandName="Update" then [do x].

I'm kind of stuck here, though. I can't create a button click event in the traditional way.

Can anyone point me to some very beginner friendly documentation on how this works?

Is This A Good Question/Topic? 0
  • +

Replies To: Custom Button Event in a ListView

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 06:00 AM

With your CommandName being "Update", you can use the ItemUpdating event. Inside of that event, you use the FindControl method to find the individual controls.

Here is some sample code.

protected void lvDefectCodes_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
    // this is used so that the control does NOT fire the "UpdateMethod" event for the binding source.
    // Personally, I use very little binding.  I like to the updating with my own code.
    // Because of this, I use the ItemUpdating event, which is fired BEFORE the "UpdateMethod" event of
    //     binding source.
    e.Cancel = true;

    lblInformation.Text = "";

    HiddenField lblDefectCodeID = (lvDefectCodes.Items[e.ItemIndex].FindControl("lblDefectCodeID") as HiddenField);
    TextBox txtDefectCode = (lvDefectCodes.Items[e.ItemIndex].FindControl("txtDefectCode") as TextBox);
    TextBox txtDefectDescription = (lvDefectCodes.Items[e.ItemIndex].FindControl("txtDefectDescription") as TextBox);
    TextBox txtBackgroundColor = (lvDefectCodes.Items[e.ItemIndex].FindControl("txtBackgroundColor") as TextBox);
    TextBox txtTextColor = (lvDefectCodes.Items[e.ItemIndex].FindControl("txtTextColor") as TextBox);

    if (txtDefectCode == null)
        return;

    // some code to add the data to a database.

    lvDefectCodes.EditIndex = -1;
    lvDefectCodes.InsertItemPosition = InsertItemPosition.None;
}



In my code, I have 4 TextBoxes that the user could type in and a HiddenField that contains an ID. I use the FindControl method to get the controls. From there, I simply use the controls as normal.
Was This Post Helpful? 0
  • +
  • -

#3 KCross  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 03-March 10

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 07:29 AM

This looks awesome. I'm trying it, and I'm missing something obvious.

When I click the "Update" buttom (that is, the button to commit the changes - the one that should trigger the "Update" event), all it does on postback is the Page_Load event (which is empty).

I'm not certain how to get the button to use that ItemUpdating event of the listview control... I think I put my code in the wrong place. This is what my .cs page looks like at the moment:

public partial class BBCheckIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            e.Cancel = true;

            CheckBox checkin = ListView1.Items[e.ItemIndex].FindControl("CheckBox1") as CheckBox;
            if (checkin.Checked)
            {
                string Available = "Available";
                Debug.WriteLine(checkin + " is " + Available);
            }
        }
    }

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 08:29 AM

If you put a breakpoint at the e.Cancel = true line, does the breakpoint get hit?
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 08:32 AM

ItemUpdating is not the correct event to be using with a button control in a ListView, this is used for two-way binding of the ListView.

You should be using the ItemCommand event. That is what it is designed for.

ListView.ItemCommand
Was This Post Helpful? 0
  • +
  • -

#6 KCross  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 03-March 10

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 08:56 AM

View Posteclipsed4utoo, on 05 March 2010 - 07:29 AM, said:

If you put a breakpoint at the e.Cancel = true line, does the breakpoint get hit?


It does not.

View PostJayman, on 05 March 2010 - 07:32 AM, said:

ItemUpdating is not the correct event to be using with a button control in a ListView, this is used for two-way binding of the ListView.

You should be using the ItemCommand event. That is what it is designed for.

ListView.ItemCommand


I tried this:

protected void ListView1_OnItemCommand(object sender, ListViewUpdateEventArgs e)
        {
            e.Cancel = true;

            CheckBox checkin = ListView1.Items[e.ItemIndex].FindControl("CheckBox1") as CheckBox;
            if (checkin.Checked)
            {
                string Available = "Available";
                Debug.WriteLine(checkin + " is " + Available);
            }
        }


It still doesn't touch this event. All it does is Page_Load.
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 08:59 AM

You are going to need to post your code in its entirety, including the markup for the page.

I need to see exactly how you have defined the controls on the page and how you are handling the events.
Was This Post Helpful? 0
  • +
  • -

#8 KCross  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 03-March 10

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 09:06 AM

View PostJayman, on 05 March 2010 - 07:59 AM, said:

You are going to need to post your code in its entirety, including the markup for the page.

I need to see exactly how you have defined the controls on the page and how you are handling the events.


Certainly! I just didn't want to bloat up a post with needless info.

Here we go!

BBCheckIn.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="BBCheckIn.aspx.cs" Inherits="Loaner_Inventory.BBCheckIn" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

        <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlBBInUse">
        <LayoutTemplate>
       <h3> Please select a BlackBerry to check in:</h3>
       Sort by: 
            <asp:LinkButton ID="LinkButton1" runat="server" Text="Phone Number" CommandName="Sort" CommandArgument="PhoneNumber"></asp:LinkButton> | 
            <asp:LinkButton ID="LinkButton2" runat="server" Text="Checkout Date" CommandName="Sort" CommandArgument="CheckOutDate"></asp:LinkButton> |
            <asp:LinkButton ID="LinkButton3" runat="server" Text="User" CommandName="Sort" CommandArgument="CurrentUser"></asp:LinkButton><br />
       <br />
       <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
        </LayoutTemplate>

        <ItemTemplate>
        
        PIN: <asp:Label ID="lblPIN" runat="server" Text='<%# Eval("PIN") %>' Font-Names="Calibri"></asp:Label><br />
        Phone Number: <asp:Label ID="lblPhoneNumber" runat="server" Text='<%# Eval("PhoneNumber") %>' Font-Names="Calibri"></asp:Label><br />
        Model: <asp:Label ID="lblModel" runat="server" Text='<%# Eval("Model") %>' Font-Names="Calibri"></asp:Label><br />
        Current User: <asp:Label ID="lblUser" runat="server" Text='<%# Eval("CurrentUser") %>' Font-Names="Calibri"></asp:Label><br />
        Checkout Date: <asp:Label ID="lblDateOut" runat="server" Text='<%# Eval("CheckOutDate") %>' Font-Names="Calibri"></asp:Label><br />
        <asp:Button ID="btnCheckIn" runat="server" Text="Check In" CommandName="Edit"/>
        </ItemTemplate>

        <ItemSeparatorTemplate><hr /></ItemSeparatorTemplate>

        <EditItemTemplate>
        PIN: <asp:Label ID="lblPIN" runat="server" Text ='<%# Eval("PIN") %>' Font-Names="Calibri"></asp:Label><br />
        Phone Number: <asp:Label ID="lblPhoneNumber" runat="server" Text='<%# Eval("PhoneNumber") %>' Font-Names="Calibri"></asp:Label><br />
        Model: <asp:Label ID="lblModel" runat="server" Text='<%# Eval("Model") %>' Font-Names="Calibri"></asp:Label><br />
        Current User: <asp:Label ID="lblUser" runat="server" Text='<%# Eval("CurrentUser") %>'></asp:Label><br />
        Checkout Date: <asp:Label ID="lblDateOut" runat="server" Text='<%# Eval("CheckOutDate") %>' Font-Names="Calibri"></asp:Label><br />
        <br />
        Please check the box below and click to confirm.
        <br />
        <br />
        <asp:CheckBox ID="CheckBox1" runat="server" Text="Check In" /><br />
        <asp:Button ID="btnEditConfirm" runat="server" Text="Confirm" CommandName="Update" /><br />
        </EditItemTemplate>
        </asp:ListView>
        <p>
   
        <asp:SqlDataSource ID="SqlBBInUse" runat="server" 
            ConnectionString="<%$ ConnectionStrings:LoanerInventoryConnectionString %>" 
            DeleteCommand="DELETE FROM [BlackBerry] WHERE [PIN] = @PIN" 
            InsertCommand="INSERT INTO [BlackBerry] ([PIN], [PhoneNumber], [Model], [OS], [Carrier], [Status], [CurrentUser], [CheckOutDate]) VALUES (@PIN, @PhoneNumber, @Model, @OS, @Carrier, @Status, @CurrentUser, @CheckOutDate)" 
            SelectCommand="SELECT * FROM [BlackBerry] WHERE ([Status] = @Status)" 
            UpdateCommand="UPDATE [BlackBerry] SET [PhoneNumber] = @PhoneNumber, [Model] = @Model, [OS] = @OS, [Carrier] = @Carrier, [Status] = @Status, [CurrentUser] = @CurrentUser, [CheckOutDate] = @CheckOutDate WHERE [PIN] = @PIN">
            <DeleteParameters>
                <asp:Parameter Name="PIN" Type="String" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="PIN" Type="String" />
                <asp:Parameter Name="PhoneNumber" Type="String" />
                <asp:Parameter Name="Model" Type="String" />
                <asp:Parameter Name="OS" Type="String" />
                <asp:Parameter Name="Carrier" Type="String" />
                <asp:Parameter Name="Status" Type="String" />
                <asp:Parameter Name="CurrentUser" Type="String" />
                <asp:Parameter DbType="Date" Name="CheckOutDate" />
            </InsertParameters>
            <SelectParameters>
                <asp:QueryStringParameter DefaultValue="In Use" Name="Status" 
                    QueryStringField="Status" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="PhoneNumber" Type="String" />
                <asp:Parameter Name="Model" Type="String" />
                <asp:Parameter Name="OS" Type="String" />
                <asp:Parameter Name="Carrier" Type="String" />
                <asp:Parameter Name="Status" Type="String" />
                <asp:Parameter Name="CurrentUser" Type="String" />
                <asp:Parameter DbType="Date" Name="CheckOutDate" />
                <asp:Parameter Name="PIN" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
            <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" 
                PageSize="3">
                <Fields>
                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                        ShowLastPageButton="True" />
                </Fields>
            </asp:DataPager>
    </p>
    </asp:Content>



BBCheckIn.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

namespace Loaner_Inventory
{
    public partial class BBCheckIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ListView1_OnItemCommand(object sender, ListViewUpdateEventArgs e)
        {
            e.Cancel = true;

            CheckBox checkin = ListView1.Items[e.ItemIndex].FindControl("CheckBox1") as CheckBox;
            if (checkin.Checked)
            {
                string Available = "Available";
                Debug.WriteLine(checkin + " is " + Available);
            }
        }
    }
}


Main.master.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loaner_Inventory
{
    public partial class Main : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}


I included Main.master.cs simply because on a postback, it does the Page_Load event of BBCheckIn.apsx.cs, then Main.master.cs. I wanted to include all code that is processed during run-time.

I didn't include Main.master... Do you need to see that as well?
Was This Post Helpful? 0
  • +
  • -

#9 KCross  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 03-March 10

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 09:12 AM

I just noticed that I don't have OnItemCommand="ListView1_OnItemCommand" in my <asp:ListView> tag...

Does this mean on every single command, it will use that event? So I need to write a custom event for all actions that could occur within the ListView?

When I try to use OnItemCommand="ListView1_OnItemCommand" (or "ListView1_ItemCommand") I get this:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0123: No overload for 'ListView1_ItemCommand' matches delegate 'System.EventHandler<System.Web.UI.WebControls.ListViewCommandEventArgs>'

Source Error:

 

Line 4:  <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
Line 5:  
Line 6:          <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlBBInUse" OnItemCommand="ListView1_ItemCommand">
Line 7:          <LayoutTemplate>
Line 8:         <h3> Please select a BlackBerry to check in:</h3>
 

Source File: c:\Users\kcross\Documents\Visual Studio 2010\Projects\Loaner Inventory\Loaner Inventory\BlackBerry\BBCheckIn.aspx    Line: 6 



Strangely enough, when I change that to OnItemUpdating="ListView1_ItemUpdating" it works perfectly.

I'm renaming
 protected void ListView1_OnItemCommand(object sender, ListViewUpdateEventArgs e) 

to what I have in quotes for each attempt.

Why is OnItemUpdating not correct?

This post has been edited by KCross: 05 March 2010 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 10:53 AM

The signature of the event should be this..

protected void lvDefectCodes_ItemCommand(object sender, ListViewCommandEventArgs e)
{

}


Was This Post Helpful? 1
  • +
  • -

#11 KCross  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 03-March 10

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 11:15 AM

View Posteclipsed4utoo, on 05 March 2010 - 09:53 AM, said:

The signature of the event should be this..

protected void lvDefectCodes_ItemCommand(object sender, ListViewCommandEventArgs e)
{

}



Yes, I changed that signature upon each iteration of code I attempted. It always matched the <ListView OnCommandCommand="..." string. I realize the code I posted didn't match, because it was from 2 different attempts.

For consistencies sake, here is the code from 1 attempt:

aspx page:
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="BBCheckIn.aspx.cs" Inherits="Loaner_Inventory.BBCheckIn" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

        <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlBBInUse" OnItemCommand="ListView1_ItemCommand">
        <LayoutTemplate>
       <h3> Please select a BlackBerry to check in:</h3>
       Sort by: 
            <asp:LinkButton ID="LinkButton1" runat="server" Text="Phone Number" CommandName="Sort" CommandArgument="PhoneNumber"></asp:LinkButton> | 
            <asp:LinkButton ID="LinkButton2" runat="server" Text="Checkout Date" CommandName="Sort" CommandArgument="CheckOutDate"></asp:LinkButton> |
            <asp:LinkButton ID="LinkButton3" runat="server" Text="User" CommandName="Sort" CommandArgument="CurrentUser"></asp:LinkButton><br />
       <br />
       <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
        </LayoutTemplate>

        <ItemTemplate>
        
        PIN: <asp:Label ID="lblPIN" runat="server" Text='<%# Eval("PIN") %>' Font-Names="Calibri"></asp:Label><br />
        Phone Number: <asp:Label ID="lblPhoneNumber" runat="server" Text='<%# Eval("PhoneNumber") %>' Font-Names="Calibri"></asp:Label><br />
        Model: <asp:Label ID="lblModel" runat="server" Text='<%# Eval("Model") %>' Font-Names="Calibri"></asp:Label><br />
        Current User: <asp:Label ID="lblUser" runat="server" Text='<%# Eval("CurrentUser") %>' Font-Names="Calibri"></asp:Label><br />
        Checkout Date: <asp:Label ID="lblDateOut" runat="server" Text='<%# Eval("CheckOutDate") %>' Font-Names="Calibri"></asp:Label><br />
        <asp:Button ID="btnCheckIn" runat="server" Text="Check In" CommandName="Edit"/>
        </ItemTemplate>

        <ItemSeparatorTemplate><hr /></ItemSeparatorTemplate>

        <EditItemTemplate>
        PIN: <asp:Label ID="lblPIN" runat="server" Text ='<%# Eval("PIN") %>' Font-Names="Calibri"></asp:Label><br />
        Phone Number: <asp:Label ID="lblPhoneNumber" runat="server" Text='<%# Eval("PhoneNumber") %>' Font-Names="Calibri"></asp:Label><br />
        Model: <asp:Label ID="lblModel" runat="server" Text='<%# Eval("Model") %>' Font-Names="Calibri"></asp:Label><br />
        Current User: <asp:Label ID="lblUser" runat="server" Text='<%# Eval("CurrentUser") %>'></asp:Label><br />
        Checkout Date: <asp:Label ID="lblDateOut" runat="server" Text='<%# Eval("CheckOutDate") %>' Font-Names="Calibri"></asp:Label><br />
        <br />
        Please check the box below and click to confirm.
        <br />
        <br />
        <asp:CheckBox ID="CheckBox1" runat="server" Text="Check In" /><br />
        <asp:Button ID="btnEditConfirm" runat="server" Text="Confirm" CommandName="Update" /><br />
        </EditItemTemplate>
        </asp:ListView>
        <p>
   
        <asp:SqlDataSource ID="SqlBBInUse" runat="server" 
            ConnectionString="<%$ ConnectionStrings:LoanerInventoryConnectionString %>" 
            DeleteCommand="DELETE FROM [BlackBerry] WHERE [PIN] = @PIN" 
            InsertCommand="INSERT INTO [BlackBerry] ([PIN], [PhoneNumber], [Model], [OS], [Carrier], [Status], [CurrentUser], [CheckOutDate]) VALUES (@PIN, @PhoneNumber, @Model, @OS, @Carrier, @Status, @CurrentUser, @CheckOutDate)" 
            SelectCommand="SELECT * FROM [BlackBerry] WHERE ([Status] = @Status)" 
            UpdateCommand="UPDATE [BlackBerry] SET [PhoneNumber] = @PhoneNumber, [Model] = @Model, [OS] = @OS, [Carrier] = @Carrier, [Status] = @Status, [CurrentUser] = @CurrentUser, [CheckOutDate] = @CheckOutDate WHERE [PIN] = @PIN">
            <DeleteParameters>
                <asp:Parameter Name="PIN" Type="String" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="PIN" Type="String" />
                <asp:Parameter Name="PhoneNumber" Type="String" />
                <asp:Parameter Name="Model" Type="String" />
                <asp:Parameter Name="OS" Type="String" />
                <asp:Parameter Name="Carrier" Type="String" />
                <asp:Parameter Name="Status" Type="String" />
                <asp:Parameter Name="CurrentUser" Type="String" />
                <asp:Parameter DbType="Date" Name="CheckOutDate" />
            </InsertParameters>
            <SelectParameters>
                <asp:QueryStringParameter DefaultValue="In Use" Name="Status" 
                    QueryStringField="Status" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="PhoneNumber" Type="String" />
                <asp:Parameter Name="Model" Type="String" />
                <asp:Parameter Name="OS" Type="String" />
                <asp:Parameter Name="Carrier" Type="String" />
                <asp:Parameter Name="Status" Type="String" />
                <asp:Parameter Name="CurrentUser" Type="String" />
                <asp:Parameter DbType="Date" Name="CheckOutDate" />
                <asp:Parameter Name="PIN" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
            <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" 
                PageSize="3">
                <Fields>
                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                        ShowLastPageButton="True" />
                </Fields>
            </asp:DataPager>
    </p>
    </asp:Content>



cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Data.SqlClient;

namespace Loaner_Inventory
{
    public partial class BBCheckIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ListView1_ItemCommand(object sender, ListViewUpdateEventArgs e)
        {
            e.Cancel = true;

            CheckBox checkin = ListView1.Items[e.ItemIndex].FindControl("CheckBox1") as CheckBox;
            if (checkin.Checked)
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = (@"Data Source=.\SQLEXPRESS;Initial Catalog=LoanerInventory;Integrated Security=True");

                string Available = "Available";
                Debug.WriteLine(checkin + " is " + Available);
            }
        }
    }
}


error:
Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0123: No overload for 'ListView1_ItemCommand' matches delegate 'System.EventHandler<System.Web.UI.WebControls.ListViewCommandEventArgs>'

Source Error:

 

Line 4:  <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
Line 5:  
Line 6:          <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlBBInUse" OnItemCommand="ListView1_ItemCommand">
Line 7:          <LayoutTemplate>
Line 8:         <h3> Please select a BlackBerry to check in:</h3>
 

Source File: c:\Users\kcross\Documents\Visual Studio 2010\Projects\Loaner Inventory\Loaner Inventory\BlackBerry\BBCheckIn.aspx    Line: 6 


Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 11:50 AM

You are using the wrong EventArgs object.

This:
protected void ListView1_ItemCommand(object sender, ListViewUpdateEventArgs e)



Should be this:
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)


We are trying to help you, but you really need to read the answers we are providing. And I would highly suggest you let the designer generate the event declarations for you instead of hand coding them. At least until you have a good understanding of events.

Quote

Does this mean on every single command, it will use that event? So I need to write a custom event for all actions that could occur within the ListView?


The ItemCommand event will only fire when a button inside a ListView is clicked. This answer you could have easily found in the link I provided on the ItemCommand event.

No is the answer to the second question.
Was This Post Helpful? 0
  • +
  • -

#13 KCross  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 03-March 10

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 12:03 PM

View PostJayman, on 05 March 2010 - 10:50 AM, said:

You are using the wrong EventArgs object.

This:
protected void ListView1_ItemCommand(object sender, ListViewUpdateEventArgs e)



Should be this:
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)


Awesome. I see what I was doing wrong. Thank you.

Quote

We are trying to help you, but you really need to read the answers we are providing.


Yes, I'm trying. I've only been learning for about 3 weeks now. Only 1 week on C#. I do sometimes overlook things.

Quote

And I would highly suggest you let the designer generate the event declarations for you instead of hand coding them. At least until you have a good understanding of events.


I WISH I knew how to do this.

Quote

Quote

Does this mean on every single command, it will use that event? So I need to write a custom event for all actions that could occur within the ListView?


The ItemCommand event will only fire when a button inside a ListView is clicked. This answer you could have easily found in the link I provided on the ItemCommand event.

No is the answer to the second question.


The MSDN Library, frankly, is not beginner friendly. I try to look at the code adn figure out how it works, because their explanations just confuse me. I have a way to go before I can comfortably understand those MSDN articles.

Thank you again. Sorry for the trouble. I think I have it sorted now.

This post has been edited by KCross: 05 March 2010 - 12:05 PM

Was This Post Helpful? 0
  • +
  • -

#14 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 01:19 PM

View PostKCross, on 05 March 2010 - 11:03 AM, said:

Thank you again. Sorry for the trouble. I think I have it sorted now.

It is no trouble, really. We are here to help. I wasn't trying to be hard on you, just saying you need to read carefully. We were all beginners at one time and had to deal with the same issues you are now experiencing.

You will do just fine. :)

View PostKCross, on 05 March 2010 - 11:03 AM, said:

Quote

And I would highly suggest you let the designer generate the event declarations for you instead of hand coding them. At least until you have a good understanding of events.


I WISH I knew how to do this.


It is actually pretty easy. Switch to the design view, select a control that you want to generate an event for.

Go to the Properties window, near the top you will see a little lightning bolt icon. Click it. What you are seeing is a list of all the events for that control.

Double click the event you need and it will create the event handler in the code behind for you.

See the screenshot for an example of the GridView events.

Hope that helps.
Attached Image
Was This Post Helpful? 0
  • +
  • -

#15 KCross  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 03-March 10

Re: Custom Button Event in a ListView

Posted 05 March 2010 - 02:12 PM

Blah! I've been working in Source for a few days on this - I was looking all over for that icon and just didn't see it. I figured I couldn't automatically create events from within a ListView (since that's where the entirety of the page is).

I didn't realize it's only visible in Design view! That makes things a bit easier...

One last question on this...
CheckBox checkin = ListView1.Items[e.ItemIndex].FindControl("CheckBox1") as CheckBox; 



That returns a "null" value when I use ListViewCommandEventArgs (apparently it's not a method). I found the correct syntax for that EventArgs object:

CheckBox checkin = e.Item.FindControl("CheckBox1") as CheckBox;


I don't understand why this works. Isn't e the object that initiated the postback (i.e. the button)? How could I find a CheckBox control on the button?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2