SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple Buttons in a DataGrid - How to find which was pressed

    Hello everyone,

    I have a small problem. I need to display a list of all registered users in a table. The first three columns contain the user information the last page has three buttons “Details”, “E-Mail” and “Löschen”
    Displaying the table is not a problem. The problem is responding to a click on of the buttons. As there are several users, the 3-button group is repeated and whenever I click a button nothing happens.
    I have attached a picture showing the current table. I am using a DataGrid to display the information.

    So my problem is, how do I find out which button has been clicked and handle the event? Is it also possible to access information of the row in which the button has been clicked?

    Thanks in advance for your help.
    Christian
    Attached Images Attached Images

  2. #2
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should be in the .NET forum.

  3. #3
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops, I'm sorry. I had two tabs open and apparently opened the thread in the wrong tab and forum.
    If an admin could please move this. Thanks.

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Off Topic:

    moved

  5. #5
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Nashville, TN
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    This should get you started...

    You need to do the following:

    - Add an OnItemCommand="YourMethodHere" to your DataGrid declaration.
    Code:
    <asp:DataGrid runat="server" id="dgStuff" OnItemCommand="YourMethodHere">
    This is the method that will be called whenever a button in your datagrid is clicked.

    Code:
    protected void YourMethodHere(sender as Object, e As DataGridCommandEventArgs) {
          switch(e.CommandName){
               case "details":
                Response.Write(e.CommandArgument.ToString());
                break;
    
               case "Button2":
    
                break;
          }
    }
    - Add a CommandName and CommandArgument to each ButtonTemplate.

    Code:
    <asp:ButtonColumn Text="Details" HeaderText="Details" CommandName="details" CommandArgument="Some Unique Row Identifier Here." ButtonType="PushButton" />
    The CommandName is used to identify which type of button is clicked, and the CommandArgument can be used to pass a button specific (or more likely row-sepecific) identifier.

    DISCLAIMER - This code probably won't compile
    - it is intended to get you going in the right direction. Let me know if you have any other questions. For a great tutorial on the datagrid, Check out Scott Mitchell's Extensive Examination of the DataGrid Control .

  6. #6
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent!

    Thanks for the code and the great link. I have it working now.
    I didn't have to use the CommandArgument parameter, though.

    Christian


Bookmarks

Posting Permissions

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