Results 1 to 15 of 15

Thread: Add button in listview

  1. #1

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Add button in listview

    Hi,

    I would like to know how i can easly add a button to my listview control.

    Thanks

  2. #2

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: Add button in listview

    ok, i was able to add a button with this line of code.

    vb Code:
    1. <asp:Button ID="Button1" runat="server" CommandName="Edit" Text="Editor" />

    but now i would like to know where i can edit the command for this button.

    thx

  3. #3
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add button in listview

    Hey,

    I think I am correct in saying that it is the ItemCommand of the ListView that you need to use:

    http://msdn.microsoft.com/en-us/libr...emcommand.aspx

    Gary

  4. #4

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: Add button in listview

    Hi,

    ok, is there another way to add a button and write the code for than ItemCommand ?

    thx

  5. #5
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add button in listview

    Hey,

    Why would you need another way? What is wrong with this way?

    Gary

  6. #6

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: Add button in listview

    Hi,

    Well, i wasn't able to make the code run correctly. I used the example to work with my code, but it's not working

  7. #7
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add button in listview

    Hey,

    Can you post the code that you tried? This is definitely the correct technique to use, so it is just a case of getting it to work.

    Did you create the entire example code that was given? This is always a good place to start. It will let you see how things work, and you can build on it from there.

    Gary

  8. #8

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: Add button in listview

    Well, the problem i'm having is, it's showing the msgbox whenever any button is clicked.

    vb Code:
    1. <asp:ListView ID="ListView8" runat="server" DataKeyNames="id"
    2.                 DataMember="DefaultView" DataSourceID="website"  OnItemCommand="ListView8_OnItemCommand" InsertItemPosition="LastItem"
    3.                 style="text-align: left">
    4.                 <ItemTemplate>
    5.                     <tr style="background-color: #FFFBD6;color: #333333;">
    6.                         <td>
    7.                             <asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
    8.                                                            Text="Delete" />
    9.                             <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
    10.                              <asp:Button ID="Button1" runat="server" CommandName="OnItemCommand" Text="Editor" />

    Protected Sub ListView8_OnItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs)
    MsgBox("hi")
    End Sub

  9. #9
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add button in listview

    Hey,

    That isn't how the CommandName is meant to be used. Did you read the documentation that I linked to?

    You specify the CommandName for the button, in your case Edit, and then in the ItemCommand EventHandler, you check to see what the CommandName is, and then you take the appropriate action.

    Gary

  10. #10

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Re: Add button in listview

    :S still i can get it run, it's showing the msg box for all the buttons

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Add button in listview

    Change

    <asp:Button ID="Button1" runat="server" CommandName="OnItemCommand" Text="Editor" />

    to

    <asp:Button ID="Button1" runat="server" CommandName="MET0555" Text="Editor" />


    Change the RowCommand event to

    vb Code:
    1. Protected Sub ListView8_OnItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs)
    2. If e.CommandName = "MET0555" Then
    3. MsgBox("hi")
    4. End If
    5. End Sub

    The CommandName is a property you use in the RowCommand event to distinguish between the various buttons being clicked

  12. #12
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add button in listview

    Hey,

    This is what I was trying to say, but obviously didn't say it well enough. I thought the MSDN sample said it all, but obviously not.

    Gary

  13. #13
    Addicted Member yolandre's Avatar
    Join Date
    Oct 2004
    Posts
    177

    Re: Add button in listview

    Good day all,

    I know this is a bit out-dated, but using the prescribed method is causing errors in VS2012 & IE10. Any advice?

    Thanks in advance!

  14. #14
    Addicted Member yolandre's Avatar
    Join Date
    Oct 2004
    Posts
    177

    Re: Add button in listview

    Apologies all - my bad (neglected to manage post-back).

    Best regards.

  15. #15
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Add button in listview

    Wow, this is an old thread Happy to hear that it was useful to you know!

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width