locked
Retain changes made by javascript in custom control RRS feed

  • Question

  • User-1779948929 posted

    Hi,

    How to reatin the changes done in the asp.net custom server control by javascript? During the postback / page reloads, changes done by the javascript in custom control is not retained.

    Saturday, April 28, 2012 6:02 PM

Answers

  • User3866881 posted

    I've created the hidden field in CreateChildControls();

    And please make sure that OnInit or OnLoad event,please use something like Context.Request.Form[hiddenfield.UniqueUrl] to accept the value。

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 1, 2012 8:58 PM
  • User-1779948929 posted

    Registered Hidden Field in OnPreRender()

    Page.ClientScript.RegisterHiddenField("sTextHiddenField", (string)ViewState["Text"]);

    Selected Text Property

    Used Context.Request.Form[hiddenfield.UniqueUrl] in property to get the changes made by javascript

           public string Text
            {
                get
                {
                    if(this.Page.IsPostBack)
                        ViewState["Text"] = this.Context.Request.Form["sTextHiddenField"].ToString();
                    return ViewState["Text"].ToString();
                }
                set
                {                
                    ViewState["Text"] = value;                          
                }
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 2, 2012 7:12 AM

All replies

  • User-917364509 posted

    Every changes made on the client side needs to be put in input fields for the server to be able to get the updates on a postback. What kind of things are you changing in javascript?

    Sunday, April 29, 2012 7:11 AM
  • User-1779948929 posted

    Hi,

    Please refer the below link, i've added the complete code of my custom control & test page.

    http://stackoverflow.com/questions/10367847/get-selected-text-asp-net-custom-server-control

    • In that code javascript changes the achor tag inner text dynamically
    • Text property in that code sets & gets the anchor tag inner text
    • During get Text, CreateChildControls() executes once again & not retains the latest value
    Sunday, April 29, 2012 12:39 PM
  • User3866881 posted

    Hi,

    How to reatin the changes done in the asp.net custom server control by javascript? During the postback / page reloads, changes done by the javascript in custom control is not retained.

    You can use a hidden field to store the value changed by javascript,and then at the beginning of Page_Load,you can use Request.Form["hiddenfield"] to fetch the value。

    Reguards!

    Sunday, April 29, 2012 9:22 PM
  • User-1779948929 posted

    You can use a hidden field to store the value changed by javascript,and then at the beginning of Page_Load,you can use Request.Form["hiddenfield"] to fetch the value。

    Hi,

    I've created hidden field in custom control, the value is changing from client page, when postback occurs it is not retaining the latest value as the control is created once on postback.

    Monday, April 30, 2012 5:46 AM
  • User3866881 posted

    I've created hidden field in custom control, the value is changing from client page, when postback occurs it is not retaining the latest value as the control is created once on postback.

    Where and how did you create it?

    Tuesday, May 1, 2012 1:28 AM
  • User-1011137159 posted

    Hi,

    How to reatin the changes done in the asp.net custom server control by javascript? During the postback / page reloads, changes done by the javascript in custom control is not retained.

    Save all changes in input fied like text box and other text box make sure that all are server side control mean runat ="server" is set. and fill all this field by javascripts so that will be retain there view by viewstate.

    Tuesday, May 1, 2012 1:44 AM
  • User-1779948929 posted

    Where and how did you create it?

    I've created the hidden field in CreateChildControls();

    Tuesday, May 1, 2012 1:14 PM
  • User3866881 posted

    I've created the hidden field in CreateChildControls();

    And please make sure that OnInit or OnLoad event,please use something like Context.Request.Form[hiddenfield.UniqueUrl] to accept the value。

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 1, 2012 8:58 PM
  • User-1779948929 posted

    Registered Hidden Field in OnPreRender()

    Page.ClientScript.RegisterHiddenField("sTextHiddenField", (string)ViewState["Text"]);

    Selected Text Property

    Used Context.Request.Form[hiddenfield.UniqueUrl] in property to get the changes made by javascript

           public string Text
            {
                get
                {
                    if(this.Page.IsPostBack)
                        ViewState["Text"] = this.Context.Request.Form["sTextHiddenField"].ToString();
                    return ViewState["Text"].ToString();
                }
                set
                {                
                    ViewState["Text"] = value;                          
                }
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 2, 2012 7:12 AM