.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
Easy Web
Imran Ghani

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

reStore values of datacontrols

Posted By: Cheta     Posted Date: March 06, 2013    Points:5   Category :ASP.Net
Hi all,

How to store values of datacontrols?,my requirment like is in a page i am using lots of data controls like list box,dropdown list,texbox etc.when user changing all the fileds and click on Reset button,previous values should come on page.is it possible?

Please help and thanks in advance

regards
Cheta

Responses
Author: SailajaReddy             
Posted Date: March 06, 2013     Points: 20   

Hi cheta,

viewstate can be used to store values during postbacks.
Here is the sample code with submit and reset buttons.
on click of reset button , data that is previously stored will be displayed.

Aspx Code:

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

<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter Some Data:<asp:TextBox ID="txtData" runat="server"></asp:TextBox>
<br />
Pick a Color: <asp:DropDownList ID="ddlColors" runat="server" AppendDataBoundItems="true">
<asp:ListItem Value="0" Text="Select Color"></asp:ListItem>
<asp:ListItem Value="1" Text="Red"></asp:ListItem>
<asp:ListItem Value="2" Text="Blue"></asp:ListItem>
<asp:ListItem Value="3" Text="Green"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="btnReset" runat="server" OnClick="btnReset_Click" Text="Reset" />
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
</div>
</form>
</body>
</html>


Code Behind:

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

public partial class Forum_Topics_ResettingPreviousValues : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["DataEntered"] = txtData.Text;
ViewState["ColorSelected"] = ddlColors.SelectedValue;
}
}
protected void btnReset_Click(object sender, EventArgs e)
{
/*USING VIEWSTATE TO STORE PREVIOUS INFO*/
if (ViewState["DataEntered"].ToString() == null)
txtData.Text = "";
else
txtData.Text = ViewState["DataEntered"].ToString();

if (ViewState["ColorSelected"].ToString() == null)
ddlColors.SelectedValue = "0";
else
ddlColors.SelectedValue = ViewState["ColorSelected"].ToString();

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
ViewState["DataEntered"] = txtData.Text;
ViewState["ColorSelected"] = ddlColors.SelectedValue;
}
}




Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend