I'm trying to write a web part that access lists and add items in a specific list. The user modifies the web part and type in List Name, the web part has 3 TextBoxs and a Button. The user should enter data and press the save button, that should update the list and add these data in a new record.
I'm using the following code to build my web part, but the save button doesn't update the list. I don't know what's wrong it just reload the page and add nothing.
Edit: BTW the list am trying to update has three columns, First Name, Last Name, and Email, respectively.
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Security;
namespace TimeSheetEntry
{
public class TimesheetEntry : System.Web.UI.WebControls.WebParts.WebPart
{
public TimesheetEntry() { }
protected TextBox txtFirstName;
protected TextBox txtLastName;
protected TextBox txtEmail;
protected Button btnSave;
protected string listName = string.Empty;
[WebBrowsable(true),
WebDisplayName("List Name"),
WebDescription("Enter List Name"),
Personalizable(PersonalizationScope.User)]
public string LN
{
get
{
return this.listName;
}
set
{
this.listName = value;
}
}
protected override void CreateChildControls()
{
//initialize textboxes
txtFirstName = new TextBox();
txtLastName = new TextBox();
txtEmail = new TextBox();
//initialize button
btnSave = new Button();
btnSave.Text = "Save";
btnSave.Click += new EventHandler(btnSave_Click);
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.Write("<table width=100%>");
writer.Write("<tr>");
writer.Write("<td colspan=2>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td>First:<span class=’ms-formvalidation’>*</span></td>");
writer.Write("<td>");
txtFirstName.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td>Last Name:<span class=’ms-formvalidation’>*</span></td>");
writer.Write("<td>");
txtLastName.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td>Email:<span class=’ms-formvalidation’>*</span></td>");
writer.Write("<td>");
txtEmail.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr>");
writer.Write("<td>");
writer.Write("</td>");
writer.Write("<td>");
btnSave.RenderControl(writer);
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</table>");
}
public void btnSave_Click(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Web;
SPList timesheetList = web.Lists[this.listName];
SPListItem newTimesheetEntry = timesheetList.Items.Add();
newTimesheetEntry["First Name"] = txtFirstName.Text;
newTimesheetEntry["Last Name"] = txtLastName.Text;
newTimesheetEntry["Email"] = txtEmail.Text;
newTimesheetEntry.Update();
timesheetList.Update();
}
}
}