2

I need value to be passing from ASP.NET page to JavaScript and then to HTML textfield.

My following code is able to read values from ASP.NET to JavaScript but unable to pass value form JavaScript to HTML TextField. Can anybody please correct my code.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
        function can() {
            var myVariable = '<%=ServerSideVariable %>';
            document.write(myVariable);
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <br />
        <br />
        <br />
        <br />
        <br />
        <input id="Text1" type="text" value='can()'/>
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    public string ServerSideVariable { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        string pval = "Passed value;
        ServerSideVariable = pval;
    }

}
5
  • You don't pass the value like that from JavaScript to HTML. Commented Aug 21, 2011 at 13:46
  • Why? What's wrong? Is there any issues with this Daniel? Commented Aug 21, 2011 at 14:04
  • Because you aren't calling the function at all. Commented Aug 21, 2011 at 18:03
  • @Daniel - Is there any security issue while passing value in this manner? Commented Aug 22, 2011 at 3:15
  • @Daniel - But I found that the below code is working! Commented Aug 22, 2011 at 12:46

3 Answers 3

1
document.getElementById('Text1').value = myVariable;

But you need to move the assignment script block bellow the declaration of element Text1, or do it on document.ready

Anyway, you also need to error check. It's best to use JQuery anyway, and get familiar with it.

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

4 Comments

Should I have to modify anything in <input id="Text1" type="text" value='can()'/>
@Kars - this probably did not work because you did not put it in before load. If you move the javascript to a document ready or load event or the end of the file it should work just fine too.
@Hogan - Can you please post the syntax for me.
@quandary is correct with the syntax. (except myVariable can be replaced with <%=ServerSideVariable %>). Just put the script tag between the </form> and </body> tags at the end of the document.
0

As others have said with javascript OR like this:

(since there is nothing in your example that requires javascript -- KISS-- don't use javascript if you don't have to do so.)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <br />
        <br />
        <br />
        <br />
        <br />
        <input id="Text1" type="text" ><%=ServerSideVariable %></input>
    </div>
    </form>
</body>
</html>

4 Comments

Can I write the same code using myVariable because while passing multiple values using this ServerSideVariable will not work.
@Kars - you should be able to "pass" multiple variable this way too. What exactly do you want to do?
While passing multiple values individually how can I differentiate values using ` <%=ServerSideVariable %> ` So, is there any way to retrieve using myVariable
I still don't understand. Just use a new escape for each place you want the value eg var sam = <%=ServerSideVarSam%>; var smith = <%=ServerSideVarSmith%>. Is there something I'm missing?
0

Do this instead in a document.load.

document.getElementByID('Text1').value = myVariable;

Comments

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.