Traverse (PAGE) connections, text boxes, tables

Source: Internet
Author: User
Want to traverse every TextBox Control on the page?
The Code is as follows: foreach (Control con in this. Page. Controls [1]. Controls)
{
If (con is TextBox)
{
(Con as TextBox). Text = "Hello world! ";
}
}

Use this. Page. Controls to obtain the ControlCollection, which contains three members and the Owner is:
This. Page. Controls [0] -- System. Web. UI. ResourceBasedLiteralControl
This. Page. Controls [1] -- System. Web. UI. HtmlControls. HtmlForm
This. Page. Controls [2] -- System. Web. UI. LiteralControl

It can be seen that if you want to get all TextBox Controls, You must select this. Page. Controls [1] and then obtain the Controls set under it to get the TextBox you want. This is because the TextBox is a child control of the Web Form control (the HtmlForm control)

------------------------------------------------------------

Gorgeous split line

------------------------------------------------------------

C # page TextBox Traversal
Public void SetTextBox (Control obj)
{
For (int I = 0; I <obj. Controls. Count; I ++)
{
If (obj. Controls [I]. Count> 0)
SetTextBox (obj. Controls [I]);
Else
{
If (obj. Controls [I]. GetType = typeof (TextBox ))
(TextBox) obj. Controls [I]). Text = "textbox ";
}
}
}

Protected void Page_Load (object sender, EventArgs e)
{
SetTextBox (Page );
}

// Needs to be modified
Foreach (Control ctl in this. Controls)
{
If (typeof (ctl) = typeof (TextBox ))
{
Ctl. Text = "I'm textbox ";
}
}

//*************************
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack) {getTextBox (Page );}
}
Private void getTextBox (Control obj)
{
For (int I = 0; I <obj. Controls. Count; I ++)
{
If (obj. Controls [I]. Controls. Count> 0)
GetTextBox (obj. Controls [I]);
Else
{
If (obj. Controls [I]. GetType () = typeof (TextBox ))
(TextBox) obj. Controls [I]). Text = "I Am a Text box ";
}
}

}

//****************************
Protected void button#click (object sender, EventArgs e)
{
SetTextBox (Page );

}
Public void SetTextBox (Control obj)
{
For (int I = 0; I <obj. Controls. Count; I ++)
{
If (obj. Controls [I]. HasControls ())
{
SetTextBox (obj. Controls [I]);
}
Else
{
If (obj. Controls [I] is TextBox)
{
(Obj. Controls [I] as TextBox). Text = "test ";
}
}
}

}

Three methods: recursively obtain all Textbox on the page

------------------------------------------------------------

Gorgeous split line

------------------------------------------------------------

Javascript traversal (PAGE) connection, text box, table. txt

Traverse all links and set them to invisible.
Var a = document. getElementsByTagName ("");
For (var I = 0; I <a. length; I ++)
{
If (a [I]. name. indexOf ('idview ')! =-1)
A [I]. style. display = 'none ';
}

Traverse all text boxes.

Var list = document. getElementsByTagName ("input ");

For (var I = 0; I <list. length & list [I]; I ++)
{
// Determine whether it is a text box
If (list [I]. type = "text ")
{
For (var j = 0; j <86; j ++)
{
If (list [I]. id = JsDispatch. value [0] [j])
{
List [I]. value = JsDispatch. value [1] [j];
}
}
}
}
Traverse tables, DataGrid, and GridView

Function getPreTicket ()
{
Var _ table = document. getElementById ("gvTicketList"); // obtain the table gvTicketList as the GivView Control name;
Var _ trs = _ table. all. tags ('tr'); // obtain all rows;
For (var I = 0; I <_ trs. length; I ++)
{
If (_ trs [I]. id. indexOf (_ currentTicketID )! =-1) // check whether the current row has a primary key bound to the table,
{
I --; // switch to the upstream
If (_ trs [I]! = Null & _ trs [I]. id! = "")
{
Var _ s1 = "gvTicketList ";
BindData (_ trs [I]. id. substring (_ s1.length + 1); // pass this row ID to other functions
}
Else
{
Alert ("This is the first record of the current page! ");
Document. getElementById ("aPreTicket"). style. disbaled = true;
Break;
}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.