2

I am new to ASP.net . I am trying to work with basic web forms. I created code to take first name and last name from user. When user clicks on submit, I need the data to be displayed on another page. Can someone make the code for me that should be written on the output web page?

My Code is :

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>My First web page</title>
</head>
<body>
    <form id="form1" runat="server" action="WebFormOutput.aspx" >
    <div style="position:absolute">
    <input type="text" name="txtFirstName" placeholder="Enter Your First Name" />
    <input type="text" name="txtLastName" placeholder="Enter Your Last Name"/>
        <input id="Submit1" type="submit" value="submit" />
    </div>
    </form>
</body>
</html>

Thank You

1
  • if you are working with sp.net web forms why dont you use asp controls textbox and button instead of input tags in html? Commented Feb 24, 2016 at 7:27

1 Answer 1

3

consider the following example...

1.Create a web page named default.aspx(say)like following,

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="HP_Master_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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="First Name"></asp:Label>
        <asp:TextBox ID="txtFN" runat="server"></asp:TextBox>
         <asp:Label ID="Label2" runat="server" Text="Last Name"></asp:Label>
                 <asp:TextBox ID="txtLN" runat="server"></asp:TextBox>

        <asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

and in Default.aspx.cs,like following

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["FN"] = txtFN.Text;
        Session["LN"] = txtLN.Text;
        Response.Redirect("Default2.aspx");
    }
}

what i have done is 1.Created a page,and added server controls from tool bar,Label1,Label2,txtFN,txtLN, and a Button

2.Added onclick event to button,and in code behind created two sessions to carry the values entered in the two text boxes,Session["FN"],and Session["LN"],then the page is redirected to another page "Default2.aspx" on button click

3.Default two cantains two server controls LblFN,andlblLN that contain no values initially

4.When the page is loaded as soon as the button in previous page is clicked,in the current pages page load event,the text property of labels lblFN,and lblLN are set with the values stored in the Session variables.the code for this is as per following

default2.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="HP_Master_Default2" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblFN" runat="server" Text=""></asp:Label>
        <asp:Label ID="lblLN" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

default2.aspx.cs:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class HP_Master_Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblFN.Text = Session["FN"].ToString();
        lblLN.Text = Session["LN"].ToString();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You for your help. This helped me a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.