Page.Controls對象

來源:互聯網
上載者:User

 執行個體一:

前台

  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>無標題頁</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div id="div1">
  10.         <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
  11.     </div>
  12.     </form>
  13. </body>
  14. </html>

後台

  1. public partial class _Default : System.Web.UI.Page 
  2. {
  3.     protected void Page_Load(object sender, EventArgs e)
  4.     {
  5.         
  6.     }
  7.     protected void Button1_Click(object sender, EventArgs e)
  8.     {
  9.         string name = "tree";
  10.         //Server.Transfer("ajax.aspx?id=1&name="+name);
  11.         ChangeControls();
  12.     }
  13.     /************Controls屬性************
  14.      * this.Controls則包括所有控制項。
  15.      *  System.Web.UI.LiteralControl
  16.         System.Web.UI.HtmlControls.HtmlHead
  17.         System.Web.UI.LiteralControl
  18.         System.Web.UI.HtmlControls.HtmlForm
  19.         System.Web.UI.LiteralControl
  20.      * 為<div id="div1">加上runat屬性,則Form.Controls裡則找不到Button1
  21.      */
  22.     private void ChangeControls()
  23.     {
  24.         foreach (System.Web.UI.Control control in this.Form.Controls)
  25.         {
  26.             if (control is Button)
  27.             {
  28.                 Button btn = (Button)control;
  29.                 btn.Text = "Hello";
  30.             }
  31.         }
  32.         foreach (Control control in this.Controls)
  33.         {
  34.             Response.Write(control.ToString() + "<br/>");
  35.         }
  36.     }
  37. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.