Bulvak
Bulvak

Reputation: 1804

ASP.net form getting JS error

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";

Upvotes: 3

Views: 185

Answers (2)

Darren
Darren

Reputation: 70728

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";

Upvotes: 2

jrummell
jrummell

Reputation: 43077

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";

Upvotes: 1

Related Questions