jQuery event conflicts with ASP.NET Update Panel

There is a problem when I attached click event to ASP page inside Update Panel control.
I put click event onto a button using jQuery.

$("#btn").click(function () {
	alert("clicking button");
});

After asynchronous load is triggered by update panel, clicking that button will not show any alert anymore.
Seems click event are detached.
The fact is any event control which put using client side or jQuery will failed inside update panel. Why?

Continue reading

Advertisements

Sending email for SharePoint, with/without attachment

One of great things we can do with Sharepoint is sending an email. The most easiest way is using SPUtility. This will work if you have set up Outgoing email in Central Admin.
outgoing email

private void SendEmail()
{
	// Build the email message headers
	StringDictionary headers = new StringDictionary();
	headers.Add("to", "user@email.com");
	headers.Add("subject", "Item Records");	
	headers.Add("content-type", "text/html");
	
	// Send the email
	SPSecurity.RunWithElevatedPrivileges(delegate()
	{
		using (var elevatedSite = new SPSite(SPContext.Current.Site.ID))
		{
			using (var elevatedWeb = elevatedSite.OpenWeb())
			{
				SPUtility.SendEmail(elevatedWeb, headers, GetEmailBody());
			}
		}
	});
}

private String GetEmailBody()
{
	StringBuilder sb = new StringBuilder();
	sb.Append(String.Format("<p>Below is the {1} records on {0:dd MMM yyyy HH:mm}.</p>", DateTime.Now, "Item List"));
	
	return sb.ToString();
}

Continue reading

Export/Import CSV file from/to listitem Sharepoint

We are able to export items from Sharepoint list into CSV files (comma-separated-values) and do opposite too, import csv into Sharepoint list.

Export : using codes below, you will get csv as download-file. Prepare a list inside your site, named it “Item List”, with 3 columns : Title, Price, Description.

private void ExportToCsv()
{
	Response.Clear();
	Response.Buffer = true;
	Response.AddHeader("content-disposition", "attachment;filename=Itemrecord.csv");
	Response.Charset = "";
	Response.ContentType = "application/text";
		
	// get data
	DataTable dtItem = GetDataFromList();
	StringBuilder sb = new StringBuilder();
	if(dtItem != null)
	{
		// separate each column with comma
		foreach (DataColumn dc in dtItem.Columns)
		{
			sb.Append(dc.ColumnName + ',');
		}
		
		// newline for value
		sb.AppendLine();
		
		// get value
		foreach (DataRow dr in dtItem.Rows)
		{
			foreach (DataColumn dc in dtItem.Columns)
			{
				sb.Append(dr[dc.ColumnName].ToString() + ',');
			}
			sb.AppendLine();
		}
		
		Reponse.Output.Write(sb.ToString());
		Response.Flush();
		Response.End();
	}
}

private DataTable GetDataFromList()
{
	// I have a list 'Item List' with 3columns
	SPQuery query = new SPQuery();
	query.ViewFields = "<FieldRef Name='Title' />
						<FieldRef Name='Price' />
						<FieldRef Name='Description' />";
	SPList list = SPContext.Current.Site.RootWeb.Lists.TryGetList("Item List");
	SPListItemCollection itemColl = list.GetItems(query);
	
	return itemColl.GetDataTable();
}

As result, you will get file “Itemrecord.csv”, contains firstline as column name, nextline as values.

Continue reading

Click Event on Row Gridview

Recent days, I got requirement to make gridview clickable and click event has to redirect user to Edit page. After few googling, I found a simple way to do it. Actually I did this for SPGridview (Sharepoint) and it’s also work for ASP gridview. I’ll try to expose the different of them in next post later.

<SharePoint:SPGridView ID="gridView" runat="server" OnRowDataBound="gridView_RowDataBound" OnSelectedIndexChanged="gridView_SelectedIndexChanged" DataKeyNames="ID" >

Continue reading