javascript - 无法在 JavaScript 中设置 null 的属性 'value'

标签 javascript asp.net asp.net-webpages

我在名为“item_pricecataglog.js”的 JavaScript 代码行中收到以下错误“无法将属性“值”设置为 null”

document.getElementById('ctl00_ContentPlaceHolder1_ctl00_hdn_returnboolvalue').value = 'true';

我认为这是因为我的应用程序在网页或控件中找不到相应的元素。

奇怪的是,元素“hdn_returnboolvalue”是在引用该java脚本的Web控件中声明的。下面是我的网络控件的片段

<%@ control language="C#" autoeventwireup="true" CodeBehind="EstimateProductcatalogueBind.ascx.cs" Inherits="ePrint.usercontrol.ProductCatalogue.EstimateProductcatalogueBind" %>

<script type="text/javascript" src="<%=strSitepath %>js/item/item_pricecatalog.js"
language="javascript"></script>
<div>
 <asp:HiddenField ID="hdn_kitavailibility" runat="server" Value="0" />
 <asp:HiddenField ID="hdn_returnboolvalue" runat="server" Value="" />
</div>

上面的控件位于弹出窗口内,该弹出窗口是名为“common_popup”的 aspx 页面。我已将控件添加到在弹出的 aspx 页面中声明的“ContentPlaceHolder1”。

<%@ page language="C#" masterpagefile="~/Templates/popUpMasterPage.master" autoeventwireup="true" CodeBehind="common_popup.aspx.cs" Inherits="ePrint.common.common_popup" title="Untitled Page" enableviewstatemac="false" enableEventValidation="false" theme="Theme1" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <script type="text/javascript" language="javascript" src="<%=strSitepath %>js/Item/general.js?VN='<%=VersionNumber%>'"></script>
   <div>
    <UC:callClass ID="usrCallclass" runat="server" />
   </div>
   <div>
    <asp:PlaceHolder ID="plhDiv" runat="server"></asp:PlaceHolder>
   </div>
  </asp:Content>

下面是我将控件添加到此页面的 C# 代码片段

            UserControl userControl37 = (UserControl)base.LoadControl("~/usercontrol/ProductCatalogue/EstimateProductcatalogueBind.ascx");
            this.plhDiv.Controls.Add(userControl37);

最佳答案

我认为正确的代码应该是(注意更改后的id):

document.getElementById('ContentPlaceHolder1_PriceCatalog_hdn_returnboolvalue').value = 'true';

关于javascript - 无法在 JavaScript 中设置 null 的属性 'value',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39505418/

相关文章:

asp.net-mvc - MVC 5 来自网页

javascript - Node JS 或 Express JS HTTP GET 请求

Javascript replace() 每次只发生一次

c# - asp :ListView 中当前项目的 Eval()

c# - 如何在 Linqpad 中启用 ASP.NET 角色管理器?

javascript - 从 Node 更新 Mongodb

javascript - 设置属性时如何检查值是否有效

c# - 检测到多个匹配标记?

Asp.Net 网页网站 - 如何添加 Web Api Controller