Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Cannot access Value in Row Command click event

Options
  • 22-04-2008 3:52pm
    #1
    Registered Users Posts: 2,791 ✭✭✭


    Hi,

    I have a webpart which uses an SPGridview to display items in a list. There is a button in the list which triggers an event handler which deletes the item from the list.

    My problem is that I cannot find out which row was selected. Outputting the row.Cells[0].text or any other Cell gives me nothing. I know that the Row index is correct, so there really should be no problem with this. Any insight would be greatly appreciated!

    Here's the code used to create the SPGridview:
    void LoadGridList(string ListName)
            {
                //Clear the Placeholder
                gridPlaceHolder.Controls.Clear();
                //Loads the given list into a gridview
                SPSite MainSite = new SPSite(strMainWebsite);
                SPWeb MainWeb = MainSite.OpenWeb();
                SPList list = MainWeb.Lists[ListName];
                SPDataSource ds = new SPDataSource();
                SPBoundField fldPropertyName;
    
                ds.List = MainWeb.Lists[ListName];
                ds.DataSourceMode = SPDataSourceMode.List;
                ds.IncludeHidden = false;
                ds.Scope = SPViewScope.Recursive;
    
                gridList = new SPGridView();
                gridList.ID = "gridList";
                gridList.CellPadding = 2;
                gridList.DataSource = ds;
                gridList.AutoGenerateColumns = false;
    
                //Listitem ID (invisible)
                fldPropertyName = new SPBoundField();
                fldPropertyName.HeaderText = "ID";
                fldPropertyName.DataField = "ID";
                //fldPropertyName.Visible = false;
                fldPropertyName.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
                gridList.Columns.Add(fldPropertyName);
                //Title
                fldPropertyName = new SPBoundField();
                fldPropertyName.HeaderText = "Title";
                fldPropertyName.DataField = "Title";
                fldPropertyName.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
                gridList.Columns.Add(fldPropertyName);
    
     //Add the Remove button
    
               btnRemoveItem = new ButtonField();
               btnRemoveItem.ButtonType = ButtonType.Button;
               btnRemoveItem.Text = "Remove";
               gridList.RowCommand += new GridViewCommandEventHandler(gl_RowCommand);
               gridList.Columns.Add(btnRemoveItem);
                  
               gridPlaceHolder.Controls.Add(gridList);
               gridList.DataBind();
               gridList.PagerTemplate = null;  // Must be called after Controls.Add()
    
    }
    

    Now here's the event handler, it is currently outputting "Selected: row number : " where row number is the correct row
    void gl_RowCommand(Object sender, GridViewCommandEventArgs  e)
            {
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
                int index = Convert.ToInt32(e.CommandArgument);
                int itemID = 0;
              
                // Retrieve the row that contains the button clicked
                // by the user from the Rows collection.
                GridViewRow row = gridList.Rows[index];
         
                msgLabel.Text = "Selected: " + index + " : " + SPEncode.HtmlDecode(row.Cells[1].Text);
            }
    
    


Comments

  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Haven't done much SharePoint dev work so not sure if I can help much, but first thing I'd suggest is to step through your event handler in the debugger (you know you can attach the VS2005 debugger to the w3p process?), make sure your variables and objects like index, row etc are being populated the way you think they are. It looks to me like e.CommandArgument doesn't in fact contain the index of the selected row.

    I think that what you're trying to do is have a "Remove" button in each row, the "Remove" button triggers an event, much the way a select button would do, which you then code to delete the row?

    I haven't used the SPGridView, but I think it's safe to assume it works fairly similarly to the standard ASP.Net GridView, and that being the case I had a very similar problem with one of them before. The problem being I added a simple button field which I wrote an event handler for, but the event handler could not tell which row the button had been clicked in.

    Conveniently I can't remember exactly how I worked around it :) I think one way was to use a template column which acted as a select button and that event handler was passed the row index, or possibly I had to embed the row index in the buttons tag or something like that. I'll try to remeber more but that might get you pointed in the right direction at least.


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Hi Steven,

    Many thanks for the reply. I finally got a solution to it from someone on the MSDN forums (for once! :eek:)

    SPBoundFeild's get rendered as labels so you need to get the control out instead of the text.

    Label lbl1 = (Label) row.Cells[1].Controls[0];

    msgLabel.Text = lbl1.Text;

    This works fine for me thank god, been at it all day

    stevenmu wrote: »
    Haven't done much SharePoint dev work so not sure if I can help much, but first thing I'd suggest is to step through your event handler in the debugger (you know you can attach the VS2005 debugger to the w3p process?), make sure your variables and objects like index, row etc are being populated the way you think they are. It looks to me like e.CommandArgument doesn't in fact contain the index of the selected row.

    I think that what you're trying to do is have a "Remove" button in each row, the "Remove" button triggers an event, much the way a select button would do, which you then code to delete the row?

    I haven't used the SPGridView, but I think it's safe to assume it works fairly similarly to the standard ASP.Net GridView, and that being the case I had a very similar problem with one of them before. The problem being I added a simple button field which I wrote an event handler for, but the event handler could not tell which row the button had been clicked in.

    Conveniently I can't remember exactly how I worked around it :) I think one way was to use a template column which acted as a select button and that event handler was passed the row index, or possibly I had to embed the row index in the buttons tag or something like that. I'll try to remeber more but that might get you pointed in the right direction at least.


Advertisement