1

I am newer one in ASP.net. I want to store text in the text box from JavaScript to session variables and pass those session variables to client side JavaScript. Is this possible?

1
  • Could you explain the scenario that you want to realize? Do you want to pass textbox value from one page to another's page javascript or some else? Commented Jun 10, 2011 at 5:31

1 Answer 1

1

You will need to do this in the code behind.

To store the value from the textbox in the session, in the correct event handler you would need to put code like:

if (!IsPostback) {
    Session("TextboxContent") = txtTextbox.Text;
}

And to populate it in client side javascript, it depends on if you are using a library or not, but something that should work regardless is to have the following in your markup:

<script type="text/javascript">
    var tb = document.getElementById('<%= txtTextbox.ClientID');
    if (tb) tb.value = '<%= Session("TextboxContent").ToString().Replace("'", @"\'") %>';
</script>

Note that having code like I have done here in <%= %> ("alligator tags") is generally considered pretty bad practice, but you can use an <asp:Literal> or whatever if you like.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, if you are using the ScriptManager control, you can also do something like the following in the code behind: ScriptManager.RegisterStartupScript(Me, Me.GetType(), "PopulateTextbox", "var tb = document.getElementById('" + txtTextbox.ClientID + "'); if (tb) tb.value = '" + Session("TextboxContent").ToString().Replace("'", @"\'") + "';", true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.