using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security;
using Microsoft.SharePoint;
[assembly: AllowPartiallyTrustedCallers]
namespace BasicWebPartst
{
public class HelloWorld : WebPart
{
TextBox txtBoxtitle, txtBoxCity, txtBoxID;
//protected override void Render(HtmlTextWriter writer)
//{
// writer.Write("hi!");
//}
protected override void CreateChildControls()
{
base.CreateChildControls();
InitializeComponents();
}
private void InitializeComponents()
{
Label lbltest = new Label();
lbltest.Text = "Fill the below table to populate the List";
this.Controls.Add(lbltest);
this.Controls.Add(new LiteralControl("<br>"));
this.Controls.Add(new LiteralControl("<br>"));
Table t = new Table();
t.BorderStyle = BorderStyle.Dotted;
t.BorderWidth = 1;
t.Width = 400;
# region Row1
//row 1
TableRow tr1 = new TableRow();
TableCell tc11, tc12;
tc11 = new TableCell();
tc12 = new TableCell();
tc11.Text = "Title: ";
tc11.Width = 100;
tc11.ColumnSpan = 1;
tc12.Width = 300;
tc12.ColumnSpan = 3;
txtBoxtitle = new TextBox();
txtBoxtitle.Width = 300;
tc12.Controls.Add(txtBoxtitle);
tr1.Cells.Add(tc11);
tr1.Cells.Add(tc12);
#endregion
#region Row2
//row 2
TableRow tr2 = new TableRow();
TableCell tc21, tc22;
tc21 = new TableCell();
tc22 = new TableCell();
tc21.Text = "City: ";
tc21.Width = 100;
tc22.Width = 300;
tc21.ColumnSpan = 1;
tc22.ColumnSpan = 3;
txtBoxCity = new TextBox();
txtBoxCity.Width = 300;
tc22.Controls.Add(txtBoxCity);
tr2.Cells.Add(tc21);
tr2.Cells.Add(tc22);
#endregion
#region Row3
//row 3
TableRow tr3 = new TableRow();
TableCell tc31, tc32;
tc31 = new TableCell();
tc32 = new TableCell();
tc31.ColumnSpan = 1;
tc32.ColumnSpan = 3;
tc31.Text = "ID: ";
tc31.Width = 100;
tc32.Width = 300;
txtBoxID = new TextBox();
txtBoxID.Width = 300;
tc32.Controls.Add(txtBoxID);
tr3.Cells.Add(tc31);
tr3.Cells.Add(tc32);
#endregion
#region Row4
//row 4
TableRow tr4 = new TableRow();
TableCell tc41, tc42, tc43,tc44;
tc41 = new TableCell();
tc42 = new TableCell();
tc43 = new TableCell();
tc41 = new TableCell();
tc42 = new TableCell();
tc44 = new TableCell();
tc41.Width = tc42.Width = tc43.Width = tc44.Width = 100;
tc41.ColumnSpan = tc42.ColumnSpan = tc43.ColumnSpan = 1;
Button BtnAdd = new Button();
BtnAdd.Text = " Add Item";
BtnAdd.Click += new EventHandler(BtnAdd_Click);
Button BtnDelete = new Button();
BtnDelete.Text = " Delete Item";
BtnDelete.Click += new EventHandler(BtnDelete_Click);
Button BtnSearch = new Button();
BtnSearch.Text = " Search Item";
BtnSearch.Click += new EventHandler(BtnSearch_Click);
Button BtnReset = new Button();
BtnReset.Text = " Reset";
BtnReset.Click += new EventHandler(BtnReset_Click);
BtnAdd.Width = BtnDelete.Width = BtnReset.Width = BtnSearch.Width= 100;
tc41.Controls.Add(BtnReset);
tc42.Controls.Add(BtnAdd);
tc43.Controls.Add(BtnDelete);
tc44.Controls.Add(BtnSearch);
tr4.Cells.Add(tc41);
tr4.Cells.Add(tc42);
tr4.Cells.Add(tc43);
tr4.Cells.Add(tc44);
#endregion
t.Rows.Add(tr1);
t.Rows.Add(tr2);
t.Rows.Add(tr3);
TableRow trblank = new TableRow();
t.Rows.Add(trblank);
t.Rows.Add(tr4);
this.Controls.Add(t);
}
void BtnSearch_Click(object sender, EventArgs e)
{
ClearForm();
if (string.IsNullOrEmpty(txtBoxID.Text))
{
return;
}
SPWeb currentWeb = SPContext.Current.Web;
currentWeb.AllowUnsafeUpdates = true;
SPList list = currentWeb.Lists["VivekList"];
SPListItem temp = null;
try
{
temp = list.Items.GetItemById(Convert.ToInt16(txtBoxID.Text));
}
catch { };
if (temp != null)
{
txtBoxtitle.Text = temp["Title"].ToString();
txtBoxCity.Text = temp["City"].ToString();
}
}
void BtnReset_Click(object sender, EventArgs e)
{
ClearForm();
}
private void AddItem(string StrTitle, string StrCity)
{
SPWeb currentWeb = SPContext.Current.Web;
currentWeb.AllowUnsafeUpdates = true;
SPList list = currentWeb.Lists["VivekList"];
SPListItem newListItem = list.Items.Add();
newListItem["Title"] = StrTitle;
newListItem["City"] = StrCity;
newListItem.Update();
txtBoxID.Text = Convert.ToString( newListItem.ID);
}
void BtnDelete_Click(object sender, EventArgs e)
{
//don't delete if item id is blank
if (String.IsNullOrEmpty(txtBoxID.Text))
{
return;
}
SPWeb currentWeb = SPContext.Current.Web;
currentWeb.AllowUnsafeUpdates = true;
SPList list = currentWeb.Lists["VivekList"];
list.Items.DeleteItemById(Convert.ToInt16(txtBoxID.Text));
ClearForm();
}
private void ClearForm()
{
txtBoxID.Text = "";
txtBoxCity.Text = "";
txtBoxtitle.Text = "";
}
void BtnAdd_Click(object sender, EventArgs e)
{
//Empty items dont add
if (string.IsNullOrEmpty(txtBoxtitle.Text) || string.IsNullOrEmpty(txtBoxCity.Text))
{
return;
}
AddItem(txtBoxtitle.Text, txtBoxCity.Text);
}
}
}