遍歷頁面控制項

2022-04-13 06:06:31 字數 3236 閱讀 4443

普通aspx頁面:

頁面所有元素

@ page language="

c#"autoeventwireup="

true

"codebehind="

default.aspx.cs

"inherits="

web._default

"%>

doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" ""

>

<

html 

xmlns

="">

<

head 

runat

="server"

>

<

title

>

controls

title

>

head

>

system.web.ui.literalcontrol-

system.web.ui.htmlcontrols.htmlhead-

system.web.ui.literalcontrol-

system.web.ui.htmlcontrols.htmlform-form1

system.web.ui.literalcontrol-

<

body

>

<

form 

id="form1"

runat

="server"

>

<

div>

<

asp:button 

id="button1"

runat

="server"

text

="button"

/>

<

asp:linkbutton 

id="linkbutton1"

runat

="server"

>

linkbutton

asp:linkbutton

>

<

asp:textbox 

id="textbox1"

runat

="server"

>

asp:textbox

>

<

asp:label 

id="label1"

runat

="server"

text

="label"

>

asp:label

>

div>

form

>

body

>

html

>

呈現最頂層控制項元素**

foreach

(control control 

inpage.controls)

顯示結果(不包含子控制項)

system.web.ui.literalcontrol-

system.web.ui.htmlcontrols.htmlhead-

system.web.ui.literalcontrol-

system.web.ui.htmlcontrols.htmlform-form1

system.web.ui.literalcontrol-

取頁面所有控制項元素,包含子控制項

取頁面所有控制項元素

protected

stringbuilder coninfo 

=new

stringbuilder();

protected

void

page_load(

object

sender, eventargs e)

response.write(coninfo.tostring());

}protected

void

outputcontrol(controlcollection controls, 

intdepth)

>", 

newstring('

-', depth *4

string

.format(

"(編號:)

", control.gettype().tostring(),control.id));

if(control.controls.count

>0&&

control.controls

!=null

string

.format(

"(擁有個子控制項)

", control.controls.count));

outputcontrol(control.controls, depth +1

);}}   

}顯示結果

呈現頁面所有控制項元素

>system.web.ui.literalcontrol(編號:)

>system.web.ui.htmlcontrols.htmlhead(編號:)(擁有1個子控制項)

---->system.web.ui.htmlcontrols.htmltitle(編號:)

>system.web.ui.literalcontrol(編號:)

>system.web.ui.htmlcontrols.htmlform(編號:form1)(擁有9個子控制項)

---->system.web.ui.literalcontrol(編號:)

---->system.web.ui.webcontrols.button(編號:button1)

---->system.web.ui.literalcontrol(編號:)

---->system.web.ui.webcontrols.linkbutton(編號:linkbutton1)

---->system.web.ui.literalcontrol(編號:)

---->system.web.ui.webcontrols.textbox(編號:textbox1)

---->system.web.ui.literalcontrol(編號:)

---->system.web.ui.webcontrols.label(編號:label1)

---->system.web.ui.literalcontrol(編號:)

>system.web.ui.literalcontrol(編號:) 

C 控制項遍歷

遍歷控制項 foreach control control in this.controls if control.name picturebox2 查詢某name的控制項 control control in this.controls 遍歷當前控制項的所有子控制項,這裡的子控制項就是form了。...

Delphi遍歷控制項

var i integer begin for i 0 to panel1.controlcount do begin 控制項 panel1.controls i 加入自己 end end procedure tform1.button2click sender tobject var i inte...

delphi控制項遍歷

delphi中,遍歷窗體上的控制項,並顯示在listbox1中 listbox1.items.add 開始遍歷控制項 for i 1 to componentcount 1 do begin listbox1.items.add components i name if components i i...