简体   繁体   中英

How Read Asp.net TextBox value when changed it text in clientside by javascript

i have a serverside textbox like this:

<asp:TextBox runat="server" id="testText" >hi this is a world!</asp:TextBox>

so i change this value in clienside with javascript like this

document.getElementById("<%=testText.ClientID%>").value="Hahaha"

when i read value it write like blow code in codebehind it print "hi this is a world!" value why?

 response.write(testText.text); // print "hi this is a world!"

When you are rendering the text through request and response, it takes the value from server so your request and response will show the value set on server. Javascript works only on client side and once the document is loaded,it is not dependent on request and response.

your question is not clarifying to me! but suppose you have also button to change the text, you can change it according to requirment

<asp:TextBox ID="testText" runat="server" ClientIDMode="Static">hi this is a world!</asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"></asp:Button>

Add an event of javascritp on your Page_Load (i am adding onclick event to button for handling javascript function)

Button1.Attributes.Add("onclick", "javascript:clientsite()");

then i think you want change server site value from client site, so for this you have to replace the value

 function clientsite() {

    var servervalue = document.getElementById("testText").value;
    var replaceIt = servervalue.replace(servervalue, "hahaha");
    document.getElementById("testText").value = replaceIt;
}

Now when you click on button it will replace or change a value from server side to client side
may be this can be helpful to you

happy coding :D

I know, this question has been asked long ago, but still adding my answer as it might be helpful to someone.

Use following line of code :

response.write(Request.Form.Get(testText.UniqueID));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM