Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

Sample Code For Beginners



using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using System.Security;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

using System.Data;

using System.Drawing;
[assembly:AllowPartiallyTrustedCallers]

namespace BeginnersCodeSamples
{
[Guid("e2526166-7e83-4a71-8693-37ea4c20208c")]
public class BeginnersCodeSamples : System.Web.UI.WebControls.WebParts.WebPart
{

#region DeclareVariablesInsertion
TextBox txtInsertLoginName;
TextBox txtInsertLoginPwd;
RequiredFieldValidator rfvInsertLoginName;
RequiredFieldValidator rfvInsertLoginPwd;
Button btnSave;

#endregion
#region DeclareVariablesSearch
DropDownList ddlUserName;
TextBox txtEditPassWord;
RequiredFieldValidator rfvSearchUserName;
RequiredFieldValidator rfvEditPassWord;
Button btnSearch;
Button btnUpdate;

#endregion
#region DeclareVariablesDelete
DropDownList ddlDeleteUserName;
RequiredFieldValidator rfvDeleteUserName;
Button btnDelete;

#endregion
#region SpGridViewSample
DataSet dSet = new DataSet();
DataTable dt = new DataTable();
SPGridView sgvTasks;

#endregion
#region SpGridViewGroupsSampleRetrieve
DataSet dsetGroups = new DataSet();
DataTable dtGroups = new DataTable();
SPGridView sgvGroupsData;

#endregion
#region AddUserToGroup
PeopleEditor peAddUser;
DropDownList ddlGroupName;
Button btnAddUser;

#endregion
#region SpGriviewGroupBySample
SPGridView sgvGroupBy;

#endregion

string strError = string.Empty;
public BeginnersCodeSamples()
{
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(strError);
try

{
if (Page.Request.Url.ToString().Contains("Add New Item.aspx"))
{

#region InsertSampleCodeDesign
writer.Write("<Table align='Center'>");
writer.Write("<Tr>");
writer.Write("<Td colspan='2' Align='Center'>");
writer.Write("<Strong>");
writer.Write("Insertion Sample");
writer.Write("</Strong>");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td align='Right'>");
writer.Write("Enter UserName");
writer.Write("</Td>");
writer.Write("<Td>");
txtInsertLoginName.RenderControl(writer);
writer.Write("&nbsp;");
rfvInsertLoginName.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Align='Right'>");
writer.Write("Enter PassWord");
writer.Write("</Td>");
writer.Write("<Td>");
txtInsertLoginPwd.RenderControl(writer);
writer.Write("&nbsp;");
rfvInsertLoginPwd.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td colspan='2' Align='Center'>");
btnSave.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("</Table>");

#endregion

}
else if (Page.Request.Url.ToString().Contains("Search Record.aspx"))
{

#region SearchSampleCodeDesign
writer.Write("<Table Align='Center'>");
writer.Write("<Tr>");
writer.Write("<Td colspan='2' Align='Center'>");
writer.Write("<Strong>");
writer.Write("Search Sample");
writer.Write("</Strong>");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Select UserName");
writer.Write("</Td>");
writer.Write("<Td>");
ddlUserName.RenderControl(writer);
writer.Write("&nbsp;");
rfvSearchUserName.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Enter PassWord");
writer.Write("</Td>");
writer.Write("<Td>");
txtEditPassWord.RenderControl(writer);
writer.Write("&nbsp;");
rfvEditPassWord.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td colspan='2' Align='Center'>");
btnSearch.RenderControl(writer);
writer.Write("&nbsp;");
btnUpdate.RenderControl(writer);
writer.Write("<Td>");
writer.Write("</Tr>");
writer.Write("</Table>");

#endregion

}
else if (Page.Request.Url.ToString().Contains("Delete Record.aspx"))
{

#region DeleteSampleCodeDesign
writer.Write("<Table>");
writer.Write("<Tr>");
writer.Write("<Td colspan='2' Align='Center'>");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td>");
writer.Write("Select UserName");
writer.Write("</Td>");
writer.Write("<Td>");
ddlDeleteUserName.RenderControl(writer);
writer.Write("&nbsp;");
rfvDeleteUserName.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='2' Align='Center'>");
btnDelete.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("</Table>");

#endregion

}
else if (Page.Request.Url.ToString().Contains("Single Row Backcolcolor Based on Status.aspx"))
{
sgvTasks.RenderControl(writer);
}
else if (Page.Request.Url.ToString().Contains("Multiple Row Backcolor Based on Status.aspx"))
{
sgvTasks.RenderControl(writer);
}
else if (Page.Request.Url.ToString().Contains("Images Based on Status.aspx"))
{
sgvTasks.RenderControl(writer);
}
else if (Page.Request.Url.ToString().Contains("Dynamic HTML Table.aspx"))
{

#region DynamicHTMLTable
writer.Write("<Style type='Text/Css'>");
writer.Write(".HeadingClass");
writer.Write("{");
writer.Write("font-weight:bold;font-size:10px;text-align:center;color:navy; border:1px;font-family:Trebuchet MS;border-color:red;");
writer.Write("}");
writer.Write(".NonHeadingClass");
writer.Write("{");
writer.Write("font-size:9px;text-align:center;font-family:Trebuchet MS;border:1px;border-color:red;");
writer.Write("}");
writer.Write(".blankTD");
writer.Write("{");
writer.Write("background-color:blue;");
writer.Write("}");
writer.Write(".MessageClass");
writer.Write("{");
writer.Write("color:Navy;font-size:xx-small;font-family:Trebuchet MS;text-Align:Center;font-weight:bold;border:1px;border-color:red;");
writer.Write("}");
writer.Write(".TableStyle");
writer.Write("{");
writer.Write("border:1px;border-color: red;");
writer.Write("}");
writer.Write("</Style>");
writer.Write("<Table width='100%' cellpadding='0' cellspacing='0' Class='TableStyle'>");
SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPList lstTasks = currentWeb.Lists["Tasks"];
SPQuery sQuery = new SPQuery();
sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
SPListItemCollection myColl = lstTasks.GetItems(sQuery);
if (myColl.Count > 0)
{
writer.Write("<Tr>");
writer.Write("<Td Colspan='3' Class='blankTD'>");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='3' Class='HeadingClass'>");
writer.Write("Tasks Sample");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='3' Class='blankTD'>");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Class='HeadingClass'>");
writer.Write("Title");
writer.Write("</Td>");
writer.Write("<Td Class='HeadingClass'>");
writer.Write("Status");
writer.Write("</Td>");
writer.Write("<Td Class='HeadingClass'>");
writer.Write("Priority");
writer.Write("</Td>");
writer.Write("</Tr>");
for(int i=0;i<myColl.Count;i++)
{
string strTitle = string.Empty;
string strStatus = string.Empty;
string strPriority = string.Empty;
SPListItem item = myColl[i];
writer.Write("<Tr>");
writer.Write("<Td Colspan='3' Class='blankTD'>");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Class='NonHeadingClass'>");
strTitle = item.Title.ToString();
writer.Write(strTitle);
writer.Write("</Td>");
writer.Write("<Td Class='NonHeadingClass'>");
if (item["Status"].ToString() != "")
{
strStatus = item["Status"].ToString();
}
else

{
strStatus = "--";
}
writer.Write(strStatus);
writer.Write("</Td>");
writer.Write("<Td Class='NonHeadingClass'>");
if (item["Priority"].ToString() != "")
{
strPriority = item["Priority"].ToString();
}
else

{
strPriority = "--";
}
writer.Write(strPriority);
writer.Write("</Td>");
writer.Write("</Tr>");
}
writer.Write("<Tr>");
writer.Write("<Td Colspan='3' Class='blankTD'>");
writer.Write("</Td>");
writer.Write("</Tr>");
}
else

{
writer.Write("<Tr>");
writer.Write("<Td Colspan='3' Class='blankTD'>");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='3' Align='Center' Class='MessageClass'>");
writer.Write("No Records Available");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='3' Class='blankTD'>");
writer.Write("</Td>");
writer.Write("</Tr>");
}
writer.Write("</Table>");

#endregion

}
else if (Page.Request.Url.ToString().Contains("Groups Retrieve Sample.aspx"))
{
sgvGroupsData.RenderControl(writer);
}
else if (Page.Request.Url.ToString().Contains("Add User.aspx"))
{

#region AddUserRenderPart
string strStyle = string.Empty;
strStyle = "font-size:xx-small;";
writer.Write("<Table>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='2' Align='Center' style='"+strStyle+"'>");
writer.Write("<Strong>");
writer.Write("Add User");
writer.Write("</Strong>");
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td style='"+strStyle+"'>");
writer.Write("Add User");
writer.Write("</Td>");
writer.Write("<Td>");
peAddUser.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td style='" + strStyle + "'>");
writer.Write("Select Group");
writer.Write("</Td>");
writer.Write("<Td>");
ddlGroupName.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("<Tr>");
writer.Write("<Td Colspan='2' Align='Center'>");
btnAddUser.RenderControl(writer);
writer.Write("</Td>");
writer.Write("</Tr>");
writer.Write("</Table>");

#endregion

}
else if (Page.Request.Url.ToString().Contains("Group By Sample.aspx"))
{
sgvGroupBy.RenderControl(writer);
}
}
catch (Exception ex)
{
writer.Write(ex.ToString());
}
}
protected override void CreateChildControls()
{
try

{
if (Page.Request.Url.ToString().Contains("Add New Item.aspx"))
{

#region InsertionSampleControls
txtInsertLoginName = new TextBox();
txtInsertLoginName.ID = "txtInsertLoginName";
this.Controls.Add(txtInsertLoginName);
rfvInsertLoginName = new RequiredFieldValidator();
rfvInsertLoginName.ID = "rfvInsertLoginName";
rfvInsertLoginName.ControlToValidate = txtInsertLoginName.ID;
rfvInsertLoginName.ErrorMessage = "Please Enter UserName...!";
this.Controls.Add(rfvInsertLoginName);
txtInsertLoginPwd = new TextBox();
txtInsertLoginPwd.ID = "txtInsertLoginPwd";
this.Controls.Add(txtInsertLoginPwd);
rfvInsertLoginPwd = new RequiredFieldValidator();
rfvInsertLoginPwd.ID = "rfvInsertLoginPwd";
rfvInsertLoginPwd.ControlToValidate = txtInsertLoginPwd.ID;
rfvInsertLoginPwd.ErrorMessage = "Please Enter Password...!";
this.Controls.Add(rfvInsertLoginPwd);
btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save";
btnSave.Click += new EventHandler(btnSave_Click);
this.Controls.Add(btnSave);

#endregion

}
else if (Page.Request.Url.ToString().Contains("Search Record.aspx"))
{

#region SearchSampleControls
ddlUserName = new DropDownList();
ddlUserName.ID = "ddlUserName";
SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPList lst = currentWeb.Lists["Login_Dat001"];
SPQuery sQuery = new SPQuery();
sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
SPListItemCollection myColl = lst.GetItems(sQuery);
if (myColl.Count > 0)
{
ddlUserName.DataValueField = "ID";
ddlUserName.DataTextField = "UserName";
ddlUserName.DataSource = myColl.GetDataTable();
ddlUserName.DataBind();
}
ddlUserName.Items.Insert(0, new ListItem("Select UserName"));
this.Controls.Add(ddlUserName);
rfvSearchUserName = new RequiredFieldValidator();
rfvSearchUserName.ID = "rfvSearchUserName";
rfvSearchUserName.ControlToValidate = ddlUserName.ID;
rfvSearchUserName.InitialValue = "Select UserName";
rfvSearchUserName.ErrorMessage = "Please Select UserName...!";
this.Controls.Add(rfvSearchUserName);
txtEditPassWord = new TextBox();
txtEditPassWord.ID = "txtEditPassWord";
this.Controls.Add(txtEditPassWord);
rfvEditPassWord = new RequiredFieldValidator();
rfvEditPassWord.ID = "rfvEditPassWord";
rfvEditPassWord.ControlToValidate = txtEditPassWord.ID;
rfvEditPassWord.ErrorMessage = "Please Enter PassWord...!";
this.Controls.Add(rfvEditPassWord);
btnSearch = new Button();
btnSearch.ID = "btnSearch";
btnSearch.Text = "Search";
btnSearch.CausesValidation = false;
btnSearch.Click += new EventHandler(btnSearch_Click);
this.Controls.Add(btnSearch);
btnUpdate = new Button();
btnUpdate.ID = "btnUpdate";
btnUpdate.Text = "Update";
btnUpdate.Click += new EventHandler(btnUpdate_Click);
this.Controls.Add(btnUpdate);

#endregion

}
else if (Page.Request.Url.ToString().Contains("Delete Record.aspx"))
{

#region DeleteSampleControls
ddlDeleteUserName = new DropDownList();
ddlDeleteUserName.ID = "ddlDeleteUserName";
SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPList lst = currentWeb.Lists["Login_Dat001"];
SPQuery sQuery = new SPQuery();
sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
SPListItemCollection myColl = lst.GetItems(sQuery);
if (myColl.Count > 0)
{
ddlDeleteUserName.DataValueField = "ID";
ddlDeleteUserName.DataTextField = "UserName";
ddlDeleteUserName.DataSource = myColl.GetDataTable();
ddlDeleteUserName.DataBind();
}
ddlDeleteUserName.Items.Insert(0, new ListItem("Select UserName"));
this.Controls.Add(ddlDeleteUserName);
rfvDeleteUserName = new RequiredFieldValidator();
rfvDeleteUserName.ID = "rfvDeleteUserName";
rfvDeleteUserName.ControlToValidate = ddlDeleteUserName.ID;
rfvDeleteUserName.InitialValue = "Select UserName";
rfvDeleteUserName.ErrorMessage = "Select UserName";
this.Controls.Add(rfvDeleteUserName);
btnDelete = new Button();
btnDelete.ID = "btnDelete";
btnDelete.Text = "Delete";
btnDelete.Click += new EventHandler(btnDelete_Click);
this.Controls.Add(btnDelete);

#endregion

}
else if (Page.Request.Url.ToString().Contains("Single Row Backcolcolor Based on Status.aspx"))
{
getGridControl();
}
else if (Page.Request.Url.ToString().Contains("Multiple Row Backcolor Based on Status.aspx"))
{
getGridControl();
}
else if (Page.Request.Url.ToString().Contains("Images Based on Status.aspx"))
{
getGridControl();
}
else if (Page.Request.Url.ToString().Contains("Groups Retrieve Sample.aspx"))
{

#region bindGroupsData
sgvGroupsData = new SPGridView();
sgvGroupsData.ID = "sgvGroupsData";
sgvGroupsData.AutoGenerateColumns = false;
sgvGroupsData.Attributes.Add("RunAt", "Server");
this.Controls.Add(sgvGroupsData);
BoundField bTitle = new BoundField();
bTitle.HeaderText = "Title";
bTitle.DataField = "Title";
sgvGroupsData.Columns.Add(bTitle);
BoundField bRole = new BoundField();
bRole.HeaderText = "Role";
bRole.DataField = "Role";
sgvGroupsData.Columns.Add(bRole);
BoundField bUser = new BoundField();
bUser.HeaderText = "User";
bUser.DataField = "User";
sgvGroupsData.Columns.Add(bUser);
sgvGroupsData.DataSource = getUsersData();
sgvGroupsData.DataBind();

#endregion

}
else if (Page.Request.Url.ToString().Contains("Add User.aspx"))
{

#region AddUserControls
ddlGroupName = new DropDownList();
ddlGroupName.ID = "ddlGroupName";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb currentWeb = SPControl.GetContextWeb(Context);
using (SPSite site = new SPSite(currentWeb.Url))
{
using (SPWeb objWeb=site.OpenWeb())
{
foreach (SPGroup grp in objWeb.Groups)
{
ddlGroupName.Items.Add(grp.ToString());
}
}
}
});
ddlGroupName.Items.Insert(0, new ListItem("Select Group"));
this.Controls.Add(ddlGroupName);
peAddUser = new PeopleEditor();
peAddUser.ID = "peAddUser";
peAddUser.SharePointGroup = ddlGroupName.SelectedItem.Text;
peAddUser.MultiSelect = false;
this.Controls.Add(peAddUser);
btnAddUser = new Button();
btnAddUser.ID = "btnAddUser";
btnAddUser.Text = "Add User";
btnAddUser.Click += new EventHandler(btnAddUser_Click);
this.Controls.Add(btnAddUser);

#endregion

}
else if (Page.Request.Url.ToString().Contains("Group By Sample.aspx"))
{

#region GroupBySample
sgvGroupBy = new SPGridView();
sgvGroupBy.ID = "sgvGroupBy";
sgvGroupBy.AutoGenerateColumns = false;
sgvGroupBy.Attributes.Add("RunAt", "Server");
sgvGroupBy.AllowGrouping = true;
sgvGroupBy.AllowGroupCollapse = true;
sgvGroupBy.GridLines = GridLines.Horizontal;
sgvGroupBy.GroupField = "Status";
sgvGroupBy.DisplayGroupFieldName = false;
this.Controls.Add(sgvGroupBy);
HyperLinkField hlnkTitle = new HyperLinkField();
hlnkTitle.HeaderText = "Task Name";
hlnkTitle.DataTextField = "Title";
hlnkTitle.DataNavigateUrlFields = new string[] { "URL" };
hlnkTitle.DataNavigateUrlFormatString = "{0}";
sgvGroupBy.Columns.Add(hlnkTitle);
sgvGroupBy.DataSource = getData();
sgvGroupBy.DataBind();

#endregion

}
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
void btnAddUser_Click(object sender, EventArgs e)
{
try

{

#region AddUserToGroup
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb currentWeb = SPControl.GetContextWeb(Context);
using (SPSite site = new SPSite(currentWeb.Url))
{
using (SPWeb objWeb = site.OpenWeb())
{
objWeb.AllowUnsafeUpdates = true;
SPUser spUser = objWeb.EnsureUser(peAddUser.Accounts[0].ToString());
if (spUser != null)
{
SPGroup spGroup = objWeb.Groups[ddlGroupName.SelectedItem.Text];
if (spGroup != null)
{
spGroup.AddUser(spUser);
string strUrl=objWeb.Url+"/UserPages/Add User.aspx";
Page.Response.Redirect(strUrl);
}
}
}
}
});

#endregion

}
catch (Exception ex)
{
strError += ex.ToString();
}
}

#region BindData
public DataSet getData()
{
try

{
dt.Columns.Add("URL");
dt.Columns.Add("Title");
dt.Columns.Add("Status");
SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPList lst = currentWeb.Lists["Tasks"];
SPQuery sQuery = new SPQuery();
if (Page.Request.Url.ToString().Contains("Group By Sample.aspx"))
{
sQuery.Query = "<OrderBy><FieldRef Name='Status' Ascending='False' /></OrderBy>";
}
else

{
sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
}
SPListItemCollection myColl = lst.GetItems(sQuery);
string strTitle, strUrl, strStatus = string.Empty;
if (myColl.Count > 0)
{
foreach (SPListItem item in myColl)
{
strUrl = currentWeb.Url + "/Lists/Tasks/DispForm.aspx?ID=" + item.ID + "&Source=" + Page.Request.Url;
strTitle = item["Title"].ToString();
if (item["Status"].ToString() != "")
{
strStatus = item["Status"].ToString();
}
DataRow drow = dt.NewRow();
drow["Title"] = strTitle;
drow["URL"] = strUrl;
drow["Status"] = strStatus;
dt.Rows.Add(drow);
}
}
dSet.Tables.Clear();
dSet.Tables.Add(dt);
return dSet;
}
catch (Exception ex)
{
strError += ex.ToString();
return null;
}
}

#endregion
#region bindDataGroups
public DataSet getUsersData()
{
try

{
dtGroups.Columns.Add("Title");
dtGroups.Columns.Add("Role");
dtGroups.Columns.Add("User");
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb currentWeb = SPControl.GetContextWeb(Context);
using (SPSite site = new SPSite(currentWeb.Url))
{
using (SPWeb objWeb = site.OpenWeb())
{
foreach (SPGroup grp in objWeb.Groups)
{
foreach (SPUser user in grp.Users)
{
DataRow dr = dtGroups.NewRow();
dr["Title"] = user.Name.ToString();
dr["Role"] = grp.Name;
dr["User"] = user.LoginName.ToString();
dtGroups.Rows.Add(dr);
}
}
}
}
});
dsetGroups.Tables.Clear();
dsetGroups.Tables.Add(dtGroups);
return dsetGroups;
}
catch (Exception ex)
{
strError += ex.ToString();
return null;
}
}

#endregion
#region CreateSpGridDynamically
public void getGridControl()
{
try

{
sgvTasks = new SPGridView();
sgvTasks.ID = "sgvTasks";
sgvTasks.AutoGenerateColumns = false;
sgvTasks.Attributes.Add("RunAt", "Server");
sgvTasks.RowDataBound += new GridViewRowEventHandler(sgvTasks_RowDataBound);
this.Controls.Add(sgvTasks);
HyperLinkField hlnkTitle = new HyperLinkField();
hlnkTitle.HeaderText = "Task Name";
hlnkTitle.DataTextField = "Title";
hlnkTitle.DataNavigateUrlFields = new string[] { "URL" };
hlnkTitle.DataNavigateUrlFormatString = "{0}";
sgvTasks.Columns.Add(hlnkTitle);
BoundField bStatus = new BoundField();
bStatus.HeaderText = "Task Status";
bStatus.DataField = "Status";
sgvTasks.Columns.Add(bStatus);
sgvTasks.DataSource = getData();
sgvTasks.DataBind();
}
catch (Exception ex)
{
strError += ex.ToString();
}
}

#endregion
#region GridRowDataBound
void sgvTasks_RowDataBound(object sender, GridViewRowEventArgs e)
{
SPWeb currentWeb = SPControl.GetContextWeb(Context);
if (Page.Request.Url.ToString().Contains("Single Row Backcolcolor Based on Status.aspx"))
{
if (e.Row.Cells[1].Text == "In Progress")
{
e.Row.Cells[1].BackColor = System.Drawing.Color.Orange;
}
else if (e.Row.Cells[1].Text == "Not Started")
{
e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
}
else if (e.Row.Cells[1].Text == "Deferred")
{
e.Row.Cells[1].BackColor = System.Drawing.Color.Aqua;
}
else if (e.Row.Cells[1].Text == "Waiting on someone else")
{
e.Row.Cells[1].BackColor = System.Drawing.Color.Yellow;
}
else if (e.Row.Cells[1].Text == "Completed")
{
e.Row.Cells[1].BackColor = System.Drawing.Color.Green;
}
}
else if (Page.Request.Url.ToString().Contains("Multiple Row Backcolor Based on Status.aspx"))
{
if (e.Row.Cells[1].Text == "In Progress")
{
e.Row.BackColor = System.Drawing.Color.Orange;
}
else if (e.Row.Cells[1].Text == "Not Started")
{
e.Row.BackColor = System.Drawing.Color.Red;
}
else if (e.Row.Cells[1].Text == "Deferred")
{
e.Row.BackColor = System.Drawing.Color.Aqua;
}
else if (e.Row.Cells[1].Text == "Waiting on someone else")
{
e.Row.BackColor = System.Drawing.Color.Yellow;
}
else if (e.Row.Cells[1].Text == "Completed")
{
e.Row.BackColor = System.Drawing.Color.Green;
}
}
else if (Page.Request.Url.ToString().Contains("Images Based on Status.aspx"))
{
string strImage = string.Empty;
if (e.Row.Cells[1].Text == "In Progress")
{
strImage = currentWeb.Url + "/UserPages/Images/In Progress.JPG";
e.Row.Cells[1].Text = "<Img src='" + strImage + "' height='25px' border='0' />";
e.Row.Cells[1].ToolTip = "In Progress";
}
else if (e.Row.Cells[1].Text == "Not Started")
{
strImage = currentWeb.Url + "/UserPages/Images/Not Started.JPG";
e.Row.Cells[1].Text = "<Img src='" + strImage + "' height='25px' border='0' />";
e.Row.Cells[1].ToolTip = "Not Started";
}
else if (e.Row.Cells[1].Text == "Deferred")
{
strImage = currentWeb.Url + "/UserPages/Images/Deferred.JPG";
e.Row.Cells[1].Text = "<Img src='" + strImage + "' height='25px' border='0' />";
e.Row.Cells[1].ToolTip = "Deferred";
}
else if (e.Row.Cells[1].Text == "Waiting on someone else")
{
strImage = currentWeb.Url + "/UserPages/Images/Waiting on someone else.JPG";
e.Row.Cells[1].Text = "<Img src='" + strImage + "' height='25px' border='0' />";
e.Row.Cells[1].ToolTip = "Waiting on someone else";
}
else if (e.Row.Cells[1].Text == "Completed")
{
strImage = currentWeb.Url + "/UserPages/Images/Completed.JPG";
e.Row.Cells[1].Text = "<Img src='" + strImage + "' height='25px' border='0' />";
e.Row.Cells[1].ToolTip = "Completed";
}
}
}

#endregion

void btnDelete_Click(object sender, EventArgs e)
{
try

{

#region DeleteRecord
SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPList Login_Dat = currentWeb.Lists["Login_Dat001"];
SPListItemCollection myColl = Login_Dat.Items;
foreach (SPListItem item in myColl)
{
if (item["ID"].ToString() == ddlDeleteUserName.SelectedValue)
{
item.Delete();
ddlDeleteUserName.Items.Clear();
SPQuery sQuery = new SPQuery();
sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
SPListItemCollection myColl1 = Login_Dat.GetItems(sQuery);
if (myColl1.Count > 0)
{
ddlDeleteUserName.DataValueField = "ID";
ddlDeleteUserName.DataTextField = "UserName";
ddlDeleteUserName.DataSource = myColl.GetDataTable();
ddlDeleteUserName.DataBind();
}
Page.RegisterClientScriptBlock("alert", "<Script language='JavaScript' type='Text/JavaScript'>alert('Deletion Successful');</Script>");
break;
}
}
ddlDeleteUserName.Items.Insert(0, new ListItem("Select UserName"));
clear_Dat();

#endregion

}
catch (Exception ex)
{
strError += ex.ToString();
}
}
void btnUpdate_Click(object sender, EventArgs e)
{
try

{

#region UpdateRecord
SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPList Login_Dat = currentWeb.Lists["Login_Dat001"];
SPListItemCollection myColl = Login_Dat.Items;
foreach (SPListItem item in myColl)
{
if (item["ID"].ToString() == ddlUserName.SelectedValue)
{
item["PassWord"] = txtEditPassWord.Text;
item.Update();
clear_Dat();
Page.RegisterClientScriptBlock("alert", "<Script language='JavaScript' type='Text/JavaScript'>alert('Updation Successful');</Script>");
break;
}
}

#endregion

}
catch (Exception ex)
{
strError += ex.ToString();
}
}
void btnSearch_Click(object sender, EventArgs e)
{
try

{

#region searchRecord
if (ddlUserName.SelectedIndex != 0)
{
SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPList Login_Dat = currentWeb.Lists["Login_Dat001"];
SPQuery sQuery = new SPQuery();
sQuery.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + ddlUserName.SelectedValue + "</Value></Eq></Where>";
SPListItemCollection myColl = Login_Dat.GetItems(sQuery);
if (myColl.Count > 0)
{
SPListItem item = myColl[0];
txtEditPassWord.Text = item["PassWord"].ToString();
}
}
else

{
Page.RegisterClientScriptBlock("alert", "<Script language='JavaScript' type='Text/JavaScript'>alert('Please Select UserName');</Script>");
}

#endregion

}
catch (Exception ex)
{
strError += ex.ToString();
}
}
void btnSave_Click(object sender, EventArgs e)
{
try

{

#region SaveRecord
SPWeb currentWeb = SPControl.GetContextWeb(Context);
SPList Login_Dat = currentWeb.Lists["Login_Dat001"];
SPQuery sQuery = new SPQuery();
sQuery.Query = "<Where><Eq><FieldRef Name='UserName' /><Value Type='Text'>" + txtInsertLoginName.Text.ToUpper() + "</Value></Eq></Where>";
SPListItemCollection myColl = Login_Dat.GetItems(sQuery);
if (myColl.Count > 0)
{
Page.RegisterClientScriptBlock("alert", "<Script language='JavaScript' type='Text/JavaScript'>alert('Record already Available');</Script>");
}
else

{
SPListItem item = Login_Dat.Items.Add();
item["UserName"] = txtInsertLoginName.Text;
item["PassWord"] = txtInsertLoginPwd.Text;
item.Update();
Page.RegisterClientScriptBlock("alert", "<Script language='JavaScript' type='Text/JavaScript'>alert('Insertion Successful');</Script>");
clear_Dat();
}

#endregion

}
catch (Exception ex)
{
strError += ex.ToString();
}
}
public void clear_Dat()
{
try

{

#region ClearControls
if (Page.Request.Url.ToString().Contains("Add New Item.aspx"))
{
txtInsertLoginName.Text = "";
txtInsertLoginPwd.Text = "";
txtInsertLoginName.Focus();
}
else if (Page.Request.Url.ToString().Contains("Search Record.aspx"))
{
ddlUserName.SelectedIndex = 0;
txtEditPassWord.Text = "";
ddlUserName.Focus();
}
else if (Page.Request.Url.ToString().Contains("Delete Record.aspx"))
{
ddlDeleteUserName.SelectedIndex = 0;
ddlDeleteUserName.Focus();
}

#endregion

}
catch (Exception ex)
{
strError += ex.ToString();
}
}
}
}

No comments:

Post a Comment