[英]Property Value gets cleared on PostBack
I have to Get and Set the BackColur of Current Page. 我必须获取并设置当前页面的BackColur。 So user can use this property from any other page 因此用户可以从任何其他页面使用此属性
And this is my code 这是我的代码
Private _BackgroundColour As System.Drawing.Color
Public Property MenuBackColour() As System.Drawing.Color
Get
Return _BackgroundColour
End Get
Set(ByVal value As System.Drawing.Color)
_BackgroundColour = value
End Set
End Property
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
NavigationMenu.BackColor = MenuBackColour
Catch ex As Exception
End Try
End Sub
But the problem is Property value gets cleared on PostBack so I'm not able to get the BackColor value from other page 但是问题是在PostBack上清除了Property值,所以我无法从其他页面获取BackColor值
On post back only the input elements are posted. 在回发时,仅输入元素。
So they are the same as before the post back, what they have before the post back, have and now. 因此,它们与回发之前,回发之前所拥有的,拥有的和现在的相同。 So actually you do not "get" this parameters on code behind, you only "set" them and on server controls with viewstate on, you can remember this parametres on post back - but you can not change them on client side and expect to read this change on server. 因此,实际上您不会在代码后面“获取”此参数,而只是在“ viewstate”处于打开状态的服务器控件上“设置”它们,您可以在回发时记住此参数-但您无法在客户端更改它们并希望读取服务器上的此更改。
Here the workaround is. 解决方法在这里。 Either use the viewstate of the page to save some values and keep them on post back, ether use input hidden elements to have them on post back. 使用页面的viewstate保存一些值并将其保留在回发中,ether使用输入隐藏元素将其保留在回发中。
If your main purpose is to retain or share the backcolor of some control between different pages, there are many ways to do it in ASP.NET. 如果您的主要目的是保留或共享不同页面之间某些控件的背景色,则在ASP.NET中有很多方法可以实现。 You can keep the value in cookies or sessions, or cache. 您可以将值保留在Cookie或会话或缓存中。
As per request, if Property must be used, I create a Default.aspx as the following: 根据要求,如果必须使用Property,我将创建一个Default.aspx,如下所示:
Public Class _Default
Inherits Page
Private Shared _BackgroundColour As System.Drawing.Color = Drawing.Color.Azure
Public Shared Property MenuBackColour() As System.Drawing.Color
Get
Return _BackgroundColour
End Get
Set(ByVal value As System.Drawing.Color)
_BackgroundColour = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
MenuBackColour = Drawing.Color.Red
Response.Redirect("Default1")
End Sub
End Class
It will automatically be redirected to Default1.aspx, where there is a Label control in that page. 它会自动重定向到Default1.aspx,该页面上有一个Label控件。 It will use _Default.MenuBackColour as the label's backcolor: 它将使用_Default.MenuBackColour作为标签的背景色:
Public Class Default1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.BackColor = _Default.MenuBackColour
End Sub
End Class
Again, this approach is not recommended. 同样,不建议使用此方法。 If Default.aspx has never been run (at least once), MenuBackColour's value may not be what you think it is. 如果Default.aspx从未运行过(至少运行过一次),则MenuBackColour的值可能与您认为的不一样。 I won't encourage people to retain any static variable or property in an .aspx page for sharing. 我不会鼓励人们在.aspx页中保留任何静态变量或属性以进行共享。
In web applications, variable values simply get erased. 在Web应用程序中,变量值只是被擦除。 But it is very simple to persist these values. 但是,保留这些值非常简单。 They may be persisted using the Viewstate object. 它们可以使用Viewstate对象持久化。 Before the postback is invoked, the variable's value is saved in a viewstate object. 在调用回发之前,变量的值保存在viewstate对象中。 In the recieving page, the viewstate's value may be retrieved back. 在接收页面中,可以返回视图状态的值。
//Save the value in ViewState object before PostBack //在PostBack之前将值保存在ViewState对象中
ViewState("myColour")="Black";
//Retrive the value from ViewState after the PostBack //在PostBack之后从ViewState检索值
myColourProperty=ViewState("myColour").ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.