3

I have an ASP.NET form (C#) but which I click this button that should execute a JS method I get the error "Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object"

This is odd because it should work:

                <asp:LinkButton ID="btnDriverPopulate" runat="server" Text="" Height="55px" Width="172px" CssClass="btnMTO" 
                    OnClientClick="readLicense()" ToolTip="Check Driver" /> 

textbox code here

 <asp:TextBox ID="DriverLast" runat="server" Width="180px" Text='<%# Eval("Last") %>' ToolTip="Enter driver last name"></asp:TextBox>
                                    <cc:TextBoxValidator ID="valDriverLast" runat="server" AllowBlank="false" Display="dynamic" ControlToValidate="DriverLast" ValidationGroup="ValGrpDriver"
                                        CorrectFormat="Please enter in alphanumeric characters only." FieldName="Last Name" RegString="^[a-zA-Z0-9\s.,\-']+$" />

JS function readLicense that SHOULD set the textbox's value to "hi"

document.getElementById("DriverLast").value = "hi";

2 Answers 2

1

In the markup is there an input with the id DriverLast? ASP.NET may changed the ID of this control to it's container ID, i.e: _ctl_DriverLast. Therefore you could use the ClientId property.

Try:

document.getElementById(<%=DriverLast.ClientId %>.value = "Hi";
Sign up to request clarification or add additional context in comments.

1 Comment

Look in the markup, do you see an element with the ID DriverLast?
0

The rendered client id isn't necessarily the same as the server control id. You can use the ClientIDMode property to change this behavior.

document.getElementById("<%= DriverLast.ClientID %>").value = "hi";

2 Comments

Can you verify that the id in your script matches the id of the html element?
I did that and you're right! the id seems to be different when I go to my html source, I pasted the html source textbox's ID and no error!

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.