ASP.NET的document.getElementById('<%= Control.ClientID%GT;');返回null [英] ASP.NET document.getElementById('<%=Control.ClientID%>'); returns null

查看:220
本文介绍了ASP.NET的document.getElementById('<%= Control.ClientID%GT;');返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找回在JavaScript中的服务器控件。出于测试目的,我呼吁从页面加载事件的JavaScript函数。

I'm trying to retrieve a server control in JavaScript. For testing purposes I am calling the JavaScript function from the page load event.

protected void Page_Load(object sender, EventArgs e){
    ClientScript.RegisterClientScriptBlock(GetType(), "js", "confirmCallBack();", true);
}

和我的JavaScript函数就是

And my JavaScript function is

function confirmCallBack() {
    var a = document.getElementById('<%= Page.Master.FindControl("PlaceHolderContent").FindControl("Button1").ClientID %>');
    var b = document.getElementById('<%=Button1.ClientID%>');
}

我的问题是,A和B都返回NULL。甚至当我查看网页源代码正确的ClientID被返回。

my problem is that both a and b return null. Even when I view the page source the correct ClientID is returned.

我要补充一点,我使用的母版页。

I should add that I'm using master page.

任何想法。

推荐答案

疑难杂症!

您必须使用的RegisterStartupScript 而不是的RegisterClientScriptBlock

下面我举的例子。

母版:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs"
    Inherits="prueba.MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">

        function confirmCallBack() {
            var a = document.getElementById('<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("Button1").ClientID %>');

            alert(a.value);

        }

    </script>

    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

WebForm1.aspx的

WebForm1.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true"
    CodeBehind="WebForm1.aspx.cs" Inherits="prueba.WebForm1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Content>

WebForm1.aspx.cs中

WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace prueba
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "js", "confirmCallBack();", true);

        }
    }
}

这篇关于ASP.NET的document.getElementById('&LT;%= Control.ClientID%GT;');返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆