Search This Blog

Wednesday, January 25, 2012

How to go back to the previous page in ASP.NET

There are various ways using which you can navigate back to the previous page. To keep the example short and simple, I will be using two pages and a few buttons to demonstrate the navigation. So let us get started:
Step 1: Create an ASP.NET Project with two pages, Page1.aspx and Page2.aspx.

Step 2: On Page1.aspx, drag and drop a button control. In the click event, use this code:


C#

protected void Button1_Click(object sender, EventArgs e)

{

Response.Redirect("Page2.aspx");


}

VB.NET

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


Response.Redirect("Page2.aspx")

End Sub

Step 3: On Page2.aspx, we will be dragging and dropping 3 buttons. Each button will represent a method to go back to the previous page. Let us explore them one at a time:

Method 1 – Using a static variable and UrlReferrer (only for understanding, not to be used in production apps)

URLReferrer gets the URL of the previous page that linked to the current URL. To use this property, declare a static variable called ‘prevPage’ in Page2.aspx. Drag and drop a button, button1 on Page2.aspx. On the Page_Load, use the Request.UrlReferrer to populate the prevPage variable with its value. Then on the button1 click event, use this variable to go back to the previous page as demonstrated below :


C#

// static variable

static string prevPage = String.Empty;


protected void Page_Load(object sender, EventArgs e)


{

if( !IsPostBack )

{

prevPage = Request.UrlReferrer.ToString();

}


}



protected void Button1_Click(object sender, EventArgs e)

{

Response.Redirect(prevPage);

}


VB.NET

'static variable

Private Shared prevPage As String = String.Empty



Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load


If (Not IsPostBack) Then

prevPage = Request.UrlReferrer.ToString()

End If

End Sub



Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


Response.Redirect(prevPage)

End Sub


Method 2 – Using Javascript

Drag and drop another button called button2 on the Page2.aspx. In the Page_Load event, add the following lines of code :

C#

protected void Page_Load(object sender, EventArgs e)


{

Button2.Attributes.Add("onClick", "javascript:history.back(); return false;");

}


protected void Button2_Click(object sender, EventArgs e)


{


}


VB.NET


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load


Button2.Attributes.Add("onClick", "javascript:history.back(); return false;")

End Sub


Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click



End Sub


Note : Notice the ‘return false’ snippet used in the Button2.Attributes.Add method. Well this is used to cancel the submit behaviour that occurs on the button click. Since the Click event precedes over the other events, we need to return false to cancel the submit and go back to the previous page.



Method 3 – Using ViewState



If you do not intend to declare a static variable (which you should avoid in all cases), you can use viewstate to go back to the previous page by using the same UrlReferrer property that we used in Method 1. To do so, drag and drop a third button, Button3 on Page2.aspx. In the Page_Load event, use the ViewState to store the value of the Request.UrlReferrer property. Then access the same value in the click event of the third button to go back to the previous page as shown below:

C#

protected void Page_Load(object sender, EventArgs e)


{

if( !IsPostBack )

{

ViewState["RefUrl"] = Request.UrlReferrer.ToString();

}


}



protected void Button3_Click(object sender, EventArgs e)

{

object refUrl = ViewState["RefUrl"];


if (refUrl != null)

Response.Redirect((string)refUrl);

}


VB.NET




Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load


If (Not IsPostBack) Then

ViewState("RefUrl") = Request.UrlReferrer.ToString()

End If


End Sub



Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click


Dim refUrl As Object = ViewState("RefUrl")

If Not refUrl Is Nothing Then


Response.Redirect(CStr(refUrl))

End If

End Sub




Well as we saw, it was quiet easy to use these different methods to go back to the previous page. You are free to experiment and use any of these methods (avoid static variables though). If you have experimented with any method, you can share it over here with other readers.



I hope this article was useful and I thank you for viewing it. Happy Coding!!

Friday, January 13, 2012

Disable Multiple Button Click in Asp.net

Disable Multiple Button Click in Asp.net
Preventing Multiple Button Click on Asp.net Page

Problem: How to disable button immediately on user click in asp.net so that user cannot click multiple time.

Solution: To solve this problem use regular html button, including runat=”server” attribute and write the disable client-side click event which disable button immediately and server-side event which runs the actual code for click event.


To perform this task add javascript function.

Client-Side Event to disable button immediately.
<head runat="server">
<script language = "javascript">
var c=0;
function DisableClick()
{
var objName = 'Button1';
document.getElementById(objName).disabled=true;
c=c+1;
msg = 'Please Wait...('+ c +')!';
document.getElementById(objName).value= msg;
var t=setTimeout('DisableClick()',1000);
}
</script>
</head>

Note: Replace “Button1” with object name of button control

And following button code in body.


Button Control – HTML Button instead of Asp button.
<INPUT id="Button1"
onclick="DisableClick();" type="button"
value="Submit Payment" name="Button1"
runat="server" onserverclick="Button1_Click">

Note: onclick event is calling javascript client-side function and onserverclick event is calling server-side function which performs actual task.


Server-Side Event to perform actual task
This task can be any as per your logic, for an example I am performing some heavy time consuming task, you can even make use of Threading concept to minimize code…

protected void Button1_Click(object sender, EventArgs e)
{
ArrayList a = new ArrayList();
for (int i = 0; i < 10000; i++)
{
for (int j = 0; j < 1000; j++)
{
a.Add(i.ToString() + j.ToString());
}
}
Response.Write("I am done: " +
DateTime.Now.ToLongTimeString());
}



Before Button Click








During Button Click







After Task is done



Understanding DateTime and TimeSpan in .Net through real time example

Understanding DateTime and TimeSpan in .Net through real time example




Understanding DateTime and TimeSpan in .Net through real time example



//Creating Two Instance of DateTime
DateTime dt1 = new DateTime();
DateTime dt2 = new DateTime();


//Initializing DateTime Object
dt1 = DateTime.Now.Date; //Initializing with Current Date
Response.Write(dt1.ToString() + "<br>");
//Output: 22-06-2007 00:00:00

//Examples for DateTime Manipulation in .Net
//Example 1 : Adding Minutes to Current Date.
dt1 = dt1.AddMinutes(5.0);
Response.Write(dt1.ToString() + "<br>");
//Output: 22-06-2007 00:05:00

//Example 2 : Adding Seconds to DateTime Object., similarly you can add milliseconds
dt1 = dt1.AddSeconds(30.0);
Response.Write(dt1.ToString() + "<br>");
//Output: 22-06-2007 00:05:30

//Example 3 : Adding Hours to DateTime Object in .Net.
dt1 = dt1.AddHours(5.0);
Response.Write(dt1.ToString() + "<br>");
//Output: 22-06-2007 05:05:30

//Example 4 : Adding Time to DateTime Object.
//You can replace Example 1,2 and 3 by simply adding TimeSpan with your desired
//TimeSpan value.
//Here we are adding 5 Hours, 5 Minutes and 30 Seconds.
dt1 = dt1.Add(new TimeSpan(5,5,30));
Response.Write(dt1.ToString() + "<br>");
//Output: 22-06-2007 10:11:00

//Example 5 : Adding Days to DateTime Object in .Net
dt2 = dt1.AddDays(7.0); //Initializing dt2 object with 7 days ahead from
//dt1 DateTime value.
Response.Write(dt2.ToString() + "<br>");
//Output: 29-06-2007 10:11:00

//Example 6 : Adding Month to DateTime Object
dt2 = dt2.AddMonths(1);
Response.Write(dt2.ToString() + "<br>");
//Output: 29-07-2007 10:11:00

//Example 7 : Adding Year to DateTime Object
dt2 = dt2.AddYears(1);
Response.Write(dt2.ToString() + "<br>");
//Output: 29-07-2008 10:11:00


//Examples of Retrieving DateTime object value.
//Example 1 : Get Day value.
int intDay = dt1.Day;
Response.Write(intDay.ToString() + "<br>");
//Output: 22

//Example 2 : Get Day of Week.
DayOfWeek dow = dt1.DayOfWeek;
Response.Write(dow.ToString() + "<br>");
//Output: Friday
//How to find whether day of week is sunday or saturday?
if(dow == DayOfWeek.Sunday dow == DayOfWeek.Saturday)
{
//Hurray its weekend.
}

//Example 3 : Get the Day of Year
int intYear = dt1.DayOfYear; //Similarly you can get Year value.
Response.Write(intYear.ToString() + "<br>");
//Output: 173


//Similarly you can get Hours, Minutes, Seconds, Milliseconds value.



//Conversion of String to DateTime
//Make use of Parse Method of DateTime to "Convert String to DateTime in .Net"
DateTime dt4 = DateTime.Parse("08/06/2007");
DateTime dt5 = DateTime.Parse("10/06/2007");

//Comparing Date
//How to Find whether both dates are equal or Not?
//How to compare two dates in .Net?
if (DateTime.Compare(dt4, dt5) == 0)
{
Response.Write("Both Dates are Equal" + "<br>");
}
else if (DateTime.Compare(dt4, dt5) < 0)
{
Response.Write(dt4.ToString() + " is Less than "
+ dt5.ToString() + "<br>");
}
else
{
Response.Write(dt4.ToString() + " is Greater than "
+ dt5.ToString() + "<br>");
}
//Output: 08-06-2007 00:00:00 is Less than 10-06-2007 00:00:00
/*Note: You may recieve error: "String was not recognized as a valid
DateTime." This may be occur as in India the DateTime format is followed
as "dd/mm/yyyy" But let say if you are in USA than it follow "mm/dd/yyyy" so here
there is crash situation and so you might recieve above error, so to avoid such
error make sure that user input valid date before you Parse date. */


//Difference between Date
//How to Find difference between two dates in .Net?
//How to Find days difference between two dates in .Net?
//How to subtract one date from another?
TimeSpan tsDiff = dt5.Subtract(dt4); //Note: Always subtract smaller date
//value from greater date
Response.Write("Difference between " + dt4.ToString() + " and " +
dt5.ToString() + " is " + tsDiff.Days.ToString() + " days." + "<br>");
//Output: Difference between 08-06-2007 00:00:00 and 10-06-2007 00:00:00 is 2 days.


//Similarly you can also find:
//How many hour difference between two dates
//How many seconds difference between two dates
//And so on... by changing tsDiff property value to hours, seconds....
//instead of tsDiff.Days.


//Compare Time
//How to Find whether both Time are equal or Not?
//How to compare Time in .Net?
dt4 = DateTime.Parse("4:30 AM");
dt5 = DateTime.Parse("7:30 PM");
if (DateTime.Compare(dt4, dt5) == 0)
{
Response.Write("Both Times are Equal" + "<br>");
}
else if (DateTime.Compare(dt4, dt5) < 0)
{
Response.Write(dt4.ToShortTimeString() + " is Less than "
+ dt5.ToShortTimeString() + "<br>");
}
else
{
Response.Write(dt4.ToShortTimeString() + " is Greater than "
+ dt5.ToShortTimeString() + "<br>");
}
//Output: 04:30:00 is Less than 19:30:00


//Difference between Time
//How to Find difference between two Time value in .Net?
//How to Find Hours difference between two Time in .Net?
//How to subtract one Time from another?
//How to find elapsed Time between Two Time value in .Net?
tsDiff = dt5.Subtract(dt4); //Note: Always subtract smaller date
//value from greater date
Response.Write("Difference between " + dt4.ToString() + " and " + dt5.ToString() + " is " + tsDiff.Hours.ToString() + " Hours." + "<br>");
//Output: Difference between 22-06-2007 04:30:00 and 22-06-2007 19:30:00 is 15 Hours.


//More on DateTime
//How many days in a given month in .Net?
int intDaysInMonth = DateTime.DaysInMonth(2007, 6); //Pass valid year, and month
Response.Write(intDaysInMonth.ToString() + "<br>");
//Output: 30

//How to find whether given year is Leap year or not in .Net?
Response.Write( DateTime.IsLeapYear(2007)); //Pass valid year
//Output: False

//How to find current date and time?
Response.Write("Current Date and Time: " + DateTime.Now + "<br>");
//Output: Current Date and Time: 22-06-2007 11:31:04

//How to find current date?
Response.Write("Current Date: " + DateTime.Today + "<br>");
//Output: Current Date: 22-06-2007 00:00:00






Thursday, January 12, 2012

Intergration Reporting Service with SharePoint 2010

Intergration Reporting Service with SharePoint 2010


Hello, nice day to all. Today I happy to share with you the way to integrate Reporting Service with SharePoint Foundation 2010. First at all we look how Reporting Service on SharePoint 2010



To integrate Reporting Service with SharePoint Foundation 2010 we need three works: Setup SQL Server 2008 R2 with "Reporting Service Integrated SharePoint Mode", configure SharePoint (Farm) with Reporting Service, and making report so can be run on SharePoint 2010.



A. Setup SQL Server 2008 R2 with "Reporting Service Integrated SharePoint Mode"


A.1. Click on "New installation or add features to an existing installation."

      

A.2.  Enter the product key





A.3.  Choose SQL Server Features Installation






 A.4. Select all features





A.5.  Instance Configuration



A.6.  Server Configuration : click "Use the same account for all SQL Server services"




A.7.  Server Configuration: user administrator account or any that we want



A.8. Database Engine Configuration: click "Add Current User"



A.9.  Database Engine Configuration: Click to "Add..." if we want to add more accounts




 A.10. Analysis Service Configuration: click "Add Current User"





A.11. Reporting Service Configuration: choose "Install the SharePoint integrated mode default configuration."





A.12. After finish the installation we can see the Report Server Web Service URL like





A.13. If click on the Report Server Web Service URL then we logon by administrator account






A.14. We see "Reporting Service Error" because we not yet setup SharePoint Foundation 2010 and configure with the Reporting Service





B. Configure SharePoint (Farm) with Reporting Service

 B.1. Install software prerequisites





B.2. Install SharePoint Foundation






B.3. License Terms and click "Continue"



 B.4. Click on "Server Farm"





B.5. Choose "Complete - Install all components. Can add servers to form a SharePoint farm."

 B.6. Install progress



B.7. Start to configure SharePoint Products

B.8. Click "Yes" to continue




B.9. Choose "Create a new server farm"

B.10. Specify Configuration Database Settings

B.11. Specify Farm Security Settings

B.12. Configure SharePoint Central Administration Web Application

B.13.Completing the SharePoint Products Configuration Wizard

B.14. Configuring SharePoint Products 3 of 10 steps

B.15. Configuring SharePoint Products 8 of 10 steps

B.16. Configuration Successful


B.17. The first logon to central administration site

B.18. Click on "Reporting Services Integration"

B.19.Get Web Service URL of Reporting Service from Reporting Services Configuration Manager





B.20. View Web Services URL of Reporting Service on browser

B.21.Back to Reporting Services Integration page to fill in "Report Server Web Services URL", "Authentication Mode" and "Credentials"



B.22. Reporting Services Integration Summary

B.23. Go to any web application site then click on "Page"




B.24. Click on "Edit"





B.25. Click on "Insert"





B.26. Click on "Web Part"





B.27. Click on "SQL Server Reporting"



B.28. Click on "Add" to add the SQl Server Reporting web part to the page






B.29. Now we can see "Report Viewer" inside SharePoint page





B.30. Click on "Page" then Click on "Save & Close" to keep the changing to page









C. Making a report so can be run on SharePoint 2010

C.1. Download AdventureWorks2008 SR4.exe  sample database from http://msftdbprodsamples.codeplex.com/releases/view/37109   then setup it






  C.2. Open SQL Server Business Intelligence Development Studio from Start -> "All Programs" -> "Microsoft SQL Server 2008 R2"



C.3. File -> New -> Project...




C.4. Create new Report Server project





C.5. Right click on "Share Data Sources" then click "Add New Data Source"



C.6. Click "Edit" to edit data source







C.7. Select "Server name:" and "database name:" can be choose any server name or database name that you want or can be access to

C.8. Shared Data Source Properties




C.9. Add New Report

C.10. Click Next to continue







C.11. Click Next after select Data Source



C.12. Click "Query Builder..."







C.13. Add a Select comment and click "Run"


C.14. Select result then click "OK"

C.15. Query string after make a builder then click "Next"

C.16. Chose Tabular or Matrix that you like

C.17. Design report

C.18. Choose a Style for report

C.19. Choose report name

C.20. CustomerInfor report look like on Business Intelligent Visual Studio

C.21. View code of report CustomerInfor





 C.22. Old Data Source reference code
C.23. Change DataSources reference values
C.24. Back to preview report after change DataSources by code


C.25. Back to a web application the click on "Shared Documents"


C.26. Click "+Add document"
C.27. Click on "Browse..."
C.28. Select CustomerInfor report
C.29.Click "OK" to add CustomerInfor report file
C.30. Click on "Home" to back to home page
C.31. Edit Web Part
C.32. Click on button "..." to add report file
C.33. Click on "Up"
C. 34. Click on "Shared Documents"
C.35. Click on "CustomerInfor" and click on "OK"
 C.36. Click on "Apply"



C.37. Click on "OK"


C.38. The report CustomerInfor display well on page
C. 39. If you want to display more nice so go to tool part again and change the width of Report Viewer


C.40. The Report Viewer is more nice now
God bless us!