简体   繁体   English

UncaughtTypeError:无法调用null的方法“ getElementByTagName”

[英]UncaughtTypeError:Cannot call method 'getElementByTagName' of null

I am developing a website in asp.net. 我正在asp.net中开发一个网站。 I used a GridView edited with RadioButton. 我使用了用RadioButton编辑的GridView。 This is the code for the GridView. 这是GridView的代码。 Along with this RadioButton GridView is getting value from a table in DataBase and it contains 3 columns. 与此RadioButton一起,GridView从数据库的一个表中获取价值,它包含3列。

<asp:GridView ID="creditGridView" runat="server"  Width="500" Height="210px"  GridLines="None" CssClass="classGridView" >
                   <Columns>
                       <asp:TemplateField HeaderText="Pick One">
                           <ItemTemplate>
                               <asp:RadioButton ID="radBatId" runat="server" onclick= "javascript:CheckOtherIsCheckedByGVID(this);" />
                           </ItemTemplate>
                       </asp:TemplateField>
                   </Columns>
</asp:GridView>

And I used JavaScript to make the functionality, select one RadioButton at a time in GridView. 然后我使用JavaScript来实现该功能,一次在GridView中选择一个RadioButton。 This is the JavaScript code: 这是JavaScript代码:

function CheckOtherIsCheckedByGVID(rdBtn) {
        var currentRdBtnId = rdBtn.id;
        alert(currentRdBtnId);
        var parent = document.getElementById("<%=creditGridView.ClientID%>");

    var items = parent.getElementsByTagName("input");
    for (i = 0; i < items.length; i++) {
        if (items[i].id != currentRdBtnId && items[i].type == "radio") {
            if (items[i].checked) {
                items[i].checked = false;
            }
        }
        else {
            document.getElementById("<%=radioBtnIndex.ClientID%>").value = i;
        }
    }
}

When I put this function in the same page containing the GridView it is working fine. 当我将此功能放在包含GridView的同一页面中时,它工作正常。 When I put this function in a separate JavaScript file it is showing the error 当我将此功能放在单独的JavaScript文件中时,它显示错误

UncaughtTypeError:Cannot call method 'getElementByTagName' of null UncaughtTypeError:无法调用null的方法“ getElementByTagName”

Here variable parent is getting the value null . 在这里,变量parent得到的值为null ie the statement document.getElementById("<%=creditGridView.ClientID%>"); 即语句document.getElementById("<%=creditGridView.ClientID%>"); is not getting the GridView id when the function is not in the same page. 当函数不在同一页面中时,未获取GridView ID。 Can anyone help me solve this issue or give me an alternate way to access the GridView id? 谁能帮助我解决这个问题,或者给我一种替代的方式来访问GridView ID?

You can't use expressions like <%=creditGridView.ClientID%> in separate js file. 您不能在单独的js文件中使用<%=creditGridView.ClientID%>类的表达式。 If such expression placed just on aspx page it evaluated during page rendering and replaced with calculated value (eg client id of gridview control). 如果这样的表达式仅放在aspx页面上,则它将在页面渲染期间进行评估并替换为计算值(例如gridview控件的客户端ID)。 But as separate file doesn't precessed by asp.net it left as it is. 但是由于asp.net不会处理单独的文件,因此它保持原样。 You may mark GridView with some css class and use css class selectors in separeate js file to get GridView. 您可以使用某些CSS类标记GridView,并在单独的js文件中使用CSS类选择器来获取GridView。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM