我是视觉工作室的新手。我有一个Sharepoint列表,其中包含以下字段组名称,用户,部分和活动(这是一个复选框)。我有一个网格视图可视化Web部件,它将从列表中检索值,我需要在网格视图中选中一个复选框。
当用户选中网格视图中的复选框并单击“确定”按钮时,我希望SharePoint列表项“活动”更新为“真”或“是”并取消选中并单击“确定”为“否”或“否”。
以下是我到目前为止所做的工作。我能够从列表中检索值。但是,当我单击复选框时,我的SharePoint列表中没有任何更新。
<asp:GridView ID="GridView1" runat="server"
onselectedindexchanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Group Name" HeaderText="Group Name" />
<asp:BoundField DataField="User" HeaderText="User" />
<asp:BoundField DataField="Section" HeaderText="Section" />
<asp:TemplateField HeaderText="Action" ShowHeader="true" HeaderStyle-HorizontalAlign="Center" Visible="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="#ad0015">
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server" AutoPostBack="true"
Width="100px"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<HeaderTemplate>
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Ok"
Width="119px" />
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = GetData();
if (!Page.IsPostBack)
{
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
DataTable GetData()
{
SPSite oSiteCollection = SPContext.Current.Site;
SPWeb oWeb = oSiteCollection.OpenWeb();
SPList oSPList = oWeb.Lists["sample"];
SPListItemCollection oSPListItemCollection = oSPList.Items;
DataTable dt = new DataTable();
try
{
dt.Columns.Add("Group Name", typeof(String));
dt.Columns.Add("User", typeof(String));
dt.Columns.Add("Section", typeof(String));
dt.Columns.Add("Active", typeof(bool));
dt.Columns.Add("ID", typeof(int));
DataRow dataRow;
foreach (SPListItem oSplistItem in oSPListItemCollection)
{
dataRow = dt.Rows.Add();
dataRow["Group Name"] = oSplistItem["Group Name"].ToString();
dataRow["User"] = oSplistItem["User"].ToString();
dataRow["Section"] = oSplistItem["Section"].ToString();
//dataRow["ID"] = oSplistItem["ID"].ToString();
// dataRow["Active"] = oSplistItem["Active"].ToString();
}
return dt;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Managers Approval" + ex.Message.ToString());
return dt;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("chkStatus");
bool status = cb.Checked;
string strBoolean = Convert.ToString(status);
if (cb != null)
{
SPSite oSiteCollection = SPContext.Current.Site;
using (SPWeb oWeb = oSiteCollection.OpenWeb())
{
SPList oSPList = oWeb.Lists["sample"];
SPListItemCollection oSPListItemCollection = oSPList.Items;
foreach (SPListItem oSplistItem in oSPListItemCollection)
{
if (strBoolean == "True")
{
oSplistItem["Active"] = true;
}
else
{
oSplistItem["Active"] = false;
}
oSplistItem.Update();
}
}
}
答案 0 :(得分:0)
uncomment the following lines 1. uncomment the following lines 2. // dataRow["Active"] = oSplistItem["Active"].ToString(); 3. in your button_click event, get the row id 4. give the id to the splistitem and it will find the items as well as improve performance 5. you have to use the AllowUnsafeUpdates before and after your changes let me know if I can help further
<asp:GridView ID="GridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Group Name" HeaderText="Group Name" />
<asp:BoundField DataField="User" HeaderText="User" />
<asp:BoundField DataField="Section" HeaderText="Section" />
<asp:TemplateField HeaderText="Action" ShowHeader="true" HeaderStyle-HorizontalAlign="Center" Visible="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="#ad0015">
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server" AutoPostBack="true" Width="100px"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<HeaderTemplate>
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Ok"
Width="119px" />
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = GetData();
if (!Page.IsPostBack)
{
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
DataTable GetData()
{
SPSite oSiteCollection = SPContext.Current.Site;
SPWeb oWeb = oSiteCollection.OpenWeb();
SPList oSPList = oWeb.Lists["sample"];
SPListItemCollection oSPListItemCollection = oSPList.Items;
DataTable dt = new DataTable();
try
{
dt.Columns.Add("Group Name", typeof(String));
dt.Columns.Add("User", typeof(String));
dt.Columns.Add("Section", typeof(String));
dt.Columns.Add("Active", typeof(bool));
dt.Columns.Add("ID", typeof(int));
DataRow dataRow;
foreach (SPListItem oSplistItem in oSPListItemCollection)
{
dataRow = dt.Rows.Add();
dataRow["Group Name"] = oSplistItem["Group Name"].ToString();
dataRow["User"] = oSplistItem["User"].ToString();
dataRow["Section"] = oSplistItem["Section"].ToString();
dataRow["ID"] = oSplistItem["ID"];
dataRow["Active"] = oSplistItem["Active"].ToString();
}
return dt;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Managers Approval" + ex.Message.ToString());
return dt;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("chkStatus");
bool status = cb.Checked;
string strBoolean = Convert.ToString(status);
if (cb != null)
{
SPSite oSiteCollection = SPContext.Current.Site;
using (SPWeb oWeb = oSiteCollection.OpenWeb())
{
SPList oSPList = oWeb.Lists["sample"];
SPListItemCollection oSPListItemCollection = oSPList.Items;
//foreach (SPListItem oSplistItem in oSPListItemCollection)
//{
int rowId = Int32.Parse(grdList.DataKeys[dr.RowIndex].Value.ToString());
SPListItem oSplistItem = oSPList.GetItemById(Convert.ToInt32(rowId));
oWeb.AllowUnsafeUpdates = true;
{
if (strBoolean == "True")
{
oSplistItem["Active"] = true;
}
else
{
oSplistItem["Active"] = false;
}
oSplistItem.Update();
}
oWeb.AllowUnsafeUpdates = false;
}
}
}