Understanding IsPostback
Posted: 17/11/2011 Filed under: Development | Tags: ASP.NET Leave a comment »The IsPostback is probably one of the most commonly used property in ASP.NET development.
But what is it exactly?
It is a property that belongs to the Page class under the System.Web.UI namespace. Essentially it returns a boolean value indicating if a page is being requested for the first time or if is being loaded in respond to a postback.
Please elaborate?
Well, when we talk on the web, we use the HTTP protocol. The communication or exchange of information between a client (web browser) and an application or site (web browser) is initiated when we type in a site URL or click on a hyperlink. This is known as a Request (GET), the site or application then Responds to the client. This process is known as a round-trip.
However, we the client, also interact with a site or application through the execution of a particular web control (such as a button click). This is when data is sent to the site or application, a POST is made, and this process is known as a postback.
Important! – We can send data back using a GET as well, but this is not discussed here.
So, the IsPostback property on a ASP.NET page essentially checks if the request is either a GET or POST.
Example
The code snippet below basically shows an array is bound to a drop-down list when a page loaded.
1: string arrPets = new string[] {"Dog", "Cat", "Bird", "Fish"};
2:
3: public void Page_Load(object sender, EventArgs e)
4: {
5: // The pet values from the array is not bound here.
6: // This is because the when the page is requested
7: // for the first time, it is not a postback
8: // There will be no values on the drop-down
9: // list control.
10: // =================================================
11: // COMMENTED OUT
12: //if(this.Page.IsPostback)
13: //{
14: //this.dropDownList.DataSource = arrPets;
15: //this.dropDownList.DataBind();
16: //}
17:
18: // The pet values from the array will be bound here.
19: // The values will be available on the drop-down
20: // list control.
21: // ====================================================
22: if(!this.Page.IsPostback)
23: {
24: this.drownDownList.DataSource = arrPets;
25: this.dropDownList.DataBind();
26: }
27: }
Easy Progress Indicator using jQuery
Posted: 11/11/2011 Filed under: Development | Tags: jQuery Leave a comment »This is a truly simplified approach to providing end-users with an indicator that an operation is in progress.
The page markup:
The script:
Pre-Populating SharePoint Fields
Posted: 20/10/2011 Filed under: Development | Tags: Client Side Code, JavaScript, jQuery Leave a comment »What if you needed to pre-populate your SharePoint fields with data based on user selection?
For example if a user’s selected country is Australia, then the Country field on your EditForm.aspx page will be pre-populated with the value Australia.
You can achieve this through QueryStrings and jQuery/JavaScript.
In my example I simply directed the user to a page via-URL with QueryString, such as below:
http://intranet/Pages/default.aspx?Country=Australia
.aspx?=.aspx?=http://<url>.aspx?<FieldName>=<UserSelectedValue>
Where <FieldName> is the Internal field name and NOT the display name, and <UserSelectedValue> is a value obtain from a user’s input, such as a textbox, drop-down list, checkbox etc etc.
NOTE: It is important to check for special characters and change them accordingly. For example, a space is replaced with %20
Once you have the <FieldName> and <UserSelectedValue>, you can then retrieve them and populate the SharePoint field quite easily. Basically, you will need to grab the URL from the current location, then separate your QueryStrings into an Array, as you may have more then one NameValue pair. You will then need to iterate through the Array find the relevant <FieldName> and use it’s corresponding <UserSelectedValue> on the SharePoint field. In my example the snippet is as below:
<script type="text/JavaScript">
$(window).load(function() {
// Get URL from the current location, then separate your QueryStrings into an Array
var queryString = location.search.replace(‘?’, ”).split(‘&’);
// iterate through the Array find the relevant <FieldName> and use it’s corresponding
// <UserSelectedValue> to update the SharePoint field
for(var i=0; i<queryString.length; i++)
{
var value = queryString[i].split(‘=’)[1];
if(queryString[i].split(‘=’)[0] == "Policy")
{
$("#<SharePointFieldControlID>").val(value);
};
};
});
</script>
NOTE: Remember to check for special characters and change them accordingly.
iTextSharp and SharePoint
Posted: 12/05/2011 Filed under: Development | Tags: Development, iTextSharp 7 Comments »
For those that have not heard of iTextSharp, it is a free downloadable library that is use for pdf generation, formatting and a whole lot more. I myself was introduced to it by Tommy Segoro. I must admit that it does take some effort to get into the workings of it, but the payoff is just superb.
I don’t intend to introduce you to the wonders of this library, but if you have not played with it, then I recommend paying a visit to Mikesdotnetting. He has a wonderful series of post that gets your familiarised with iTextSharp.
My snippet below just shows how to save a generated pdf into a SharePoint library directly. Hope it helps, happy days.
Document pdfDoc = new Document();
MemoryStream ms = new MemoryStream();
PdfWriter.GetInstance(pdfDoc, ms);
pdfDoc.Open();
pdfDoc.Add(new Paragraph("This is a demo document created with iTextSharp."));
pdfDoc.Close();
// Save document to a SharePoint document library
SPWeb oWeb = SPContext.Current.Web;
SPFolder oLibrary = oWeb.GetFolder("Demo");
string fullUrl = oWeb.Url + "/" + oLibrary.Url + "/demo.pdf";
byte[] data = ms.GetBuffer();
SPFile oFile = oLibrary.Files.Add(fullUrl, data);
oFile.Update();
HttpPostedFile FileName
Posted: 17/01/2011 Filed under: Development | Tags: C# Leave a comment »When we use HttpPostedFile FileName property to get a particular file name, we are resigned to not only getting the file name but the full file path as well. For example, instead of just the file name “sample.docx”, we get “C:\FilePath\sample.docx”
So the question is, can we just retrieve the file name without the file path? Yes, we can and the easiest option would be to use the Path class (see code line below).
// Option to retrieve only the file name
// fileName is the string variable that holds the value
string fileName = Path.GetFileName(PostedFileObject.FileName);
SPEncode.UrlEncode returns %2b instead of %20
Posted: 10/08/2010 Filed under: Development | Tags: API, Development Leave a comment »SharePoint URL empty spaces are translated as %20. Hence, in order to ensure that are URL’s are reference correctly we would use the UrlEncode method of the SPEncode class. Be aware though, that this will return %2b instead of %20 as we would have been hoping for. In order to get around this and still have my URL’s reference correctly I use the string Contains and string Replace methods. Code snippet as below:
private string fullUrl = “<some url>”
if(fullUrl.Contains(“ ”))
{
fullUrl.Replace(“ ”, “%20”);
return fullUrl;
}
Create a List Item (70-541)
Posted: 18/12/2009 Filed under: Certification, Development | Tags: 70-541, Certification Leave a comment »SPSite oSite = new SPSite("
http://rootsite
");
SPWeb oWeb = oSite.AllWebs["subsite"];
SPListItemCollection oItems = oWeb.Lists["list title"].Items;
// Create a New Item
SPListItem oItem = oItems.Add();
oItem["field"] = "value";
// Code that populates other fields
oItem.Update();
// Clean up
oWeb.Dispose();
oSite.Dispose();
Delete a List Item (70-541)
Posted: 18/12/2009 Filed under: Certification, Development | Tags: 70-541, Certification Leave a comment »SPSite oSite = new SPSite("
http://rootsite
");
SPWeb oWeb = oSite.AllWebs["subsite"];
SPListItemCollection oItems = oWeb.Lists["list title"].Items;
// Enumerate through all list items
foreach (SPListItem oItem in oItems)
{
// Delete a List Item
if(oItem["field"].ToString() == "value")
{
oItem.Delete()
return;
}
}
// Clean up
oWeb.Dispose();
oSite.Dispose();
Note, the return statement right after the item has been deleted. You do not want to keep looping through all list items once we’ve deleted the one required, as you will be thrown an exception.
Update a List Item (70-541)
Posted: 18/12/2009 Filed under: Certification, Development | Tags: 70-541, Certification Leave a comment »SPSite oSite = new SPSite("
http://rootsite
");
SPWeb oWeb = oSite.AllWebs["subsite"];
SPListItemCollection oItems = oWeb.Lists["list title"].Items;
// Enumerate through all list items
foreach (SPListItem oItem in oItems)
{
// Update a List Item
if(oItem["field"].ToString() == "value")
{
oItem["field"] = "new value";
oItem.Update();
return;
}
}
// Clean up
oWeb.Dispose();
oSite.Dispose();
Enumerate List Items (70-541)
Posted: 18/12/2009 Filed under: Certification, Development | Tags: 70-541, Certification Leave a comment »SPSite oSite = new SPSite("
http://rootsite
");
SPWeb oWeb = oSite.AllWebs["subsite"];
SPListItemCollection oItems = oWeb.Lists["list title"].Items;
// Enumerate Through All List Items
foreach (SPListItem oItem in oItems)
{
// Additional Code
}
// Clean Up
oWeb.Dispose();
oSite.Dispose();