This is a migrated thread and some comments may be shown as answers.

Ways to store variables

0 Answers 139 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
LEWINA
Top achievements
Rank 1
LEWINA asked on 22 Jan 2009, 04:26 PM
Hi I am working on a webpart in sharepoint which uses Ajax controls and I was wondering what the best type of storage to use would be so that I can store user independent variables, in my case it is string variables. I have tried using static variables but they are generic throughout all the users. I have also used session variables although they reduce the performance of the server. Is there any other ways to store variables?

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)

 

{

}

}

}

No answers yet. Maybe you can help?

Tags
General Discussions
Asked by
LEWINA
Top achievements
Rank 1
Share this question
or