Custom control loose value on postback
-
I have make a custom control it has a property the value of property can be set & get from server side & also from client side (javascript). when i set value from server side on post back control hold it value while when i set value from client side on post back control loose its value How can to persist it that control can't loose value set throgh client side on post back Thanks Shahzad This is the sample code for property [Bindable(true), Category("Appearance"), DefaultValue("block")] public string Value { get { object o = ViewState["Value"]; if (o == null) { return null; } return (string)o; } set { ViewState["Value"] = value; } }
-
I have make a custom control it has a property the value of property can be set & get from server side & also from client side (javascript). when i set value from server side on post back control hold it value while when i set value from client side on post back control loose its value How can to persist it that control can't loose value set throgh client side on post back Thanks Shahzad This is the sample code for property [Bindable(true), Category("Appearance"), DefaultValue("block")] public string Value { get { object o = ViewState["Value"]; if (o == null) { return null; } return (string)o; } set { ViewState["Value"] = value; } }
Hi there, The value of the property is persisted in the ViewState (a hidden element), and the ASP.NET basically has no idea about the change you make at the client side with client side script other than the ViewState. So if you want that to happen, you need to persist the new value somewhere such as a hidden element so that it can be posted back to the server and you can get the value and assign it to the property of your cusom control.
-
Hi there, The value of the property is persisted in the ViewState (a hidden element), and the ASP.NET basically has no idea about the change you make at the client side with client side script other than the ViewState. So if you want that to happen, you need to persist the new value somewhere such as a hidden element so that it can be posted back to the server and you can get the value and assign it to the property of your cusom control.
-
Thanks for reply As I have experience with TextBox control, its TEXT property doesn’t lose value as we set it from serverside or from client side, on postback . It persist value I want to achieve the same behavior
shahzadarfan wrote: As I have experience with TextBox control, its TEXT property doesn’t lose value as we set it from serverside or from client side, on postback . It persist value The nature of it is that the ASP.NET processes the postback data in the "Process postback data" phase of the control execution lifecycle[^]. Because the TextBox control implements the IPostBackDataHandler interface, and provides a snippet of code to get the value of the textbox sent from the client side and assign it to the Text property, as a result you can see the value at the server side. If you want to the do the same with your custom control, your custom control needs to implement the IPostBackDataHandler infertface, and provide sample code in the LoadPostData method. For more information, you can see Processing Postback Data[^]
-
shahzadarfan wrote: As I have experience with TextBox control, its TEXT property doesn’t lose value as we set it from serverside or from client side, on postback . It persist value The nature of it is that the ASP.NET processes the postback data in the "Process postback data" phase of the control execution lifecycle[^]. Because the TextBox control implements the IPostBackDataHandler interface, and provides a snippet of code to get the value of the textbox sent from the client side and assign it to the Text property, as a result you can see the value at the server side. If you want to the do the same with your custom control, your custom control needs to implement the IPostBackDataHandler infertface, and provide sample code in the LoadPostData method. For more information, you can see Processing Postback Data[^]
-
I have make a custom control it has a property the value of property can be set & get from server side & also from client side (javascript). when i set value from server side on post back control hold it value while when i set value from client side on post back control loose its value How can to persist it that control can't loose value set throgh client side on post back Thanks Shahzad This is the sample code for property [Bindable(true), Category("Appearance"), DefaultValue("block")] public string Value { get { object o = ViewState["Value"]; if (o == null) { return null; } return (string)o; } set { ViewState["Value"] = value; } }
There is not need for hidden fields or things like that. Make sure that your control implements the IPostBackDataHandler and then in the LoadPostData method you assign the control the value from the valuecollection, e.g. this.Value = postCollection.Item(this.UniqueID). jjrdk