Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Custom control loose value on postback

Custom control loose value on postback

Scheduled Pinned Locked Moved ASP.NET
javascriptsysadmin
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Irfan x
    wrote on last edited by
    #1

    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; } }

    M J 2 Replies Last reply
    0
    • I Irfan x

      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; } }

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      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.

      I 1 Reply Last reply
      0
      • M minhpc_bk

        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.

        I Offline
        I Offline
        Irfan x
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • I Irfan x

          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

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          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 1 Reply Last reply
          0
          • M minhpc_bk

            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 Offline
            I Offline
            Irfan x
            wrote on last edited by
            #5

            Thsnk for your help i have implement IPOSTBACKEVENTHandler now it is working Thanks agian Shahzad

            1 Reply Last reply
            0
            • I Irfan x

              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; } }

              J Offline
              J Offline
              jjrdk
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups