This example creates a very simple grid that as a checkbox on each row. Allowing you to select multiple rows at a time.
Note: You can do it in Html for simple controls, but if you need to add features such as grouping, you will probably have to build your grid programmatically. (see Oscar Medina's post for tips on how to do that)
1. Register the SharePoint web controls on the aspx page
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=11.0.0.0,
Culture=neutral,
PublicKeyToken=71e9bce111e9429c" %>
<SharePoint:SPGridView id="grid" allowsorting="true" autogeneratecolumns="false" runat="server">
<Columns>
<asp:TemplateField headertext="Select"><ItemTemplate>
<input id="select" type="checkbox" runat="server" />
<input id="itemID" type="hidden" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</SharePoint:SPGridView>
3. If you know the data columns, you can declare them here too. Or you can build them dynamically at step 5 (see my previous post)
4. In your code-behind page, add a reference to your grid
public class MycodeBehind : System.Web.UI.Page
{protected SPGridView grid;
5. Create a method to build the data fields dynamically (see my previous post)
6. Wire up an event handler for the RowDataBound event.
grid.RowDataBound += new GridViewRowEventHandler(grid_RowDataBound);
7. Create your Event Handler method. This method will set the itemID field value to the ID of the ListItem. You can use this field on the post back to identify the ListItem.
private void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{ if (e.Row.RowType == DataControlRowType.DataRow) { HtmlInputHidden itemID = (HtmlInputHidden)e.Row.FindControl("itemID");if (itemID != null)
{DataRowView data = (DataRowView)e.Row.DataItem;
itemID.Value = data["ID"].ToString();}
}
}
8. Add this code to the post back event (OnClick most likely). This will loop through your grid and find all the rows that are checked. Once you have a checked row, you can use the itemID value to quickly find the corresponding ListItem.
for (int i = 0; i < grid.Rows.Count; i++)
{ HtmlInputCheckBox selectCtl = (HtmlInputCheckBox)grid.Rows[i].FindControl("select"); HtmlInputHidden itemIDCtl = (HtmlInputHidden)grid.Rows[i].FindControl("itemID");if (selectCtl.Checked && itemIDCtl.Value != "")
{ // find the row so we can retrieve the values SPListItem myListItem = myList.Items.GetItemById(Convert.ToInt32(itemIDCtl.Value));
// do whatever you need to do}
}
6 comments:
greate post. thanks
Hi, Very interesting Article. How would one wire up the "OnClick" event for the checkboxes?
Phil
Hey Phil,
I think that would take quite a bit of work. You might try using an ASP.NET Checkbox instead of an Html checkbox and set the autopostback to true. Then you would need to capture the posting control and call the right method.
The purpose for the checkboxes in this example is to allow several to be checked at once. Having each checkbox perform a postback would be counter-productive for this case.
I've been working on a web part to do something similiar. I settled on using an HTML checkbox to do the work. ASP.Net control was not flexible enough for my requirements. It's an interesting problem because capturing data is a common task for SharePoint Development. Thanks for the post.
Do you know if you can write out(render) template columns in web parts similiar to what you did in the asp.net page? for example, In a web part, can I do, "writer.write("Template Column"); and expect the control to catch it?
Phil
Hey Phil,
I have not tried that approach.
Thanks. You saved me a lot of time. I took your idea and transfered it a SPGridView that I have in a webpart.
Post a Comment