HoBa
HoBa

Reputation: 3604

How to pass javascript var to server side on asp.net

I'm trying to pass a textarea value to the server side. The textarea cant be runat=server though. heres my code:

<script type="text/javascript">
        function replace() {
            //Replace < and > on textarea
            var obj = document.getElementById('recipient_list');
            var str = obj.value;
            str = str.replace(/</i, "(");
            str = str.replace(/>/i, ")");
            obj.value = str;
            alert("rec_lst.value: " + document.getElementById('recipient_list').value);

            //Pass value to server.
            alert("passing to server");
            document.getElementById("ctl00$ContentPlaceHolder1$txtEmails").value = str;
            alert("Passed to server");
            alert("txtEmails.value: " + document.getElementById("ctl00$ContentPlaceHolder1$txtEmails").value);
        }
    </script>

This isn't working though... Any ideas how to fix or better implement this??..

Upvotes: 0

Views: 4875

Answers (2)

anishMarokey
anishMarokey

Reputation: 11397

you cant pass value from client to server using JS .

JS is only a client side code.If you can do something like this, Hacking will be so easy :)

one thing you can do is add some text or label control as invisible and assign the value to the control and pass it to server when some event happens.

Sample code

 <asp:TextBox ID="textbox1" runat="server" ></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="callOnEvets()"/>



function callOnEvets()
    {document.getElementById('textbox1').value = 10; }

Upvotes: 0

Shadow Wizard
Shadow Wizard

Reputation: 66389

Try this:

var txtEmail = document.getElementById("<%=txtEmails.ClientID%>");
txtEmail.value = str;

However this won't pass anything to the server, just change text box value.

To have it sent to the server user will have to click submit button, or if you have server side button have such code to "auto click" it thus performing post back:

document.getElementById("<%=btnSubmit.ClientID%>").click();

Upvotes: 2

Related Questions