Below is the source code for a sample webpart which I have created. In this webpart I am retrieving the value that the user enters in the textbox and storing it in the variable (input). The value of this variable gets stored even if the user refreshes the page. Is there any other method I can use to do this without session variables or static variables?
Thank You,
Alex
public
class SessionVariable_WP : System.Web.UI.WebControls.WebParts.WebPart
{
RadTextBox box;
Button alex;
protected RadAjaxPanel _mainUpdatePanel = null;
protected UserControl _loadingPanelControl = null;
protected const string _templateURL = @"/_layouts/1033/tdsb_aw/controltemplates/";
string input;
public SessionVariable_WP()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(input);
RenderChildren(writer);
}
protected override void CreateChildControls()
{
try
{
Controls.Clear();
base.CreateChildControls();
box =
new RadTextBox();
box.Visible =
true;
box.ID =
"box";
box.Text = (
string)Page.Session["box"];
alex =
new Button();
alex.ID =
"button";
alex.Text =
"Click Me";
alex.Click +=
new EventHandler(alex_Click);
_mainUpdatePanel.Controls.Add(alex);
_mainUpdatePanel.Controls.Add(box);
ChildControlsCreated =
true;
}
catch (Exception ex)
{
string strMsg = "EX in CreateChildControls: " + ex.Message;
}
}
void alex_Click(object sender, EventArgs e)
{
Page.Session[
"box"] = box.Text;
input = box.Text;
input = (
string)Page.Session["box"];
}
protected override void OnInit(EventArgs e)
{
try
{
base.OnInit(e);
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
_mainUpdatePanel =
new RadAjaxPanel();
_mainUpdatePanel.ID =
"MainUpdatePanel";
_mainUpdatePanel.LoadingPanelID =
"MainLoadingPanel";
_mainUpdatePanel.EnableAJAX =
true;
if (scriptManager == null)
{
scriptManager =
new RadScriptManager();
scriptManager.ID =
"ScriptManager";
Controls.AddAt(0, scriptManager);
}
//Register the loadingpanel control
_loadingPanelControl = (
UserControl)this.Page.LoadControl(_templateURL + "Loading.ascx");
_loadingPanelControl.ID =
"MainLoadingPanelControl";
this.Controls.Add(_mainUpdatePanel);
this.Controls.Add(_loadingPanelControl);
if ((string)Page.Session["box"] != null)
{
input = (
string)Page.Session["box"];
}
EnsurePanelFix();
}
catch (Exception ex) { }
}
private void EnsurePanelFix()
{
try
{
ScriptManager.RegisterStartupScript(this, typeof(SessionVariable_WP), "UpdatePanelFixup", "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true", true);
}
catch (Exception ex) { }
}
protected override void RenderChildren(HtmlTextWriter writer)
{
try
{
if (HasControls())
{
foreach (Control curControl in Controls)
{
curControl.RenderControl(writer);
}
}
}
catch (Exception ex)
{
}
}
}