Several Methods for retrieving controls in ASP. NET

Source: Internet
Author: User
Method 1: Javascript

Function btnclear ()
{
For (I = 0; I <document. Forms [0]. elements. length; I ++)
{
E = Document. Forms [0]. elements [I];
If (E. type = 'text' | E. type = 'textea ')
{
E. value = "";
}
}
Return false;
}

Reference in onclick onfocus onload and other events.

Method 2: foreach Traversal

Foreach (control CTL in this. Page. controls [1]. Controls)
{

If (CTL. GetType () = typeof (textbox ))
{
Textbox t = (textbox) CTL;
T. Text = string. empty;

}

Method 3:
# Region clears all controls on the specified page, public static void clearallcontent ()
/// <Summary>
/// Clear all the controls on the specified page, including Textbox, checkbox, checkboxlist, radiobutton, and radiobuttonlist. But not clear
/// In addition to ListBox and dropdownlist, such control values can be used for the current page. Generally, these controls contain stored Dictionary data.
/// Author: Kevin
/// Date: 2004-12-02
/// </Summary>
/// <Param name = "page"> specified page </param>
Public static void clearallcontent (system. Web. UI. Control page)
{
Int npagecontrols = page. Controls. count;
For (INT I = 0; I <npagecontrols; I ++)
{
Foreach (system. Web. UI. Control in page. controls [I]. Controls)
{
If (control. hascontrols ())
{
Clearalltext (control );
}
Else
{
If (control is textbox)
(Control as textbox). Text = "";

If (control is checkbox)
(Control as checkbox). Checked = false;

If (control is radiobuttonlist)
(Control as radiobuttonlist). selectedindex =-1;

If (control is radiobutton)
(Control as radiobutton). Checked = false;

If (control is checkboxlist)
{
Foreach (listitem item in (control as checkboxlist). Items)
{
Item. Selected = false;
}
}
} // If... else
} // Foreach
} //
}
# Endregion

Method 4:

Create a class to store the names and values of textbox. The Code is as follows:
Public class utilityobj
Private _ name as string
Private _ value as string
Public sub new (byval name as string, byval value as string)
_ Name = Name
_ Value = Value
End sub
Public property name () as string
Get
Return _ name
End get
Set (byval value as string)
_ Name = Name
End set
End Property
Public property value () as string
Get
Return (_ value)
End get
Set (byval value as string)
_ Value = Value
End set
End Property
End Class
This class contains two attributes: "name" and "value", and defines a public arraylist to store data.

To traverse all the controls on the ASP. NET page, we also need to define a main method. This method receives a control parameter. If this parameter is Textbox, its name and value are stored.
The Code is as follows:
Public sub loopingcontrols (byval ocontrol as control)
Dim frmctrl as control
Oarraylist = new arraylist
For each frmctrl in ocontrol. Controls
If typeof frmctrl is textbox then
Oarraylist. Add (New utilityobj (frmctrl. ID, directcast (frmctrl, textbox). Text ))
End if
If frmctrl. hascontrols then
Loopingcontrols (frmctrl)
End if
Next
End sub
Use the above method to traverse all the controls on the page
Loopingcontrols (page)
Datagrid1.datasource = oarraylist
Datagrid1.databind ()

/// <Summary>
/// Traverse the label of the page and assign a value, /// </Summary>
Protected void renamelabel ()
{
Foreach (control CTR in this. pnlmaterial. Controls)
{

If (CTR. GetType (). tostring () = "system. Web. UI. webcontrols. Label ")
{
// Response. Write ("ID:" + Ctr. ID + "text:" + (Label) CTR). Text + "<br> ");

If (CTR. Id. tostring (). Length = 6)
{
Int labelid1 = convert. toint32 (CTR. Id. substring (5, 1). tostring (). Trim ());

(Label) CTR). Text = BLL. EDI. editype. gettypename (labelid1 );
}

If (CTR. Id. tostring (). Length = 7)
{
Int labelid2 = convert. toint32 (CTR. Id. substring (5, 2). tostring (). Trim ());

(Label) CTR). Text = BLL. EDI. editype. gettypename (labelid2 );

}

}

}

}

/// <Summary>
/// Retrieve the dropdownlist Value
/// </Summary>
Protected void setdropdownlistvalue ()
{
Foreach (control CTR in this. pnlmaterial. Controls)
{

If (CTR. GetType (). tostring () = "system. Web. UI. webcontrols. dropdownlist ")
{
// Response. Write ("ID:" + Ctr. ID + "text:" + (dropdownlist) CTR). datatextfield. tostring () + "<br> ");

If (CTR. Id. tostring (). Length = 13)
{
Int labelid1 = convert. toint32 (CTR. Id. substring (12, 1). tostring (). Trim ());

(Dropdownlist) CTR). datasource = BLL. EDI. edimaterial. List (labelid1 );

(Dropdownlist) CTR). datavaluefield = "mname ";

(Dropdownlist) CTR). datatextfield = "mcode ";

(Dropdownlist) CTR). databind ();
}

If (CTR. Id. tostring (). Length = 14)
{
Int labelid2 = convert. toint32 (CTR. Id. substring (12, 2). tostring (). Trim ());

(Dropdownlist) CTR). datasource = BLL. EDI. edimaterial. List (labelid2 );

(Dropdownlist) CTR). datavaluefield = "mname ";

(Dropdownlist) CTR). datatextfield = "mcode ";

(Dropdownlist) CTR). databind ();
}

}

}

}

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.