Keep Session Alive Trick in asp.net csharp

29. January 2010

Few days ago some one ask me about his session Lose.he was working on Page filling a form and he went out and came back after 1 hour and click the button to submit the form and he saw the message timeout  when he click on button after an half hour.So i decided to write an article on Keep Session Alive.

Shortest and simple answer.

My Analysis

1) Increasing session time out for the site is bad idea in my anaylsis.

2) Build a custom control or Page that allows keeps  session  Alive for your working page.

aspx Page

Create a aspx Page e.g KeepSessionAlive.aspx

In this Page only write One Line of code in code behind.

 

 

protected void Page_Load(object sender, System.EventArgs e)
{
    Response.AddHeader("Refresh", Convert.ToString(( Session.Timeout * 60 ) -120 ));
}

 

Just add Refesh Header Attribute to the webForm

 

Now goto your Master Page and add this line of code given below

 

<IFRAME id=Defib src="/SessionKeepAlive.aspx"
     frameBorder=no width=0 height=0 runat="server"></IFRAME>

 

This iframe will be invisible in the webform.This iframe will call back  to server side 60 second before session timeout to keep session from expiring.

 

 

 

 


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net ,

Aamir Hasan

26. January 2010

Aamir Hasan on Top answer Last 30 days in Mircosoft asp.net site

in over all asp.net ,database i am in 5 postion and in web servies and  http handler i am on top postion


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net , ,

__doPostBack function

25. January 2010

 

 

Author: Aamir Hasan

hi every one Today i am going to talk about  __doPostBack function.because there is some confusion in using _dopostback

You can see this __doPostBack function in your asp.net generated HTML code.
The function takes two arguments are:

1) eventTarget

2) eventArgument

1) EventTarget contains the ID of the control that causes the postback

eventArgument contains any additional data associated with the control.


When a page is posted back to the server ASP .NET inspects __EVENTTARGET and __EVENTARGUMENT values and this way it can decide which of the controls caused the page to be postedback and what is the event that has to be handled.
In any asp.net page the two hidden fields, “__EVENTTARGET” and “__EVENTARGUMENT,” are automatically declared. The value of the eventTarget and eventArgument are stored in the hidden fields. The two hidden variables can be accessed from the code behind using the forms or params collection.

My Anaylsis(Aamir Hasan)

If we inspect the code of the __doPostBack function we can see that it first sets the values of two hidden fields created by ASP .NET named __EVENTTARGET and __EVENTARGUMENT with the two parameters passed to the function. After this the page is submitted back to the server.When a page is posted back to the server ASP .NET inspects __EVENTTARGET and __EVENTARGUMENT values and this way it can decide which of the controls caused the page to be postedback and what is the event that has to be handled.The ID of the control which causes the postback is stored in the __EVENTTARGET hidden field, so you can find the control which caused the postback.


<a id="LinkButton1" href="javascript:__doPostBack('LButton3','')">LinkButton</a>
You can see the fucntion call "__doPostBack('LButton3','')" in href and the argument passed for eventTarget is "LButton3" which is the id of the linkbutton control (EventSource)

Example

i am going to talk aamirhasan.aspx Page.

in aamirhasan.aspx Page

1) Add two hidden filed inside the form as given below

<input type =hidden name ="__EVENTTARGET" value ="">
<input type =hidden name ="__EVENTARGUMENT" value ="">

2) Add javascript under the Head tag as given below

 

 

3) add two control given below

<a id="LButton3" href="javascript:__doPostBack('Button2','')">LinkButton</a>

<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />

 

4) add function in your cs page

 

protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Write("Welcome to  Student Academic Blog");
    }

 

 

 

So this is how you can use _doPostBack

Enjoy it

Note:ultimately a postback will only fire, if there is a control in the page with a property "autopostback=true".


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, Javascript ,

Calling server side methods to ClientSide using Json, JavaScript and JQuery in ASP.Net csharp

22. January 2010

Autohr: Aamir Hasan

how many time people ask in forum.asp.net and mail to me that how can we get a value from server side to Client side by javscript

so now i have decided to make a tutorial of aspx using csharp

By using jQuery we can send and get value without call back

 

Following rules to be follows are:

1) you have to include jquery library.

2) jquery function

3) create a button and client side function call.

in your aspx page

write this line of code


<div>
Your Name :
<asp:TextBox ID="tbUserName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time"
    onclick = "ShowServerTime()" />
</div>

Client Side in side the head tag

 


Ajax method will call to server side and get the Current dattime from the server to client side.

 

 

Server side Csharp method.

 

[System.Web.Services.WebMethod]
public static string GetServerTime(string Username)
{
return "Welcome To StudentAcad.com \nThe Server Time is: "
+ DateTime.Now.ToString();
}

Above Method will return the Message with Server datetime

 

Important note: if above Web Method  will not be static then you are un able to call the method.


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, JQuery , ,

FindControl MasterPages using ASP.NET 2.0 csharp

21. January 2010

Author: Aamir Hasan

 

Lot of people ask me about how to find a Textbox ,Label from a page included master page , at last i decided to write an article on this topic.This is litle

bit tricky with Master Page.


I have done, this in my C# Main class:

Using the MasterType Directive

Label lbl = this.Master.Page.FindControl("lblHeader") as Label;
if (lbl != null)
{
lbl.Text = "Welcome from the content page!";
}


 

protected Label LabelError = null;

protected DataGrid GridViewDataList = null;

 

protected void iControls()

{

    this.LabelError = this.FindControl("LabelError") as Label;

    this.GridViewDataList = this.FindControl("GridViewDataList") as GridView;

}

 

you now have to drill into the containership with code like this:

 
protected void iControls()

{

    this.LabelError = this.Master.FindControl("Content").FindControl("LabelError") as Label;

    this.GridViewDataList = this.Master.FindControl("Content").FindControl("GridViewDataList") as DataGrid;

}


Creating Master Page Base Classes

In cases where the same public property must be defined in multiple master pages
 (such as a Label control in a website header),
a base master page class can be created that derives from MasterPage
This class can be added in the App_Code folder.

public abstract class BaseMasterPage : MasterPage
{
    public abstract Label studentacadHeaderLabel
    {
        get;
    }
}


creating a custom master page file class with a single public abstract property.
public partial class studentacad_InheritedMasterPage : BaseMasterPage

{

    public override Label studentacadHeaderLabel

    {

        get { return LabelHeader; }

    }

     protected void Page_Load(object sender, EventArgs e)

    {

        if (String.IsNullOrEmpty(LabelHeader.Text))

        {

            this.LabelHeader.Text = DateTime.Now.ToString();

        }

    }

}


How to: Reference ASP.NET Master Page Content

http://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx

Working with ASP.NET Master Pages Programmatically

http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, asp.net 4.0



User Name: Guest

Your Ip: 38.107.191.94
Time: