查看未在运行时创建

时间:2012-11-23 09:23:39

标签: c# asp.net multiview

我使用multiview选项创建了两个视图。现在在运行时,我收到了这个错误:

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 个答案:

答案 0 :(得分:1)

一方面,您将check作为参数checkbox(check)传递给事件onload,并再次在此行中定义check

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

此外,为什么不为事件onloadonchange指定一些不同的有意义的函数名称。

ADDED ,在使用标记编辑问题后,cbxAuth是服务器控件,因此您需要获取其ClientID以获取其在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
        }; 
    }
};