首先上效果图:
前台代码:
<script type="text/javascript">
function show() {
alert("abc");
}
</script>
<SharePoint:SPGridView ID="GridView1" DataKeyNames="ID" AutoGenerateColumns="false" runat="server">
<Columns>
<SharePoint:SPBoundField DataField="LinkTitle" HeaderText="标题">
</SharePoint:SPBoundField>
<SharePoint:SPBoundField DataField="_x5185__x5bb9_" HeaderText="内容">
</SharePoint:SPBoundField>
<SharePoint:SPBoundField DataField="Author" HeaderText="创造者">
</SharePoint:SPBoundField>
<asp:CommandField ShowEditButton="true" HeaderText="操作" EditText="编辑" UpdateText="更新" CancelText="取消" />
<SharePoint:SPMenuField MenuTemplateId="MenuTemplate1" HeaderText="标题1" NavigateUrlFields="ID,LinkTitle"
NavigateUrlFormat="do.aspx?p={0}&q={1}" TokenNameAndValueFields="EDIT=ID,NAME=LinkTitle"
TextFields="LinkTitle">
<ItemStyle VerticalAlign="Middle" HorizontalAlign="Center" />
</SharePoint:SPMenuField>
</Columns>
</SharePoint:SPGridView>
<SharePoint:MenuTemplate ID="MenuTemplate1" runat="server">
<SharePoint:MenuItemTemplate ID="MenuItemTemplate1" runat="server" Text="删除2" Title="删除1"
ClientOnClickNavigateUrl="do.aspx?this=%EDIT%&that=%NAME%" ImageUrl="/_layouts/images/EawfNewUser.gif">
</SharePoint:MenuItemTemplate>
<SharePoint:MenuItemTemplate ID="MenuItemTemplate2" runat="server" Text="编辑2" ImageUrl="/_layouts/images/ICWM.gif"
ClientOnClickScript="show();">
</SharePoint:MenuItemTemplate>
</SharePoint:MenuTemplate>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
if (!string.IsNullOrEmpty(id))
{
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["CusList"];
SPListItem item = list.GetItemById(Convert.ToInt32(id));
item["_x5185__x5bb9_"] = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
item.Update();
GridView1.EditIndex = -1;
BindGridView();
}
}
protected void GridView1_RowCancelling(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridView();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
private void BindGridView()
{
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["CusList"];
SPListItemCollection items = list.Items;
DataTable dt = items.GetDataTable();
if (dt != null)
{
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}
}
说明:
1.创建下拉菜单代码:
1)在SPGridView中的SPMenuField MenuTemplateId ,注意它的MenuTemplateId
属性说明:
NavigateUrlFields="ID,LinkTitle"//指定下面链接的参数
NavigateUrlFormat="do.aspx?p={0}&q={1}" //这里指定url,参数用{0}{1}...{n}替代,实际值,由上面的值替换。
TokenNameAndValueFields="EDIT=ID,NAME=LinkTitle"//指定菜单项中可以使用的值,就是ClientOnClickNavigateUrl="do.aspx?this=%EDIT%&that=%NAME%"
TextFields="LinkTitle"//指定绑定的项值,比如图中的title1,title2
2)MenuTemplate它是下啦菜单项的容器,注意它的ID必须和上面的MenuTemplateId一致。
3)SPMenuField MenuTemplateId就是菜单项了。
属性说明:
Text="删除2" //下啦菜单项看到的值
Title="删除1"//下拉菜单项title(有什么用处?)
ClientOnClickNavigateUrl="do.aspx?this=%EDIT%&that=%NAME%" //点击下拉菜单项后跳转,注意这里的参数。
ImageUrl="/_layouts/images/EawfNewUser.gif"//下拉菜单项前面的小图标
ClientOnClickScript="show();"//可以绑定js函数
2.编辑
首先SPGridView继承自GridView,所以你可以直接用GridView里面的思想来编辑。但是由于前者的列是<SharePoint:SPBoundField ,所以此时编辑时并不会像GridView那样出现文本框。如下:下面是出于编辑状态的SPGridView,可以看出,标题和内容等并没转成可输入的文本框。
将代码作如下改变即可:将上面的所有SharePoint:SPBoundField 换成asp:BoundField,就行了。如下
<SharePoint:SPGridView ID="SPGridView1" DataKeyNames="ID" AutoGenerateColumns="false"
OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelling"
OnRowUpdating="GridView1_RowUpdating" runat="server">
<Columns>
<asp:BoundField
<asp:BoundField DataField="LinkTitle" HeaderText="标题">
</asp:BoundField>
<asp:BoundField DataField="_x5185__x5bb9_" HeaderText="内容">
</asp:BoundField>
<asp:BoundField DataField="Author" HeaderText="创造者">
</asp:BoundField>
<asp:CommandField ShowEditButton="true" HeaderText="操作" EditText="编辑" UpdateText="更新" CancelText="取消" />
。。。。。。。。<节省篇幅,其他代码省略>
注意,1.有没有看到内容里有div标签,这个很不好,不过asp:BoundField里有HtmlEncode属性,设为false即可。SharePoint:SPBoundField没有该属性。
2.asp:CommandField 并没有对应的SharePoint:CommandField
3.完全可以用gridview的功能替代spgridView