1

Hello I am trying to read a textbox(runatserver) after is gets populated form the server into a javascript variable,but it gives me a console error that "can't read form NULL" however the text box is populated by the string I want to read

this is my text box:

 <form runat="server">

        <asp:TextBox ID="ServerSideTextBox" runat="server" />
    </form>

This is how I am populating it in C#:

ServerSideTextBox.Text= Object_JSON_Class.JSON_DataTable(dt);

it gets the right data also shows the right data string but the PROBLEM is when I try to read the value of the text box like this:

 var oServerSideTextBox= document.getElementById("ServerSideTextBox"); 
var oServerJSON_String=eval("("+oServerSideTextBox.value+")"); 

I get a console error that I can't read form NULL,but the text box does have the string I want to read into javascript variable,please help

0

5 Answers 5

1
 var txtToIncr = document.getElementById('<%=ServerSideTextBox.ClientID%>')

Check out this link, about reading ASP.Net controls through javascript

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

8 Comments

check your generated HTML and see what is being rendered against serversidetextbox id
I saw that too the id is the same i.e serversidetextbox (ignoring caps)
try var t = document.forms[0]['ServerSideTextBox'].value; and see if it gets the value
nope ain't working,I have also read the link you have given me,according to it .ClientID should solve the problem but its not,maybe its because of something else then ID mismatch,can't figure out where I am going wrong tho
In that case, are you sure that the error is generated by the piece of code: var oServerSideTextBox= document.getElem....; Maybe you have an error on previous lines of javascript
|
1

Try this:

var oServerSideTextBox= document.getElementById("<%=ServerSideTextBox.ClientID%>");

If that does not work, try something like this:

var oServerSideTextBox= document.getElementById("<%=ServerSideTextBox.ClientID%>_text");

Components are assigned different ID's when rendered by the client's browser. You can take a look here for more information.

Comments

0

put this on yout textbox ClientIDMode="Static"

Comments

0

Your server textbox's ID will be different if you use it in a master page structure , try finding its id.Because its actual id might be something like this ctl00_ContentPlaceHolder1_ServerSideTextbox.Check that.

Assign a class to yout textbox then try this to find the id.

$('.textbox').on('blur', function() {

        var error = this.id;
        alert(error);
                                       )};

5 Comments

I am not using master page structure right now, and how do I find the actual id the text box?
@Snedden27 , did you try that
hmm its laughable but the ID in the pop-up is same ie ServerSideTextBox ,
@Snedden27 , strange I ran into similar problem sometime ago , my issue was the id.
@Snedden27 , did you check this in other browsers also
0

Reading the comments. I think you're referring to the wrong textbox-ID.. So I made a small test-thingy: this should work.. If not look into the source from your HTML - there you can see the ID.

<script type='text/javascript'>
    function testvalue() {
        var oServerSideTextBox = document.getElementById('<%=ServerSideTextBox.ClientID%>');
        if (oServerSideTextBox == null) {
            alert('this is null');
        }
        else {
            alert(oServerSideTextBox.value);
        }
    }
</script>



<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="ServerSideTextBox" runat="server" Text="Testvalue" />
        <input type='button' onclick='testvalue();' value='Click' />
    </div>
    </form>
</body>

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.