简体   繁体   English

为什么我得到一个“无法读取属性”点击'null错误

[英]Why do I get a "Cannot read property 'click' of null error

Inside the BODY tag: BODY标签内:

<asp:Panel ID="nmSearch" CssClass="searchBox" runat="server" DefaultButton="HiddenSearchNM">
    <input type="text" runat="server" value="" placeholder="Search" id="searchB" class="styledTB searchB floatLeft" />
    <a href="JavaScript:void(0);" onclick="SearchNMClick();" title="Search" class="styledBtnSearch searchAnchor floatLeft defaultLinks">
        <asp:Image ImageUrl="~/images/searchWhite.png" CssClass="searchImg" runat="server" ToolTip="Search" AlternateText="Search" />
    </a>
    <asp:ImageButton ID="HiddenSearchNM" runat="server" CssClass="hideContent" ClientIDMode="Static" />
</asp:Panel>

Inside the HEAD tag: HEAD标签内:

<script>
function SearchNMClick() {
    document.getElementById('HiddenSearchNM').click();
}
</script>

I see the following error in the console: 我在控制台中看到以下错误:

Uncaught TypeError: Cannot read property 'click' of null
SearchNMClick
onclick

C# code behind which will fire off a search page from either Enter or click: 后面的C#代码将从Enter或单击触发搜索页面:

protected void HiddenSearchNM_Click(object sender, EventArgs e)
    {
        //MessageBox.Show("SEARCH NM");
        strSMain = searchB.Value;
        Response.Redirect("results.aspx?searchtext=" + strSMain +"&folderid=0&searchfor=all&orderby=title&orderdirection=ascending");
    }

But when I hit enter while in the textbox or the button click, I get the error above. 但是当我在文本框中按Enter键或点击按钮时,我得到上面的错误。

How do I resolve the error. 我该如何解决错误。

So weird, when I check the HTML source I see this (not sure why the ID is being changed): 太奇怪了,当我检查HTML源代码时,我看到了这一点(不确定为什么要更改ID):

<input type="image" name="ctl00$CUSTOM_Area_Top$HiddenSearchNM" id="ctl00_CUSTOM_Area_Top_HiddenSearchNM" class="hideContent" ClientIDMode="Static" src="" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$CUSTOM_Area_Top$HiddenSearchNM&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" style="border-width:0px;" />

Use a Page directive to set static ids 使用Page指令设置静态ID

<%@ Page ClientIDMode="static" %>

That should work with your id selector. 这应该适用于你的id选择器。

Also, check your rendered HTML for your ImageButton . 另外,检查您的ImageButton渲染HTML。 If everything is setup correctly, it should preserve the ID . 如果一切都设置正确,它应该保留ID

You may change you JS function to: 您可以将JS功能更改为:

function SearchNMClick() {
    document.getElementById('<%# HiddenSearchNM.ClientID %>').click();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么会出现“无法读取null值的属性”错误? - Why do I get 'cannot read property of value at null' error? 为什么我无法读取 null 的属性“样式”? - why do i get Cannot read property 'style' of null? 为什么我会收到错误“无法读取属性 'appendChild' of null” - Why did I get an error “Cannot read property 'appendChild' of null” 为什么会出现错误:Uncaught TypeError:无法使用此代码读取null的属性“ top” - Why do I get the error: Uncaught TypeError: Cannot read property 'top' of null with this code 为什么我在第 216 行收到错误:“无法读取 null 的属性 'style'”,因为第 190、200、213 行也有问题? - Why do I get the Error: “Cannot read property 'style' of null” on line 216 because of something also on lines 190, 200, 213? 我收到此错误“无法读取 null 的属性 &#39;getBBox&#39;”,我不明白为什么? - I get this error " Cannot read property 'getBBox' of null " and I couldn't understand why? 为什么我得到“Uncaught TypeError:无法读取数组中元素的属性&#39;样式&#39;? - Why do I get “Uncaught TypeError: Cannot read property 'style' of null” of an element in an array? Vue 生命周期和 firebase:为什么我会收到“TypeError: Cannot read property 'url' of null”? - Vue lifecycle and firebase: Why do I get “TypeError: Cannot read property 'url' of null”? 为什么我不断收到此错误? 未捕获的类型错误:无法读取 null 的属性“classList” - Why do I keep getting this error? Uncaught TypeError: Cannot read property 'classList' of null 尝试通过电子邮件进行身份验证时,出现“ Uncaught TypeError:无法读取null的属性&#39;value&#39;”错误。 为什么? - When attempting to authenticate with Email, I get an “Uncaught TypeError: Cannot read property 'value' of null” Error. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM