How to get value of textbox in code-behind which was set by Javascript?

1.3k views Asked by At

I'm using ASP.net and setting the value of a textbox with Javascript. When I go to save the contents of the page I am unable to get the value of the textbox, it just comes back as nothing.

I have tried setting the value using value, innertext and innerhtml and unable to get the value from any of them. Setting using JS:

document.getElementById('<%= txtNettWeight.ClientID %>').innerText = NettWeight;

On Save:

if (!string.IsNullOrEmpty(txtNettWeight.Text))
    {                                      sqlComm.Parameters.AddWithValue("@NetWeight", Convert.ToDecimal(txtNettWeight.Text));
    }
1

There are 1 answers

3
Satinder singh On

try using single qoutes

document.getElementById('<%= txtNettWeight.ClientID %>').value = "Heloo world";

This works for me,

Html Markup:

<asp:TextBox ID="txtNettWeight" runat="server"></asp:TextBox>
<div id="test">test<div>

Client-side:

$("#test").on('click', function () {
     document.getElementById('<%= txtNettWeight.ClientID %>').value = "Heloo world";
  });

Code behind:

string getValue=txtNettWeight.Text;