English 中文(简体)
ASP.NET document.getElementById(<%=Control.ClientID%>);
原标题:ASP.NET document.getElementById( <%=Control.ClientID%> ); returns null

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);
}

And my JavaScript function is

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

我的问题是,一、二、一、二、二、二、二、二、二、三、三、四、五、四、五、四、五、四、五、四、五、五、四、五、四、五、五、四、五、五、四、五、五、五、八、五、八、八、五、八、八、八、八、八、八、八、八、九、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、九、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八、八 即使我看到网页来源,正确的客户信息数据库也已经归还。

I should add that I m using master page.

任何想法。

最佳回答

Gotcha!

页: 1

我在此例。

硕士:

<%@ 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

<%@ 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

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);

        }
    }
}
问题回答

是否可见<条码>Button1? 我指的是服务器方面。 确保Button1。 显而易见。

软管网(Visible)的控制以超文本形式进行,因此,虽然它们被指定为ClientID,但它们在客户方面没有实际存在。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签