0

I am a newbie to JavaScript and I coded the following small JavaScript to make a postback function and its not working. I need anybody to correct it please.

C# code:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TextBox4.Text = "nopost";
        }
        else
        {
            TextBox4.Text = "post";
        }
    }

JavaScript:

    <script type="text/javascript">
        function a1(){
        var hid = document.getElementById('TextBox4').Value;
        if (hid == "post") {
            alert('Posted');
        }
        else if (hid == "nopost") {
        alert('Not Posted');
        }
        }
        window.onload == function () {
            a1();
        }
    </script>

ASP code

    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
5
  • 2
    I am not sure if this is the error but use small v for value document.getElementById('TextBox4').value; Commented Jan 17, 2012 at 17:09
  • 2
    What does "not working" mean? Does the Javascript get sent to the browser correctly? Do you get any errors in your browser's Javascript console? Commented Jan 17, 2012 at 17:10
  • 1
    To expand a little on jrummell's comment, "it's not working" tells us almost nothing. Always state what you expect/want to happen and what actually happens, which includes any error messages. Commented Jan 17, 2012 at 17:11
  • 1
    @jrummell, @Inerdial, @outis - Please understand one thing "JavaScript does not show errors" if syntax fails it fails to give output. Before asking What's not working read my question again and you can understand that I have issue with syntax as I am a niewbie. Commented Jan 17, 2012 at 17:36
  • 1
    @Madcoder.: please understand that JS does show errors, you just need to know where to look. Research how to open the JS console for your browser. Also, understand that "it's not working" is nowhere near enough of a problem description. My friend's computer, for example, sometimes doesn't work. Can you tell me what's wrong with it? Commented Mar 25, 2012 at 22:40

3 Answers 3

2

Try using the ClientID property and changing .Value to .value:

var hid = document.getElementById('<%= TextBox4.ClientID %>').value;
Sign up to request clarification or add additional context in comments.

4 Comments

shouldnt matter right.. he has explicitly set the id <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
If TextBox4 is in a control that implements INamingContainer, the ClientID would be Parent2_Parent1_TextBox4.
@Baz1nga it does matter as ASP.NET generates the ID to something like ctl00_ContentPlaceHolder1_TextBox4
ah ok.. I am not sure been sm time since I have dealt with those controls.. but I remember you can set the ClientId by calling sm property setter..
2

You're using == instead of = for the onload handler assignment.

This...

window.onload == function () {
    a1();
}

should be this...

window.onload = function () {
    a1();
};

or simply this...

window.onload = a1;

Comments

1

the problem might be with how you have defined the onload function change it to:

window.onload = a1;

P.S: Use the === operator in Javascript for type safe comparison.

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.