首页 > 解决方案 > 未捕获的类型错误:无法在 asp.net 中设置 null 的属性“值”

问题描述

我收到一个错误 Uncaught TypeError: Cannot set property 'value' of null in asp.net using javascript 当我单击网格视图时。为什么 document.getElementById() 将属性“值”设置为空。这是javascript中的示例代码。我的代码中关于从字段设置值有什么问题吗?

这是后面的代码

protected void gvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onclick", "ViewEmployeeInfo('" + e.Row.Cells[0].Text.Trim() + "','" + e.Row.Cells[1].Text.Trim().Replace("Ñ", "Ñ") + "','" + e.Row.Cells[2].Text.Trim() + "','" + e.Row.Cells[3].Text.Trim() + "')"); 
        }
    }

我有 4 个参数需要在 iframe/modal 的 4 个字段中设置一个值。

function ViewEmployeeInfo(field1, field2, field3, field4) {
document.getElementById("ctl00_MainContent1_tbEmployeeID").value = field1;
document.getElementById("ctl00_MainContent1_tbEmployeeName").value = field2;
document.getElementById("ctl00_MainContent1_tbDepartment").value = field3;
document.getElementById("ctl00_MainContent1_tbCompany").value = field4;  }

这是html方面

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" >
</asp:ScriptManager>

<div class="setWindow" style="background-image: url('Images/BorderDesign/divBG1.png')">
    <div class="setMenu">
        <div class="setModuleLink">
        </div>
    </div>
    <div class="setContentUser">
        <div class="userInformation">
            <div class="settingsTabTitle">
                User Setup</div>
            <hr />
            <div class="MBSGridviewUser" style="width: 45%; position: relative; float: left">
                <table border="0">
                    <tr>
                        <td colspan="">
                            <h3>
                                User Information</h3>
                        </td>
                    </tr>
                    <asp:Literal ID="Literal2" runat="server"></asp:Literal>
                    <tr>
                        <td align="left">
                            Employee Name:
                        </td>
                        <td align="left" class="style2">
                            <asp:TextBox ID="tbEmployeeName" runat="server" Width="209px" onfocus="this.blur()"></asp:TextBox>
                              <input id="EmployeeNameLookUp" class="buttonF" type="button" value="View" onclick="ViewModal();" />
                        </td>
                    </tr>
                    <tr>
                        <td align="left">
                            Employee ID:
                        </td>
                        <td align="left" class="style2">
                            <asp:TextBox ID="tbEmployeeID" runat="server" Width="209px" onfocus="this.blur()"></asp:TextBox>

                        </td>
                    </tr>
                    <tr>
                        <td align="left">
                            User ID:
                        </td>
                        <td align="left" class="style2">
                            <asp:TextBox ID="tbUserID" runat="server" Width="209px"></asp:TextBox>
                            &nbsp;
                        </td>
                    </tr>
                    </tr>--%>
                    <tr>
                        <td align="left">
                            Company:
                        </td>
                        <td align="left" class="style2">
                            <asp:TextBox ID="tbCompany" runat="server" Width="209px" onfocus="this.blur()"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td align="left">
                            Department:
                        </td>
                        <td align="left" class="style2">
                            <asp:TextBox ID="tbDepartment" runat="server" Width="209px" onfocus="this.blur()"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td align="left">
                            Email Address:
                        </td>
                        <td align="left" class="style2">
                            <asp:TextBox ID="tbEmailAdd" runat="server" Width="209px"></asp:TextBox>
                        </td>
                    </tr>
                </table>

标签: javascriptasp.net

解决方案


推荐阅读