use*_*443 4 javascript asp.net
我正在尝试在JavaScript中检索服务器控件.出于测试目的,我从页面加载事件调用JavaScript函数.
protected void Page_Load(object sender, EventArgs e){
ClientScript.RegisterClientScriptBlock(GetType(), "js", "confirmCallBack();", true);
}
Run Code Online (Sandbox Code Playgroud)
我的JavaScript功能是
function confirmCallBack() {
var a = document.getElementById('<%= Page.Master.FindControl("PlaceHolderContent").FindControl("Button1").ClientID %>');
var b = document.getElementById('<%=Button1.ClientID%>');
}
Run Code Online (Sandbox Code Playgroud)
我的问题是a和b都返回null.即使我查看页面源,也会返回正确的ClientID.
我应该补充说我正在使用母版页.
有任何想法吗.
疑难杂症!
你必须使用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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
99726 次 |
| 最近记录: |