Preserve value of textbox using View State in Asp.net? - c#

I've a button and a textbox. I want a value to be entered in textbox and when I click on button the page will reload but the value should still be in the textbox. How can I do that. The following code doesn't work
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (ViewState["value"] != null)
{
TextBox1.Text = ViewState["value"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["value"] = TextBox1.Text;
Response.Redirect("default.aspx");
}
}
}

Response.Redirect does what it says - redirects the request to a NEW page. ViewState won't get applied, ever. If you need a redirection, consider using session instead.
If you don't need a redirection, simply don't redirect and update only parts of the page that need to be updated.

Viewstate can retain the value only till when you are on the same page. You are redirecting to other page. So instead of using viewstate use session.

asp.net webform have already maintained the viewstate on page refresh . don't need any code for handle this operation .
See this : http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_aspnetviewstate
referred from : http://www.w3schools.com/aspnet/aspnet_viewstate.asp
and see this discussion

try this
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (ViewState["value"] != null)
{
TextBox1.Text = Session["value"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["value"] = TextBox1.Text;
Response.Redirect("default.aspx");
}
}
}

Since you are redirecting to a new VIEW so VIEWSTATE will not be of any HELP. SO,Use Session
namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["value"] != null)
{
TextBox1.Text = Session["value"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["value"] = TextBox1.Text;
Response.Redirect("default.aspx");
}
}
}

Related

How to set ASP.NET session variable?

I am new to ASP.NET and I am trying to set session variable. I have one form (SelectPlayer.aspx) where I am trying to set the session but when I try to see the result on second page it does not show me any value. Below is my code.
SelectPlayer.aspx
public partial class SelectPlayer : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["player1"] == null)
{
lblSelectPlayer.Text = "Select Player 1";
}
}
protected void btnSelect_Click(object sender, EventArgs e)
{
Session["player1"] = "PlayerSession";
Response.Redirect("Score.aspx");
}
}
Score.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (Session["player1"] == null)
{
Response.Redirect("SelectPlayer.aspx");
}
}
Change Response.Redirect("Score.aspx"); to Response.Redirect("Score.aspx", true);
The second parameter of a redirect indicates that you want to end the response. I've found in the past that setting a Session directly before doing a redirect without this second parameter can cause issues.

How to pass all items of Listbox to Textbox which is in another page? ASP.NET

I have an asp.net project with two web forms. I would like to pass all items of listbox3(which is home page) to textbox that is in statistics page.I try following code but didnt work.
Home page:
protected void Button5_Click(object sender, EventArgs e)
{
Response.Redirect("Statistics.aspx?ListBox3");
}
Statistics Page
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Text = ListBox3.Items;
}
}
You can't refer any controls which are not in the current page with its ID.
So it may required to keep the items from ListBoxin Session or pass it through QueryString
Home Page
protected void Button5_Click(object sender, EventArgs e)
{
string Items = string.Empty;
foreach(var item in ListBox3.Items)
{
Items += item + ",";
}
//Session["ListBox3"] = Items;
Response.Redirect("Statistics.aspx?ListBox3=" + Items);
}
Statistics Page
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Text = Request.QueryString["ListBox3"].ToString();
//TextBox3.Text = Session["ListBox3"].ToString();
}
protected void Button5_Click(object sender, EventArgs e)
{
string allItems;
for(int i = 0; i < ListBox3.Items.Count; i++)
allItems += ListBox3.Items[i].ToString() + "--";
Response.Redirect("Statistics.aspx?ListBox3=" + allItems.SubString(0, allItems.Length - 2));
}
Access it using
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Text = Request.QueryString["ListBox3"];
}
}

How can I call a Method of a Content from MasterPage in ASP.NET

I want to call a method of a Content from a master page sending a Parameter to manipulate one label.
public partial class MasterCategoria : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSalada_Click(object sender, ImageClickEventArgs e)
{
produtosCategoria x = new produtosCategoria();
x.changeLabel("Salada");
}
}
Manipulating this button on this WebForm which is a Content
public partial class produtosCategoria : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void changeLabel(string name)
{
lblTexto.Text = name;
}
But this isn't working. How can I do this work?
Thank you guys, and sorry about my english.
The object of type produtosCategoria is already created and it can be accessed from your master page via this.Page.
So to change the label of your content page you can do as in the snippet below.
Also, I added a simple type check so you won't get an error if another content page is loaded
protected void btnSalada_Click(object sender, ImageClickEventArgs e)
{
// Check if it is the correct content page
if (this.Page.GetType() == typeof(produtosCategoria))
{
produtosCategoria x = (produtosCategoria)this.Page;
x.changeLabel("Salada");
}
}
Note: When code executes in the master page this is the master page and this.Page is the content page

Get Public Content Page Variable into Masterpage

I have a public variable in my content page that I need to access in my MasterPage. So I can set a Javascript variable... .
How can I reference a public content page variable from the masterpage?.
I supposed you want to say that you want to access to variable in the MasterPage from Contend Page, if is correct, use this example:
Declare your public or protected variable:
public partial class MasterPage : System.Web.UI.MasterPage
{
public string strEmpresa = "NS";
protected void Page_Load(object sender, EventArgs e)
{
}
}
Set the following directive at the beginning of your content page:
<%# MasterType virtualPath="~/MasterPage.Master"%>
then you can use the public variables of your MasterPage, using Master.NameVariable.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = Master.strEmpresa;
}
}
In other case if you really want access to variable in ContentPage from MasterPage, you just can set the value in Session and then read in MasterPage. For example:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["myVariable"] != null)
{
TextBox1.Text = Session["myVariable"].ToString();
}
}
}
}
public partial class WebFormMP_TestPublicVariable : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["myVariable"] = "Test";
}
}
}
There are many ways that you can achieve this. check around internet ;).

Calling Content Page Method from MasterPage Method [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
content page class method calling from master page class
I need to access Content Page Method from Master page Event. How can I do this?
Content Page:
public partial class Call_Center_Main : System.Web.UI.Page
{
Page_Load(object sender, EventArgs e)
{
}
public void MenuClick(string ClkMenu)
{
// Some Code
}
}
MasterPage:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
//How Can I call MenuClick method from Content Page from Here ???
}
}
This answer is taken from Interacting with the Content Page from the Master Page
You can do this using Delegates.
For Example, you have a button in MasterPage and you want to call a Method in Content Page from Master Page. Here is the Code in Master Page.
Master Page:
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (contentCallEvent != null)
contentCallEvent(this, EventArgs.Empty);
}
public event EventHandler contentCallEvent;
}
Content Page:
public partial class Content_1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void Master_ButtonClick(object sender, EventArgs e)
{
// This Method will be Called.
}
protected void Page_PreInit(object sender, EventArgs e)
{
// Create an event handler for the master page's contentCallEvent event
Master.contentCallEvent += new EventHandler(Master_ButtonClick);
}
}
And Also add the Below Line Specifying you MasterPage Path in VirtualPath
<%# MasterType VirtualPath="~/MasterPage.master" %>
// This is Strongly Typed Reference

Categories

Resources