HTML
<body> <form id="form1" runat="server"> <asp:Button runat="server" ID="a" OnClick="a_Click" Text="apd"/> </form> </body>
码
protected void a_Click(object sender,EventArgs e) { Response.Write(((Button)FindControl("a")).Text); }
此代码工作正常.
但是,这段代码:
HTML
<%@ Page Title="" Language="C#" MasterPageFile="~/Student/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Student_Default" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <asp:Button runat="server" ID="a" OnClick="a_Click" Text="andj"/> </asp:Content>
码
protected void a_Click(object sender,EventArgs e) { Response.Write(((Button)FindControl("a")).Text); }
此代码不工作,FindControl返回Null – 为什么会这样?
FindControl方法在一个简单的页面工作正常,但在母版页,它不起作用?
a的ID被更改为ctl00_ContentPlaceHolder1_a – 如何找到控件?