Monday, 2 June 2014

Passing Value From One Form to Another in Asp.net Using C#.Net

6/02/2014 - By Pranav Singh 0

Whenever we develop any application on that case we must face some case where we needed to pass the value from one page to another page in our asp.net application and capture the pass value in our application.


Now for this article first you needed to create new asp.net application. After creating .aspx pages in form one add a textbox and a button control this textbox control value we will pass into next page when user click on button.

So this article I will show in two ways for passing the data from one page to another page. Part 1 will contain pass value by requestquerystring and part 2 will contain pass value by session.

Defaut.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ProjectDemo_Asp.et.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Passing Value From One Form to Another in Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <h3>
        Passing Value in Asp.Net Page</h3>
    <asp:TextBox ID="TextBox1" runat="server" Height="21px" Width="201px"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Click" Width="68px"
        onclick="Button1_Click" style="height: 26px" />
    </form>
</body>
</html>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Passing Value From One Form to Another in Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       Recived Value: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>   
    </div>
    </form>
</body>
</html>

Part1: By Request.QueryString

.cs code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectDemo_Asp.et
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

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

In above code we have passed the value as a parameter to the page 2.

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

namespace ProjectDemo_Asp.et
{
    public partial class Page2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["value"] != null)
            {
                Label1.Text = Request.QueryString["value"].ToString();
            }
        }
    }
}

In above code we have check whether the pass value is null or not if not the assign the value. If we not apply this check and user does not pass the value the we will get error. Now run the application for output.



Part 2: By using session

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

namespace ProjectDemo_Asp.et
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["value"] = TextBox1.Text;
            Response.Redirect("Page2.aspx");
        }
    }
}

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

namespace ProjectDemo_Asp.et
{
    public partial class Page2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["value"] != null)
            {
                Label1.Text = Session["value"].ToString();
            }
        }
    }
}

Now run the application by applying break point.

 


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