I would like to know if someone can tell me what is the "best practice" method for setting values on an aspx page from the code behind and have the value remain static. Here is "an example" of what I mean. please don't look too much at the specifics of this
example, for I am trying to learn the general way of doing this sort of thing, not trying to solve this specific example.
public static string ImageKey;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ImageKey = "123";
Page.DataBind();
}
}
The problem with this, as I am sure most of you have noticed, is that if any control on the page causes a PostBack to occur then the variable ImageKey has no value and thus the control imgBroker parameter for ImageKey also has no value. What I am wanting
is for ImageKey to retain the value between post backs. The reason is I may have a situation where I have to hit a database to get the value "123" and I would like to not have to repeat the sql everytime a postback occurs for efficiency. If I stored it in
a Session variable then it is stuck in memory even if the user navigates away from the page. I guess I am trying to find a way to store data in memory for the life of that page kind of the way a control works. For example, if I assign the value "john doe"
to the Text property of an asp:Label control, it keeps its value even after a post back.
Actually, I stand corrected, in the above example I realized setting the public variable ImageKey to "static" does make it retain its value after a post back. So now to clarify the question a little is I am obviously not understanding how to set the value
of the image control in such a way that it will retain its value. This problem may be simpler than I thought but I still open for suggestions.
I would like to know if someone can tell me what is the "best practice" method for setting values on an aspx page from the code behind and have the value remain static.
Best practices are a matter of opinion and my opinion is that the best practice is to separate programming code from data as much as possible. In your example, put the value 123 in a text file or database or something and read it every time. That way,
it will be a lot easier to change it to 456 if the need arises.
WayneFulcher
Member
137 Points
200 Posts
Static Values from Code Behind - best practice
Nov 08, 2012 03:09 PM|LINK
I would like to know if someone can tell me what is the "best practice" method for setting values on an aspx page from the code behind and have the value remain static. Here is "an example" of what I mean. please don't look too much at the specifics of this example, for I am trying to learn the general way of doing this sort of thing, not trying to solve this specific example.
In my .aspx page I have this
<gic:GeneratedImage ID="imgBroker" runat="server" ImageHandlerUrl="~/ImageDispatch.ashx"> <Parameters> <gic:ImageParameter Name="ImageSource" Value="Broker" /> <gic:ImageParameter Name="ImageKey" Value="<%# ImageKey %>" /> <gic:ImageParameter Name="MaxHeight" Value="100" /> <gic:ImageParameter Name="MaxWidth" Value="100" /> </Parameters> </gic:GeneratedImage>And in my .cs code behind file I have this
public static string ImageKey; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ImageKey = "123"; Page.DataBind(); } }The problem with this, as I am sure most of you have noticed, is that if any control on the page causes a PostBack to occur then the variable ImageKey has no value and thus the control imgBroker parameter for ImageKey also has no value. What I am wanting is for ImageKey to retain the value between post backs. The reason is I may have a situation where I have to hit a database to get the value "123" and I would like to not have to repeat the sql everytime a postback occurs for efficiency. If I stored it in a Session variable then it is stuck in memory even if the user navigates away from the page. I guess I am trying to find a way to store data in memory for the life of that page kind of the way a control works. For example, if I assign the value "john doe" to the Text property of an asp:Label control, it keeps its value even after a post back.
Any suggestions?
WayneFulcher
Member
137 Points
200 Posts
Re: Static Values from Code Behind - best practice
Nov 08, 2012 03:25 PM|LINK
Actually, I stand corrected, in the above example I realized setting the public variable ImageKey to "static" does make it retain its value after a post back. So now to clarify the question a little is I am obviously not understanding how to set the value of the image control in such a way that it will retain its value. This problem may be simpler than I thought but I still open for suggestions.
oned_gk
All-Star
31834 Points
6517 Posts
Re: Static Values from Code Behind - best practice
Nov 09, 2012 01:34 AM|LINK
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: Static Values from Code Behind - best practice
Nov 09, 2012 02:16 AM|LINK
Best practices are a matter of opinion and my opinion is that the best practice is to separate programming code from data as much as possible. In your example, put the value 123 in a text file or database or something and read it every time. That way, it will be a lot easier to change it to 456 if the need arises.
Shailendra S...
Member
551 Points
145 Posts
Re: Static Values from Code Behind - best practice
Nov 09, 2012 03:06 AM|LINK
you can take a hidden field and store value there.
www.techaray.com