Passing values between pages in ASP.Net
###########################################
Here are some useful way to pass values between pages. I think it will useful for the beginners who entered in Web development
1.Response.Redirect
This maybe the easiest of them all. You start by writing some data in the text field, and when you finish writing the data, you press the button labeled 'Response.Redirect'. One tip that I would like to share with you is, sometimes we want to transfer to another page inside the catch exception, meaning exception is caught and we want to transfer to another page. If you try to do this, it may give you a System.Threading exception. This exception is raised because you are transferring to another page leaving behind the thread running. You can solve this problem using:
Response.Redirect("WebForm2.aspx",false);
This tells the compiler to go to page "WebForm2.aspx", and "false" here means that don't end what you were doing on the current page. You should also look at the System.Threading class for threading issues. Below, you can see the C# code of the button event. "txtName" is the name of the text field whose value is being transferred to a page called "WebForm2.aspx". "Name" which is just after "?" sign is just a temporary response variable which will hold the value of the text box.
private void Button1_Click(object sender, System.EventArgs e)
{
// Value sent using HttpResponse
Response.Redirect("WebForm2.aspx?valName="+txtName.Text);
}
Okay, up till this point, you have send the values using Response. But now, where do I collect the values, so in the "WebForm2.aspx" page_Load event, write this code. First, we check that the value entered is not null. If it's not, then we simply display the value on the page using a Label control. Note: When you use Response.Redirect method to pass the values, all the values are visible in the URL of the browser. You should never pass credit card numbers and confidential information via Response.Redirect.
if (Request.QueryString["valName"]!= null)
lblName.Text = Request.QueryString["Name"];
-------------------------------------------------------------------------------------------
2.Cookies
Next up is cookies. Cookies are created on the server side but saved on the client side. In the button click event of 'Cookies', write this code:
HttpCookie cName = new HttpCookie("valName");
cName.Value = txtName.Text;
Response.Cookies.Add(cName);
Response.Redirect("WebForm2.aspx");
First, we create a cookie named "cName". Since one cookie instance can hold many values, we tell the compiler that this cookie will hold "Name" value. We assign to it the value of the TextBox and finally add it in the Response stream, and sent it to the other page using Response.Redirect method.
Let's see here how we can get the value of the cookie which is sent by one page.
if (Request.Cookies["valName"] != null )
lblName.Text = Request.Cookies["valName"].Value;
As you see, it's exactly the same way as we did before, but now we are using Request.Cookies instead of Request.QueryString. Remember that some browsers don't accept cookies.
--------------------------------------------------------------------------------------
3.Session Variables
Next we see the session variables which are handled by the server. Sessions are created as soon as the first response is being sent from the client to the server, and session ends when the user closes his browser window or some abnormal operation takes place. Here is how you can use session variables for transferring values. Below you can see a Session is created for the user and "Name" is the key, also known as the Session key, which is assigned the TextBox value.
// Session Created
Session["Name"] = txtName.Text;
Response.Redirect("WebForm2.aspx");
// The code below shows how to get the session value.
// This code must be placed in other page.
if(Session["Name"] != null)
Label3.Text = Session["Name"].ToString();
It is best practice to declare all your session variables in the global.asax file, in the session_start() event, so it will available for your whole application.
--------------------------------------------------------------------------------
4.Application Variables
Sometimes, we need to access a value from anywhere in our page. For that, you can use Application variables. Here is a small code that shows how to do that. Once you created and assigned the Application variable, you can retrieve its value anywhere in your application.
// This sets the value of the Application Variable
Application["valName"] = txtName.Text;
Response.Redirect("WebForm2.aspx");
// This is how we retrieve the value of the Application Variable
if( Application["valName"] != null )
lblName.Text = Application["valName"].ToString();
-----------------------------------------------------------------
5.HttpContext
You can also use HttpContext to retrieve values from pages. The values are retrieved using properties or methods. It's a good idea to use properties since they are easier to code and modify. In your first page, make a property that returns the value of the TextBox.
public string GetName
{
get { return txtName.Text; }
}
We will use Server.Transfer to send the control to a new page. Note that Server.Transfer only transfers the control to the new page and does not redirect the browser to it, which means you will see the address of the old page in your URL. Simply add the following line of code in 'Server.Transfer' button click event:
Server.Transfer("WebForm2.aspx");
Now, let's go to the page where the values are being transferred, which in this case is "webForm2.aspx".
// You can declare this Globally or in any event you like
WebForm1 w;
// Gets the Page.Context which is Associated with this page
w = (WebForm1)Context.Handler;
// Assign the Label control with the property "GetName" which returns string
lblName.Text = w.GetName;
============================================================
Hope it will helpful for you
any comments appreciated
Wednesday, May 14, 2008
Secure PDF Files and opening reports in a new window in ASP.Net 2.0
The main concern we faced while handling the PDF file is its security.We export the crystal report into PDF file and rendered in the browser. After viewing the PDF file anyone can access the PDF if they came to know the path. After analyzed a lot of methods we concluded with the below solution. It is easy to implement and feel happy to share this with you all the experts.
Believe it will helpful for the secure PDF file handling , rendering the Crystal report as PDF file and for opening the report in separate window.
During the analysis I came to know various ways to secure the PDF files such as Form authentication,storing the PDF files in other secure formats(like .recourse) and while calling the PDF file just save as the file into PDF and rendered using Response.WriteFile method, using session Id with the PDF file name and a few more.
For the stored PDF files or the Crystal reports need to generate and to be stored in a external device for the future purpose
We can use the below methods
1.Create a separate page 'SecurePdf.aspx'. we can use this page for entire application
2.Place a Iframe control
iframe id="fraPdfRender" runat="server" height="800" scrolling="auto" width="100%"
3.If crystal report need to rendered as a PDF file, first convert that into PDF file using the export method of crystal document and use the below code on the required page which the report or the PDF file need to rendered . It will open the 'SecurePdf.aspx' in a separate window (the popup blocker should allow this site, or if it is not required to open in a new window use the normal response.redirect method for opening the page)
string url = reportfilename;
Session["securePdf"] = url;
string script = "window.open('/reports/securePdf/SecurePdf.aspx','new_Win');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", script, true);
Session["securePdf"] = url;
string script = "window.open('/reports/securePdf/SecurePdf.aspx','new_Win');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", script, true);
4.place this code in the code behind file
string url = Session["securePdf_n"].ToString();
HtmlControl frame1 = (HtmlControl)this.FindControl("fraPdfRender");
frame1.Attributes["src"] = url;
string url = Session["securePdf_n"].ToString();
HtmlControl frame1 = (HtmlControl)this.FindControl("fraPdfRender");
frame1.Attributes["src"] = url;
I believe this code is easy to impalement and the url of the PDF file is not shown to the user.I implemented into my application but didn't get the feedback from my client.
If Crystal reports needs to generate for on demand as PDF file:
after analyzed some I used the below method
ReportDocument crReportDocument;
crReportDocument = new cReportName();
crReportDocument = new cReportName();
crReportDocument.ExportToHttpResponse(ExportFormatType.PortableDocFormat,HttpResponse,false);
this will convert the crystal report into PDF automatically and render into the browser without storing in a external disk.
Believe it will helpful for the secure PDF file handling , rendering the Crystal report as PDF file and for opening the report in separate window.
As comments and opinion always expected from you
Subscribe to:
Posts (Atom)