Friday, 27 May 2016

Passing Data from one page to another page in Asp.net Using C#.Net

5/27/2016 - By Pranav Singh 0

This article will show you how you can pass value from one page to another page in asp.net using c#.net. In this article I have shown three ways to pass and get the on other page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="WebApplication7.Page1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page 1</title>
</head>
<body>
    <form id="form1" runat="server">
        <h3>Click Button To Pass Value</h3>
        Enter Value :<asp:TextBox ID="txtValue" runat="server" Width="201px"></asp:TextBox>
        <br />
        <br />
        <div>
            <asp:Button ID="Button1" runat="server" Text="By Request Querystring" OnClick="Button1_Click" Width="203px" />
            <br />
            <br />
            <asp:Button ID="Button2" runat="server" Text="By Session" Width="204px" OnClick="Button2_Click" />
            <br />
            <br />
            <asp:Button ID="Button3" runat="server" Text="By HiddenField" Width="201px" PostBackUrl = "Page2.aspx"/><br />
        </div>
    </form>
</body>
</html>

In above code I have taken three button controls. Each button represents a way to transfer the data. In above I have passed the textbox value from one page to other page. As you execute the page1 your page will look as shown below. 


Now have a look of the below code.

Way 1:
  protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Page2.aspx?value=" + txtValue.Text);
        }

In above code I have used request querystring to pass the value. 

Way2:
protected void Button2_Click(object sender, EventArgs e)
        {
            Session["Value"] = txtValue.Text;
            Response.Redirect("Page2.aspx");
        }

In above I have used session to pass the value to other page.

Way3:
<asp:Button ID="Button3" runat="server" Text="By HiddenField" Width="201px" PostBackUrl = "Page2.aspx"/>

In above I have used PostBackUrl to pass the value and access it on other page. Now we will check the code for page2.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page2.aspx.cs" Inherits="WebApplication7.Page2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page 2</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            Request Querystring :<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
            Session Value :<asp:Label ID="Label2" runat="server" Text=""></asp:Label><br />
            Http Post:<asp:Label ID="Label3" runat="server" Text=""></asp:Label><br />
        </div>
    </form>
</body>
</html>

Now have a look the below code of page2 access the pass value.

Way1 To access:
  if (Request.QueryString["value"] != null)
            {
                Label1.Text = Request.QueryString["value"].ToString();
            }

Now press F5 to check the output.

 Way2 To Access:
  if (Session["value"] != null)
            {
                Label2.Text = Session["value"].ToString();
            }

Now press F5 to access the value.

 Way3 To access:
if (Request.Form["txtValue"] != null)
            {
                Label3.Text = Request.Form["txtValue"].ToString();
            }

Now press F5 to check the output.


Tags: , ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top