By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,387 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,387 IT Pros & Developers. It's quick & easy.

how to maintain values in text box controls in C#

P: n/a
Hi
I have developed a web page with some controls. I put all of them in
session variables. I passed this values in the page. When an user
enter some data in form fields and click the buttton , these values
are displaying in next page.after displaying in next page, when click
back button to see my entered values for change , no values are in the
form fields. the fields are empty .I tried with enable view state=true
in controls and at the top of the page.but it doesn't work.

any one help would be appriciated.

Here is the code in two pages.
--------------------------------------------------------------------------------------------------
Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication2._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 bgcolor="bule">
<form id="form1" runat="server">
<div>
<table cellspacing="1">
<tr>
<td colspan="300px">
<table width="700px" border="1">
<tr>
<td>
<tr align="center">
<td align ="center">
<asp:Label ID="lblusername"
runat="server">FirstName :</asp:Label>
<asp:TextBox ID="txtusername"
runat="server" Width="100px"></asp:TextBox>
</td>
</tr>
<tr align="center">
<td align="center" bgcolor="Fuchsia">
<asp:Label ID="lblpassword"
runat="server">LastName :</asp:Label>
<asp:TextBox ID="txtpassword"
runat="server" Width="100px"></asp:TextBox>
</td>
</tr>
<tr align="center">
<td align="center">
<asp:Label ID="lblcity"
runat="server">City :</asp:Label>
<asp:TextBox ID="txtcity" runat="server"
Width="100px"></asp:TextBox>
</td>
</tr>
<tr align="center">
<td align="center">
<asp:Label ID="lblstate" BackColor="Aqua"
runat="server">State :</asp:Label>
<asp:DropDownList runat="server"
ID="ddlstate">
<asp:ListItem Text="AZ"/>
<asp:ListItem Text="AL"/>
<asp:ListItem Text="CA"/>
<asp:ListItem Text="DL"/>
<asp:ListItem Text="FL"/>
<asp:ListItem Text="GA"/>
<asp:ListItem Text="IL"/>
<asp:ListItem Text="KE"/>
<asp:ListItem Text="MN"/>
<asp:ListItem Text="ND"/>
<asp:ListItem Text="OH"/>
<asp:ListItem Text="PA"/>
<asp:ListItem Text="SD"/>
<asp:ListItem Text="TX"/>
<asp:ListItem Text="UT"/>
<asp:ListItem Text="VA"/>
</asp:DropDownList>
</td>
</tr>
<tr align="center">
<td align="center">
<asp:Label ID="lblzip"
runat="server">Zip :</asp:Label>
<asp:TextBox ID="txtzip" runat="server"
Width="100px"></asp:TextBox>
</td>
</tr>
<tr align="center">
<td align="center">
<asp:Label ID="lblphone"
runat="server">Phone :</asp:Label>
<asp:TextBox ID="txtphone" runat="server"
Width="100px" BackColor="#0099FF"></asp:TextBox>
</td>
</tr>
<tr align="center">
<td align="center">
<asp:Label ID="Label1"
runat="server">Codeword :</asp:Label>
<asp:DropDownList ID="ddlselect"
runat="server" ForeColor="Blue"
BackColor="#FFCCFF">
<asp:ListItem Text=""/>
<asp:ListItem Text="What is your favourite
pet?."/>
<asp:ListItem Text="What is your favourite
country?."/>
<asp:ListItem Text="What is your favourite
movie?."/>
<asp:ListItem Text="What is your favourite
movie?."/>
<asp:ListItem Text="What is your maiden
father name?."/>
</asp:DropDownList>
</td>
</tr>
<tr align="center">
<td align="center">
<asp:Label ID="lblcodeword"
runat="server"></asp:Label>
<asp:TextBox ID="txtcodeword"
runat="server" Width="100px" BackColor="BlueViolet"></asp:TextBox>
</td>
</tr>
<tr align="center">
<td align="center">
<asp:Button ID="btnok" runat="server"
Text="Submit" onclick="btnok_Click" />
</td>
</tr>
</td>
</tr>

</table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

------------------------------------------------------------------------------------------------
Default.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;
using System.Data.SqlClient;

namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
Page.EnableViewState = true;
}

protected void btnok_Click(object sender, EventArgs e)
{
Session["first"] = txtusername.Text.Trim();
Session["last"] = txtpassword.Text.Trim();
Session["city"] = txtcity.Text.Trim();
Session["state"] = ddlstate.SelectedValue.ToString();
Session["zip"] = txtzip.Text.Trim();
Session["phone"] = txtphone.Text.Trim();
Session["selectcodeword"] =
ddlselect.SelectedValue.ToString();
Session["entercodeword"] = txtcodeword.Text.Trim();
Response.Redirect("WebForm1.aspx");

}

}

}
----------------------------------------------------------------------------------------------------
WebForm1.aspx.
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;

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

string firstname = Session["first"].ToString();
string lastname = Session["last"].ToString();
string cityname = Session["city"].ToString();
string statename = Session["state"].ToString();
string zipcode = Session["zip"].ToString();
string phonenumber = Session["phone"].ToString();
string selectanycodeword =
Session["selectcodeword"].ToString();
string enteranycodeword =
Session["entercodeword"].ToString();

Response.Write("you entered :" + "</br>");
Response.Write("FirstName :" + firstname + "</br>");
Response.Write("LastName : " + lastname + "</br>");
Response.Write("CodeWord : " + selectanycodeword + "</
br>");
Response.Write(enteranycodeword + "</br>");
Response.Write("CityName : " + cityname + "</br>");
Response.Write("StateName : " + statename + "</br>");
Response.Write("Zipcode : " + zipcode + "</br>");
Response.Write("Phone Number :" + phonenumber);
}

protected void btnupdate_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}

protected void btnok_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm2.aspx");
}
}
}
Aug 17 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.