2012-11-23 61 views
0

我已經使用多視圖選項創建了兩個視圖。現在運行期間,我得到這個錯誤:視圖在運行時不會創建

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

爲這個代碼:

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

現在,當我分析了動態代碼,我意識到這是我的第二個觀點並且整個代碼不存在於動態運行時。

這是我創建視圖前端代碼:

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

這是代碼,而我創造了第二種觀點:

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

什麼正當理由,爲什麼會這樣?

+1

這可以幫助你[微軟JScript運行時錯誤(http://stackoverflow.com/questions/2914033/microsoft-jscript-runtime-error-null-is-null-or-not-an-object ) –

回答

1

一方面要傳遞check作爲參數checkbox(check)功能活動onload並在此行再次定義check

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

另外,你爲什麼不指定一些不同有意義事件onloadonchange的函數名稱。

ADDED,用標記的問題編輯之後,cbxAuth是服務器控件,所以你需要得到它的客戶端ID爲獲得其在JavaScript基準。通過這種方式:

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

對於其他部分的簡單變化的函數名的一些有意義的名字前面解釋。

window.onload = function OnWindowLoad(check) { 
    var chkbox = document.getElementById("<%= cbxAuth.ClientID %>"); 
    if (chkbox) 
    { 
     chkbox.onchange = function OnCheckBoxChange(chkbox) { 
      // ... rest of your code 
     }; 
    } 
}; 
+0

你能提出一些建議嗎? – Esha

+0

看到我編輯的答案。希望這可以幫助! –

+0

此錯誤在運行時即將到來。但除此之外,我已經寫了與你給出的答案完全相同的東西。 – Esha