倒js数组还是cs数组? js: var arr=new Array(); var inputs=document.getElementsByTagName("INPUT"); var j=0; for(var i=0;i<inputs.length;i++){ if(inputs[i].type=='text'){ arr[j]=inputs[i]; j++; } } cs: private ArrayList al=new ArrayList(); void PageLoad(object sender,EventArgs e){ AddControl(Page); }void AddControl(control ctrl){ foreach(control ctl in ctrl.Controls){ if(ctrl is TextBox){ al.Add(ctrl); } AddControl(ctl); } }
void AddControl(control ctrl){ foreach(control ctl in ctrl.Controls){ if(ctl is TextBox){ al.Add(ctl); } AddControl(ctl); } }
if(ctl is TextBox_RP)为什么取不到呢我没有用TextBox,用的是自定义的控件,继承TextBox ,叫TextBox_RP
if(ctl is TextBox_RP) 条件没有成立
====usercontrol TextBox_RP; using System; using System.Collections.Generic; using System.Text; using System.Web.UI.WebControls;namespace Duduw.Controls { public class TextBox_RP:TextBox { public TextBox_RP() { this.Text = "Hello world"; } } } ===aspx <%@ Page Language="C#" %> <%@Register Assembly="Duduw.Controls" Namespace="Duduw.Controls" TagPrefix="duw" %> <script runat="server"> private ArrayList al = new ArrayList(); void Page_Load(object sender,EventArgs e){ rep.DataSource = new int[10]; rep.DataBind(); AddControl(Page); foreach(TextBox_RP tb in al){ Response.Write(tb.UniqueID + ":" + tb.Text + "<br>"); } } void AddControl(Control ctrl) { foreach (Control ctl in ctrl.Controls) { if (ctl is TextBox_RP) { al.Add(ctl); } AddControl(ctl); } }</script> ......... <duw:TextBox_RP runat="server" ID="rb1"></duw:TextBox_RP> <asp:Repeater runat="server" ID="rep"> <ItemTemplate> <duw:TextBox_RP runat="server" ID="rb2"></duw:TextBox_RP><br /> </ItemTemplate> </asp:Repeater> ........... ====结果 ctl00$CPH_Main$rb1:Hello world ctl00$CPH_Main$rep$ctl00$rb2:Hello world ctl00$CPH_Main$rep$ctl01$rb2:Hello world ctl00$CPH_Main$rep$ctl02$rb2:Hello world ctl00$CPH_Main$rep$ctl03$rb2:Hello world ctl00$CPH_Main$rep$ctl04$rb2:Hello world ctl00$CPH_Main$rep$ctl05$rb2:Hello world ctl00$CPH_Main$rep$ctl06$rb2:Hello world ctl00$CPH_Main$rep$ctl07$rb2:Hello world ctl00$CPH_Main$rep$ctl08$rb2:Hello world ctl00$CPH_Main$rep$ctl09$rb2:Hello world
js:
var arr=new Array();
var inputs=document.getElementsByTagName("INPUT");
var j=0;
for(var i=0;i<inputs.length;i++){
if(inputs[i].type=='text'){
arr[j]=inputs[i];
j++;
}
}
cs:
private ArrayList al=new ArrayList();
void PageLoad(object sender,EventArgs e){
AddControl(Page);
}void AddControl(control ctrl){
foreach(control ctl in ctrl.Controls){
if(ctrl is TextBox){
al.Add(ctrl);
}
AddControl(ctl);
}
}
foreach(control ctl in ctrl.Controls){
if(ctl is TextBox){
al.Add(ctl);
}
AddControl(ctl);
}
}
条件没有成立
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;namespace Duduw.Controls
{
public class TextBox_RP:TextBox
{
public TextBox_RP() {
this.Text = "Hello world";
}
}
}
===aspx
<%@ Page Language="C#" %>
<%@Register Assembly="Duduw.Controls" Namespace="Duduw.Controls" TagPrefix="duw" %>
<script runat="server">
private ArrayList al = new ArrayList();
void Page_Load(object sender,EventArgs e){
rep.DataSource = new int[10];
rep.DataBind();
AddControl(Page);
foreach(TextBox_RP tb in al){
Response.Write(tb.UniqueID + ":" + tb.Text + "<br>");
} }
void AddControl(Control ctrl)
{
foreach (Control ctl in ctrl.Controls)
{
if (ctl is TextBox_RP)
{
al.Add(ctl);
}
AddControl(ctl);
}
}</script>
.........
<duw:TextBox_RP runat="server" ID="rb1"></duw:TextBox_RP>
<asp:Repeater runat="server" ID="rep">
<ItemTemplate>
<duw:TextBox_RP runat="server" ID="rb2"></duw:TextBox_RP><br />
</ItemTemplate>
</asp:Repeater>
...........
====结果
ctl00$CPH_Main$rb1:Hello world
ctl00$CPH_Main$rep$ctl00$rb2:Hello world
ctl00$CPH_Main$rep$ctl01$rb2:Hello world
ctl00$CPH_Main$rep$ctl02$rb2:Hello world
ctl00$CPH_Main$rep$ctl03$rb2:Hello world
ctl00$CPH_Main$rep$ctl04$rb2:Hello world
ctl00$CPH_Main$rep$ctl05$rb2:Hello world
ctl00$CPH_Main$rep$ctl06$rb2:Hello world
ctl00$CPH_Main$rep$ctl07$rb2:Hello world
ctl00$CPH_Main$rep$ctl08$rb2:Hello world
ctl00$CPH_Main$rep$ctl09$rb2:Hello world