asp.net method of using NamingContainer attribute to obtain GridView line number
Source: Internet
Author: User
In a recent project, you used to add a DropDownList control to the GridView Template column and open its AutoPostBack property. When the SelectedIndexChanged event occurs, you want to get the line number at the same time, thus obtaining the appropriate line information. Because the dropdowelist is different from the button, the CommandName cannot be specified, so there is no way to capture the index of the row in the normal way. Quite a bit of trouble, later found using the NamingContainer attribute to solve the problem.
First take a look at Microsoft's explanation of this attribute:
----------------------------------
gets a reference to the server control's naming container, which creates a unique namespace to differentiate between server controls that have the same Control.id property values.
asp.net each page of a WEB application contains a hierarchy of controls. This hierarchy has nothing to do with whether the control generates a user-visible UI. The naming container for a given control is the parent control on top of the control in the hierarchy, which implements the INamingContainer interface. The server control that implements this interface creates a unique namespace for the ID property value of its child server control.
It is especially important to create a unique namespace for server controls when data binding is performed against list Web server controls, such as Repeater and DataList server controls. When multiple items in a data source create multiple instances of a server control, and the server control is a child of a repeating control, the naming container ensures that each instance of those child controls has a UniqueID property value that does not conflict. The default naming container for a page is an instance of the page class that is generated when you request it.
can use this property to determine the naming container in which a particular server control resides.
----------------------------------
especially the last sentence.
The following is a code summary of the SelectedIndexChanged event:
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