开发者

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??..


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();


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; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜