Question

I have created two views using multiview option. Now during runtime, I'm getting this error:

Microsoft JScript runtime error: 'null' is null or not an object

for for this code:

window.onload = function checkbox(check) {
var check = document.getElementById("<%=cbxAuth.ClientID %>"); //<- error from here
check.onchange = function checkbox(check) {
  if (this.checked == true)
   {
    document.getElementById("<%=txtUsName.ClientID %>").disabled = false;
    document.getElementById("<%=txtPassword.ClientID %>").disabled = false;
   }
  else
   {
    document.getElementById("<%=txtUsName.ClientID %>").disabled = true;
    document.getElementById("<%=txtPassword.ClientID %>").disabled = true;
   }
 }; // Till here
};

Now when I analysed the code at dynamic, I realized that this is in my second view and the entire code is not present at the dynamic runtime.

This is my front-end code for creating views:

 <td>
 <asp:Button Text="Generate License" BorderStyle="None" ID="Tab1" CssClass="Initial"
  runat="server" OnClick="Tab1_Click" ValidationGroup="generatelicence" />
 <asp:Button Text="Mail Configuration" BorderStyle="None" ID="Tab2" CssClass="Initial"
   runat="server" OnClick="Tab2_Click" ValidationGroup="sendemail" />
 <asp:MultiView ID="MainView" runat="server">

This is the code while I create the second view:

<asp:View ID="View2" runat="server">
             <table style="width: 100%; border-width: 1px; border-color: #666; border-style: solid">
                 <tr>
                     <td class="style15">
                         FROM
                     </td>
                     <td>
                         <asp:TextBox ID="txtFrom" runat="server" Width="414px" CssClass="Textbox1"></asp:TextBox>
                         <asp:LinkButton ID="lbEdit0" runat="server" onclick="lbEdit0_Click"> Edit </asp:LinkButton>
                         <asp:RegularExpressionValidator ID="regexSendFrom" runat="server" ControlToValidate="txtFrom"
                             ErrorMessage="Enter a Valid Name" ValidationExpression="\w+([\s-_]\w+)*" ValidationGroup="sendemail"></asp:RegularExpressionValidator>
                     </td>
                 </tr>
                 <tr>
                     <td class="style15">
                         SENDER MAIL</td>
                     <td>
                         <asp:TextBox ID="txtMailAdd" runat="server" Width="414px" CssClass="Textbox1"></asp:TextBox>
                         <asp:LinkButton ID="lbEdit1" runat="server" onclick="lbEdit1_Click"> Edit
                         </asp:LinkButton>
                         <asp:RegularExpressionValidator ID="regexSender" runat="server" 
                             ControlToValidate="txtMailAdd" ErrorMessage="Incorrect Mailing Address" 
                             ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
                             ValidationGroup="sendemail"></asp:RegularExpressionValidator>
                     </td>
                 </tr>
                 <tr>
                     <td class="style15">
                         SMTP IP
                     </td>
                     <td>
                         <asp:TextBox ID="txtSMTP" runat="server" Width="414px" CssClass="Textbox1"></asp:TextBox>
                         <asp:LinkButton ID="lbEdit2" runat="server" onclick="lbEdit2_Click"> Edit
                         </asp:LinkButton>
                         <asp:RegularExpressionValidator ID="regexSMTP" runat="server" 
                             ControlToValidate="txtSMTP" ErrorMessage="IP Address is Incorrect" 
                             ValidationExpression="(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" 
                             ValidationGroup="sendemail"></asp:RegularExpressionValidator>
                     </td>
                 </tr>
                 <tr>
                     <td class="style15">
                         SMTP AUTHENTICATION
                     </td>
                     <td>
                         <asp:CheckBox ID="cbxAuth" runat="server" />
                     </td>
                 </tr>
                 <tr>
                     <td class="style16">
                         &nbsp;
                     </td>
                     <td class="style11">
                         User Name&nbsp;
                         <asp:TextBox ID="txtUsName" runat="server" Enabled="false" Width="299px" CssClass="Textbox1"></asp:TextBox>
                         <asp:LinkButton ID="lbEdit3" runat="server" OnClick="lbEdit3_Click"> Edit
                         </asp:LinkButton>
                         <br />
                         Password&nbsp;&nbsp;&nbsp;
                         <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="300px" Enabled="false"
                             CssClass="Textbox1"></asp:TextBox>
                         <asp:LinkButton ID="lbEdit4" runat="server" OnClick="lbEdit4_Click"> Edit
                         </asp:LinkButton>
                         <asp:RequiredFieldValidator ID="reqPass" runat="server" ControlToValidate="txtPassword"
                             Display="Dynamic" ErrorMessage="Enter a Password" ValidationGroup="sendemail"></asp:RequiredFieldValidator>
                         &nbsp;<asp:RequiredFieldValidator ID="reqUserMail" runat="server" ControlToValidate="txtUsName"
                             Display="Dynamic" ErrorMessage="Enter a User Name" ValidationGroup="sendemail"></asp:RequiredFieldValidator>
                         &nbsp;
                     </td>
                 </tr>
                 <tr>
                 <td class="style15"> RECEIVER </td>
                 <td> <asp:TextBox ID="txtReceiver" runat="server" CssClass="Textbox1" Width="414px"></asp:TextBox>
                 <asp:LinkButton ID="lbEdit5" runat="server" OnClick="lbEdit5_Click"> Edit </asp:LinkButton>
                 </td>

                 </tr>
                 <tr>
                     <td class="style15">
                         TO MAIL
                     </td>
                     <td>
                         <asp:TextBox ID="txtTo" runat="server" CssClass="Textbox1" Width="414px"></asp:TextBox>
                         <asp:LinkButton ID="lbEdit6" runat="server" OnClick="lbEdit6_Click"> Edit
                         </asp:LinkButton>
                         <asp:RegularExpressionValidator ID="regexTo" runat="server" 
                             ControlToValidate="txtTo" Display="Dynamic" ErrorMessage="Enter an E-Mail Address" 
                             ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                         <asp:RequiredFieldValidator ID="reqMailTo" runat="server" 
                             ControlToValidate="txtTo" ErrorMessage="Enter a Mailing Address"></asp:RequiredFieldValidator>
                     </td>
                 </tr>
                 <tr>
                     <td class="style15">
                         SUBJECT
                     </td>
                     <td class="style11">
                         <asp:TextBox ID="txtSub" runat="server" Width="414px" CssClass="Textbox1"></asp:TextBox>
                     </td>
                 </tr>
                 <tr>
                     <td class="style15">
                         CONTENT
                     </td>
                     <td>
                         <asp:TextBox ID="txtBody" runat="server" Width="414px" CssClass="Textbox1" Columns="10"
                             Rows="10" TextMode="MultiLine"></asp:TextBox>
                     </td>
                 </tr>
                 <tr>
                     <td class="style16">
                         ATTACHMENT
                     </td>
                     <td>
                         <asp:FileUpload ID="fupAttach" runat="server" CssClass="Textbox1" 
                             Width="414px" />
                     </td>
                 </tr>
                 <tr>
                     <td colspan="2">
                         &nbsp; &nbsp;
                     </td>
                 </tr>
                 <tr>
                     <td colspan="2">
                         &nbsp;&nbsp;
                     </td>
                 </tr>
                 <tr>
                     <td colspan="2">
                            <tr>
                                <td colspan="2" style="margin-left: 40px" align="center">
                                    <asp:Label ID="lblMailFail" runat="server" ForeColor="Red"></asp:Label>
                                </td>
                            </tr>
                     </td>
                 </tr>
                 <tr>
                     <td colspan="2" align="center" class="style12" style="margin-left: 500px">
                         <asp:Button ID="btnSMail" runat="server" CssClass="btn" Text="Send" 
                             onclick="btnSMail_Click" />
                         &nbsp;<asp:Button ID="btnCancelMail" runat="server" CssClass="btn" Text="Cancel" />
                     </td>
                 </tr>
                 <tr>
                     <td colspan="2">
                         &nbsp;
                     </td>
                 </tr>
             </table>
         </asp:View>

Any good reason why is this happening?

Was it helpful?

Solution

On one hand you are passing check as a parameter in checkbox(check) function for event onload and again defining the check on this line:

var check = document.getElementById("cbxAuth"); //<- error from this line

In addition, why don't you specify some different meaningful function names for events onload and onchange.

ADDED, after the question edit with markup, the cbxAuth is a Server Control so you need to get its ClientID for getting its reference in JavaScript. This way:

var chkbox = document.getElementById("<%= cbxAuth.ClientID %>");

For the other part simple change the function names to some meaningful names as explained earlier.

window.onload = function OnWindowLoad(check) {
    var chkbox = document.getElementById("<%= cbxAuth.ClientID %>");
    if (chkbox)
    {
        chkbox.onchange = function OnCheckBoxChange(chkbox) {
            // ... rest of your code
        }; 
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top