Wednesday, August 17, 2011

Dynamic Load or Add User Control and Maintain the View State

In this post I will explain you how to add or load user control in a page or any other user control and maintain the view state.Normally we are facing problem when we load user control on button click or any business condition in this case when page will post back control will lost its view state or in other words losing form field values on post back.This is happen because we are adding user control after page Load View State Stage for further detail you should learn asp.net page lifecycle.That means, if we want our dynamic controls to persist view state itself we must add them to the control hierarchy in the page's Init event.

Here, I will explain the example when we want to add user control on any event.I have two user controls one is Grid view and second I will take login control and load on it into two submit buttons.
 
Adding Reference or Directive on a Webpage
 
The first thing we have to do, is declare our UserControls. It can be done either in each page where it's used, or globally in the web.config file. There is no performance difference, but when declaring UserControls in the web.config file, the controls have to reside in a different directory than the page(s) using it for further detail check my pervious post Register Custom User Control in Web.Config File

So in the webform, add the following directive:

<%@ Register src="Controls/GridView.ascx" tagname="GridView" tagprefix="uc1" %>

<%@ Register src="Controls/LoginControl.ascx" tagname="LoginControl" tagprefix="uc2" %>

Loading dynamically

For loading user control on a page you should need to drag and drop the place holder where you want to show user control.

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

Also add the two buttons to add user control on button click .
<asp:Button ID="btnGrindControl" runat="server" Text="Button" />
<asp:Button ID="btnLoginControl" runat="server" Text="Button" />
your aspx page look like this,

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Src="Controls/GridView.ascx" TagName="GridView" TagPrefix="uc1" %>
<%@ Register Src="Controls/LoginControl.ascx" TagName="LoginControl" TagPrefix="uc2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

<asp:Button ID="btnGrindControl" runat="server" Text="Button" />
<asp:Button ID="btnLoginControl" runat="server" Text="Button" />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

</asp:Content>



Code Behind

In the Code Behind of the page, we add the control like this:
PlaceHolder1.Controls.Add(LoadControl("~/UserControl.ascx"));


In order to maintain the Viewstate of user control you should maintain the UserControl complete path and used this path every time on postback and load the control.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
public string ControlPath
{
get
{
if (ViewState["controlPath"] != null)
return Convert.ToString(ViewState["controlPath"]);
else
return null;
}

set
{
ViewState["controlPath"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
LoadUserControl();

}
private void LoadUserControl()
{
if (!string.IsNullOrEmpty(ControlPath))
{
PlaceHolder1.Controls.Clear();
UserControl uc = (UserControl)LoadControl(ControlPath);
PlaceHolder1.Controls.Add(uc);
}
}



protected void btnGrindControl_Click(object sender, EventArgs e)
{
ControlPath = "Controls/GridView.ascx";
LoadUserControl();
}
protected void btnLoginControl_Click(object sender, EventArgs e)
{
ControlPath = "Controls/LoginControl.ascx";
LoadUserControl();
}
}


In above example I create one property name “ControlPath” for maintaining the Url of user control on button click .In LoadUserContol() I am just clearing the placeholder and adding the user control.In postback user control is adding everytime beacuse  user control was added after the page load and view state stage .There are few good articles written by Scott Mitchell on dynamically loading Controls which you will find in the following links:


Conclusion

In this article we saw how to work with dynamic controls so that their values and view state can be correctly persisted across postbacks.

Wednesday, August 3, 2011

Debugging Asp.net application using XML Serialization

 

Debugging on Test or Production server is very difficult task for developers.First of all developers do not have any rights to touch the Test or Production environment and he received the list of bugs mainly due to data problem on production or Test Server.When developers try to reproduce the bugs on development environment is not possible because data development environment is accurate.So I have found out the solution of this problem through XML Serialization techniques which describe as below:-

In this article I am taking a example of simple Employee Detail.I will explain how we can debug using XML Serialize data.

Example

First I have declare a Employee class and create one method for creating employee details.

public class Employee
{
public string EmployeeName { get; set; }
public string Address { get; set; }
public int MobilNo { get; set; }


public List<Employee> GetEmployees()
{
List<Employee> Emplst = new List<Employee>();

for (int i = 0; i < 5; i++)
{
Employee emp = new Employee();
emp.EmployeeName = "Employee" + "-" + i.ToString();
emp.Address = "Address" + "-" + i.ToString();
emp.MobilNo = 9899 + (i + 2);
Emplst.Add(emp);
}
return Emplst;
}

}



 



Now create one page and call the GetEmployee() Method and fill the grid.



 public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Employee objEmp= new Employee();

GridView1.DataSource = objEmp.GetEmployees();
GridView1.DataBind();
}
}





Scenario 1



It is very simple code to populate grid from GetEmployees() method but you assume that data is coming from the database and populating the grid and at this time you are facing problem .Now you can track or debug the application with XML Serialization.



protected void Page_Load(object sender, EventArgs e)
{
Employee objEmp = new Employee();

SaveXMLData(objEmp.GetEmployees(), "Employee.XML");
GridView1.DataSource = ReadXMLData<Employee>("Employee.XML");
GridView1.DataBind();
}


#region For Serialization Code

public void SaveXMLData<T>(List<T> list, string fileName)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
{
TextWriter writer = new StreamWriter(Server.MapPath("~\\XMLFiles") + "\\" + fileName);
serializer.Serialize(writer, list);
writer.Close();

}
}

public List<T> ReadXMLData<T>(string filename)
{
XmlSerializer deserializer = new XmlSerializer(typeof(List<T>));
TextReader textReader = new StreamReader(Server.MapPath("~\\XMLFiles") + "\\" + filename);
List<T> list;
list = (List<T>)deserializer.Deserialize(textReader);
textReader.Close();

return list;
}

#endregion
}


If you see the code on page load I have save the data first into XML and then retrieve and display into the grid due to this every time save data into XML So if  you find any problem in module you will get the XML file from Production or Test Server and Load into development environment through ReadXMLData() method and debug it.



Here is the XML Generate file from the above SaveXMLData() method.



<?xml version="1.0" encoding="utf-8" ?> 
- <ArrayOfEmployee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <Employee>
<EmployeeName>Employee-0</EmployeeName>
<Address>Address-0</Address>
<MobilNo>9901</MobilNo>
</Employee>
- <Employee>
<EmployeeName>Employee-1</EmployeeName>
<Address>Address-1</Address>
<MobilNo>9902</MobilNo>
</Employee>
- <Employee>
<EmployeeName>Employee-2</EmployeeName>
<Address>Address-2</Address>
<MobilNo>9903</MobilNo>
</Employee>
- <Employee>
<EmployeeName>Employee-3</EmployeeName>
<Address>Address-3</Address>
<MobilNo>9904</MobilNo>
</Employee>
- <Employee>
<EmployeeName>Employee-4</EmployeeName>
<Address>Address-4</Address>
<MobilNo>9905</MobilNo>
</Employee>
</ArrayOfEmployee>




Scenario 2



If you are thinking for every time save and read data from XML then application performance will affected so you can change the code like this only when exception will raise it will save the data into xml and then if you want to debug write ReadXMLData() method in a code where you want.



protected void Page_Load(object sender, EventArgs e)
{
try
{
Employee objEmp = new Employee();
GridView1.DataSource = objEmp.GetEmployees();
GridView1.DataBind();
}
catch (Exception ex)
{
SaveXMLData(objEmp.GetEmployees(), "Employee.XML");
throw;
}

}



Conclusion






We have seen in above samples that how we can take advantage of serialization mechanism provided by .Net. In this tutorial I have tried to keep things as simple as possible so you can understand and using the basic concepts of XML serialization for debugging the application. Feel free to share your comments.

Saturday, July 23, 2011

Validate different controls on different buttons in a same page

 

Normally developers have a requirement to validate different controls on different buttons in this situation developers face problem when user click on any button all validation will be fire which are not belongs to that button functionality.

ASP.NET V2 introduces a new “ValidationGroup” property on validation and input controls that now makes this possible. This allows page developers to group different controls together for more granular validation behavior.

For example two groups – a “Group1” and a “Group2” of validators. There are then two buttons on the page – when button1 is clicked, the first group of validators will fire. When button2 is clicked, the second group of validators will fire. Postback will be blocked client-side by default if the validation fails:

 

Client Side Validation

<html>
<body>
     <form runat=“server”>
          <asp:textbox id=“TextBox1” runat=“server”/>
          <asp:requiredfieldvalidator ValidationGroup=“Group1” ErrorText=“Need to Fill in Value!” ControlToValidate=“TextBox1” runat=“server”/>
            <asp:textbox id=“TextBox2” runat=“server”/>
            <asp:requiredfieldvalidator ValidationGroup=“Group2” ErrorText=“Need to Fill in Value!” ControlToValidate=“TextBox2” runat=“server”/>
            <asp:button text=“Group1” ValidationGroup=“Group1” runat=“server”/>
            <asp:button text=“Group2” ValidationGroup=“Group2” runat=“server”/>
     </form>
</body>
 
</html>
 

The code given above has two validation groups. The first validation groups is identified as “First” and the second validation group is identified as “Second”. Each validation group has two textboxes, a RequiredField validator and a button. If you look at the code you might see that each control has a property called “ValidationGroup” and its value is set to the name of the validation group to which that control belongs.


Clicking the “Submit1” button initiates the validation of the first group and throws an error message if the TextBox1 is left blank. Since this button belongs to the ‘First’ validation group, it initiates the validaton controls that belongs to that group. Similarly clicking the ‘Submit2’ button throws an error message if the TextBox3 is left blank. Thus the validation groups help in grouping the controls in a single web page allowing you to have separate validations for different controls while allowing you to submit values in a particular group.


 


Server Side Validation


If you want to validate on server side just call page.Validate(“Group Name”) on button click event it will automatically call respective group.You don’t have need to define specific group on html code.



<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" Runat="server" ValidationGroup="First"></asp:TextBox>
<asp:TextBox ID="TextBox2" Runat="server" ValidationGroup="First"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server" ValidationGroup="First"ErrorMessage="TextBox1 should not be blank" ControlToValidate="TextBox1"> </asp:RequiredFieldValidator>
<asp:Button ID="Submit1" Runat="server"  Text="Submit 1" />
 <br /><br /> 
<asp:TextBox ID="TextBox3" Runat="server" ValidationGroup="Second"></asp:TextBox> 
<asp:TextBox ID="TextBox4" Runat="server" ValidationGroup="Second"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server" ErrorMessage=" TextBox3 should not be blank"ControlToValidate="TextBox3" ValidationGroup="Second"></asp:RequiredFieldValidator>
 <asp:Button ID="Submit2" Runat="server"  Text="Submit 2" />
 </div>
</form>
</body> 

 



protected void Submit1_Click(object sender, EventArgs e)
{
 
Page.Validate("First");
 
if(Page.IsValid)
    {
        // Continue
    }
}
 
protected void Submit2_Click(object sender, EventArgs e)
{
 
Page.Validate("Second");
 
if(Page.IsValid)
    {
        // Continue
    }
}
  




      

Monday, May 23, 2011

Define Access Key In asp.net button control

Flickr Tags: ,

If you want to underline and define the access key in asp.net just like you define in windows application , use the html button element. To produce the same behavior as an asp.net Button control. I set the runat attribute to "server" and use the onclick event to call javascript and onServerClick event to call a server side method.


Here is the code for the page,

<form id="form1" runat="server">



<button id="hbutton1" runat="server" type="submit" accesskey="A" onserverclick="hbutton1_Click" onclick="alert('test');"><u>A</u>dd</button>



</form>




Code-behind, 
 





protected void hbutton1_Click(object sender, EventArgs e)



       {



           Response.Write("hbutton clicked");



       }




When, the page is run, a button is generated with text "Add". When clicked, at first an alert message is shown and then the page postbacks.

Sunday, May 22, 2011

Register Custom User Control in Web.Config File

 

Normally we register any user control or assemblies in page directive of aspx html source if we want to used in any aspx page like.

 


<%@ Register TagPrefix="MyControl" TagName="HeaderControl" Src="Header.ascx" %>
<%@ Register TagPrefix="MyControl" TagName="footerControl" Src="Footer.ascx" %>
<%@ Register TagPrefix="MyAssembly" Assembly="Myassembly" %>
 
 
IF you have header and footer user control and you want to adding on every page or more than one pages then you don’t need to register on every page just register only one time in web.config file and used it multiple time like this way.


<?xml version="1.0"?>
<configuration>
<system.web>
 
        <pages>
        <controls>
        <add tagPrefix="MyControl" src="~/Header.ascx"
        tagName="HeaderControl"/>
        <add tagPrefix="MyControl" src="~/Footer.ascx"
        tagName="footerControl"/>
        <add tagPrefix="MyAssembly" assembly="MyAssembly"/>
        </controls>
        </pages>
        
</system.web>
</configuration>



Sunday, April 17, 2011

Cookieless Session State Asp.net

Cookieless Session State Asp.net

ASP.NET Session State by default uses a cookie to store session ID. Session ID is a unique string, used to recognize individual visitor between visits. But, if client's web browser doesn't support cookies or visitor has disabled cookies in web browser's settings, ASP.NET can't store session id on client's machine. In this case, new session will be created for every request. This behavior is useless because we can't remember information for certain visitor between two requests. We can say that, by default, sessions can't work if browser doesn't support cookies.

ASP.NET sessions without cookies (cookieless session)

ASP.NET supports sessions without cookies, known as cookieless sessions. As an alternative, ASP.NET can embed session id inside of page URL. On this way, all page links will contain session id string. When visitor clicks on some of these links, ASP.NET will read session id from page URL, so we don't need cookies support to have functional sessions.

To enable cookieless sessions, add cookieless="true", or cookieless="UseUri" (both options have the same meaning) inside sessionState tag in web.config file, like this:

<sessionState cookieless="true" />

SessionState element in web.config file is located inside <configuration>, <system.web> elements. Default behavior for sessions with cookies could be set with cookieless="false". Since it is already specified in machine.config file and inherited to all ASP.NET applications on server, you don't need any configuration change if you use cookie sessions.

As a result of cookieless="true", session id will be embedded in all page URLs. The drawback is that you'll end up with ugly URLs, which are not so good for SEO (search engine optimization) and visitor definitely will not remember it. Here is an example URL of website which uses ASP.NET cookieless sessions:

http://mappoint.msn.com/(22b5c4zyybphaw2mt3hjni2n)/Home.aspx

Bolded part represents session id, which is used to recognize a visitor. Possible problem is that URL will be different for every visitor and every visit, because they will have different session identities. If user just copy URL and paste it somewhere while recommending your website, he or she will send its session id too. This could be potential security risk if session is not expired yet. Also, search engines will consider any of these links as separate page. This will divide page ranking and decrease position of your page in search engine results.

Cookieless session and absolute URLs problem

There is a problem with absolute URLs if Cookieless session is used (e.g. URLs which starts with http://, https:// etc.), and with URLs which starts with slash character "/". So, you can't use links like /products/Product1.aspx anymore. If you place absolute link on your page, ASP.NET will not embed session id in it. If visitor clicks on link without current session id, he or she will get new session with new session id. Previous session and its data will be lost. With every next click on absolute links, visitor will get new session each time. On this way session becomes useless, because it cant remember user's data between two requests. So, easiest solution is to simply forget links with absolute URLs on cookieless website.

Other option is to embed session id into absolute URLs by using Response.ApplyAppPathModifier method. This could be useful, for example if we read and display a list of absolute URLs from database, or perform redirection from http:// to https:// etc. Here is a simple example of modification of absolute URL. Resulting URL will contain embedded current session id:

<a id="Hyperlink1" runat="server" href='<% =Response.ApplyAppPathModifier("http://mysite.com/Some-Absolute-Link.aspx"); %>' >Absolute link with cookieless session example</a>

In the other hand, absolute links are pretty useful for static files like images, .js, .css, .zip files etc.

Cookieless session and links to images, JavaScript files, CSS files etc.

When cookieless="true" is used, every relative link will be rewritten to embed session id. That includes links to files like images, JavaScript files, CSS files, PDF files, ZIP files for download etc.

In most cases, you don't need session id embedded inside of these links. Embedded session id inside of links to images and other similar files could cause cache problems.

For every new visit, visitor will get different session id. Thus, same image will always have different URL. Different URL means different image for web browser. If you check "Temporary Internet Files" in Internet Explorer or some other browser, notice that same image is saved many times. Actually, it is saved again each time you visit cookieless website. This could be a problem, especially on high traffic websites. In this case, cookieless session could increase bandwidth costs and slow down page loading, since same files must be downloaded multiple times and stored separately.

As a simple solution, you can use absolute links for images, JavaScript, CSS, PDF and similar files and avoid session id issue on this way.

ASP.NET cookieless options

ASP.NET offers six different options for cookieless attribute.

UseCookies - This is default option, set in machine.config file. ASP.NET website will use cookies to store session id, even if client doesn't support them. In case that client refuse to save cookie, session data will be lost and new session is created for each request. Thus, clients without cookie support can't use web application.

false - has same meaning as UseCookies.

UseUri - Website will embed session id inside of all relative URLs. ASP.NET Session State will never use cookies, even if client browser supports them. Sessions will work on each client, but could be less secure than session with cookie.

true - same as UseUri.

UseDeviceProfile - ASP.NET will check client if it's capable to support cookies. If client can support cookies, cookie will be used to store session id. Notice that this method just checks browser's features, but it doesn't consider an option that user is manually disabled cookies in browser. If web browser application supports cookies but user is disabled them, session will not work.

AutoDetect - ASP.NET will automatically detect if client supports cookies or not. If client supports cookies, website will use a cookie as storage for session id. If client doesn't support cookies, ASP.NET will embed session id in page URL. This looks as good compromise, although could cause problems with search engines.

Cookieless sessions and RegenerateExpiredSessionId parameter

To enable cookieless session, it is enough to set appropriate value for cookieless parameter. But, sessionState element contains RegenerateExpiredSessionId parameter which is important for security reasons. RegenerateExpiredSessionId could have two values:

True - Expired session ids can't be used again. If visitor comes to website using old session id, it will be replaced with new session id

False - If visitor uses old session id to visit website (e.g. through a bookmark, search engines, social networks etc.), website will create new session collection but session id will stay same.

For security reasons, especially with cookieless sessions, is better to always use RegenerateExpiredSessionId="True". On this way expired session id will not be used for new session, to avoid that different visitors have same session id.

SEO (Search Engine Optimization) and cookieless sessions

Search engines like Google could provide a lot of visits to website. Everything begins when Googlebot, or some others' search bot application, make requests to website to load its content and save it to their database for further analyze. Saved data are then provided for search on search engine, and possibly listed in search results.

Important issue when working with cookieless sessions is duplicate content. Let's suppose that you have one page with unique content. Each time when search bot reads that page, ASP.NET will assign different session id to it. Different session id means different URLs on page. So, instead to see your page as one page with original content, search bot could consider that you have a lot of different pages with same content. That is not good for search engine optimization, because your page will divide its ranking and be shown on the bottom of search results where no one clicks.

The solution for this could be to use cookieless="UseDeviceProfile" and then add new browser profile for each search bot inside <browsers> element in web.config. For example, if user agent string contains word "Googlebot", browser profile will tell ASP.NET that Googlebot supports cookies. On this way, session id will not use cookies when Google bot comes. Of course, we know that Googlebot doesn't support cookies, so this sounds more like hack than recommended nice looking solution. But, it will work and you can add new browser profile for any problematic search bot. Notice that UseDeviceProfile only looks on browser capabilities, thus on this way you are losing visitors with capable browsers, but who manually disabled cookies.

One more potential SEO problem with cookieless sessions is HTTP 302 redirect. When, search bot makes requests to some domain which uses cookieless sessions, ASP.NET will redirect visitor to other link with session id embedded. During this process, ASP.NET will use HTTP 302 Found message. Code 302 could have two meanings: 303 or 307. "303 See Other" means that correct content is found on different URL. 307 response represents temporary redirect. So, correct status code should be 307, temporary redirect, because in that case main link without session id is considered as the correct one. Unfortunately, bad architecture of ASP.NET cookieless session redirects using vague 302 message which is more often perceived as 303, than as 307. The consequence is that search bots usually save URL with session id embedded and even worse, see different pages each time when crawl through website.

Fortunately, main search engines learned how to deal with session variables, so now the problem is not so big as before. Still, clean URLs with keywords only will always rank better in search results, thus I can say that cookieless sessions generally are not good for SEO.

Security issues and cookieless session

Main security problem when working with sessions is a possibility that malicious user will find out others' session id. If two users share same session id, they share same session variables too and website is considering them as one visitor. This could be a security risk if session is used for any private or sensitive data, or to allow access to restricted areas of web site. When cookies are used, session id can be protected using SSL and by marking a cookie as secure. But, in case of cookieless session, session id is part of URL and is much more vulnerable.

Attacker could use network monitoring tool to see requested URLs. Because session id is part of URL, if you know requested URL you know session id too. Also, if malicious user get access to recent web server's logs, it is possible to read recent requested URLs and visit website with still active session.

On more problem is e-mail, social network or instant messenger communication. Pretty innocent question, as "Can you send me link to that page please?" could be huge security risk. Visitor could copy link from address bar and send it to someone through an email, some instant messenger, like AIM, Windows Live Messenger, Skype etc., or through social networks like Facebook. When other person use received link to open website, he or she will share same session as sender, and possibly see private data or have access to restricted area of web site.

Search bots generally don't accept cookies. That means that bots will get cookieless session and see URLs with embedded session id. If these URLs appear in search results, they will still contain that session id. So, imagine that first visitor comes to your website from Google search. Website will read session id from given URL. Then, after few minutes, second visitor comes using the same link. Now these two, and every next visitor that comes from search engine will share same session id, and same session variables and all of them will be considered as single visitor! This could make sessions useless and also be potential security risk. To solve this problem, always set RegenerateExpiredSessionId="True" when working with cookieless session. When RegenerateExpiredSessionId="True" and some visitor comes through an URL that contains expired session id, ASP.NET will assign new unique session id.

Since session id is so exposed when cookieless sessions are used, to increase security you can check some other information from visitor, like information from Request.Browser properties. Checking of IP address is also an option, but notice that some Internet service providers change IP address during the single visit. In cases like this, often you still can check first two numbers of IP address because they usually stay the same.

Notice that visitors usually disable cookies for security reasons. In case of cookieless sesions, ironically, visitor become much more insecure than if session with cookie is used.

Menu and TreeView control when cookieless session is used

Menu and TreeView controls are standard controls used for site navigation. Keep in mind that these controls will not automatically add session ids. For example, very common scenario is to bind Menu or TreeView control to SiteMapDataSource control. It looks logical that resulting links on page will have session id embedded. But, Menu or TreeView control will display just simple absolute links without ids.

To resolve this, we need to use Response.ApplyAppPathModifier method again. In this case, we'll use MenuItemDataBound or TreeNodeDataBound events respectively:

[ C# ]

// Cookieless sessions with Menu control
protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
{
// Embeds session id
   e.Item.NavigateUrl = Response.ApplyAppPathModifier(e.Item.NavigateUrl);
}
// Cookieless sessions with TreeView control
protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
// Embeds session id
   e.Node.NavigateUrl = Response.ApplyAppPathModifier(e.Node.NavigateUrl);
}

[ VB.NET ]

' Cookieless sessions with Menu control
Protected Sub Menu1_MenuItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles Menu1.MenuItemDataBound
' Embeds session id
   e.Item.NavigateUrl = Response.ApplyAppPathModifier(e.Item.NavigateUrl)
End Sub
' Cookieless sessions with TreeView control
Protected Sub TreeView1_TreeNodeDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodeDataBound
' Embeds session id
   e.Node.NavigateUrl = Response.ApplyAppPathModifier(e.Node.NavigateUrl)
End Sub

Now, all links in Menu or TreeView controls will have session ids embedded.

Session.IsCookieless property

You can check in run time if session is cookieless sessions or not. This is useful if you are creating an application that could be included as module on others' websites (e.g. forum, user support application etc.). In this case, you can't change settings in main web.config file, but you can check in code if session requires cookies but visitor is disabled them, and if needed redirect and inform visitor so he or she knows why application doesn't work. Example of using Session.IsCookieless property could look like this:

[ C# ]

if (Session.IsCookieless)
{
// Web application uses cookieless session
}
else
{
// Web application uses cookies to work with sessions
// If needed, inform visitor that cookies are required
}

[ VB.NET ]

If (Session.IsCookieless) Then
' Web application uses cookieless session
Else
' Web application uses cookies to work with sessions
' If needed, inform visitor that cookies are required
End If

Conclusion

HTTP is stateless protocol, which means that it doesn't provide a way to save information of same visitor between two requests. ASP.NET offers Session State as a method to resolve this problem. To find out more about other possible methods, check ASP.NET Session State Alternatives tutorial. In general, to save visitor's state when using stateless protocol, you can place id inside of page's HTML or as part of URL. In HTML, there are two options: <head> or <body> tags. Cookies are part of <head> tag, but this is automatically managed by web browser.

ASP.NET ViewState stores information inside of <body> tag, as hidden field. This works nice, but the problem is that this method can only work with POST, and can't work if GET method is used.

Third, and last option is to store information inside of page URL, and that is how cookieless session work. Be aware that some mobile devices can't read format of cookieless URLs with parentheses, and cookieless sessions could cause problems with some URL rewriting modules.

If you have pure HTML pages, there is a problem to share POST data between HTML form and ASP.NET web form. When cookieless ASP.NET form is posted, it contains information about session id too. But, if you make POST on HTML form, it will not contain session id and new session will be created. In that case, data of previous session will be lost. The solution could be to embed session id to link in form's action attribute. You can do that using Response.ApplyAppPathModifier method like in previous examples.

Limitations

The principal limitation of this feature is the limited amount of data that can be stored in the URL. This feature is not targeted at common browsers such as IE, since these do support cookies and do not require this feature. The browsers that do not support cookies are the ones found on mobile devices (such as phones), and these browsers typically severely limit the size of the URL they support. So, be careful when you use this feature—try to make sure that the cookieless string generated by your application is small.

A second limitation on the size comes on the Windows 2003 platform. A (configurable) setting on the Windows Networking layer requires that each segment of the URL be less than 256 chars in length. Since the entire cookieless string goes into 1 segment, it must be less than 256 chars. (A segment of the URL is the portion of the URL between two forward slashes ('/')).

PDF Arabic watermark using MVC and iTextSharp

PDF full page Arabic watermark using MVC and iTextSharp Download :  Source Code Most of the time we have requirement to  gen...