asp.net method of using NamingContainer attribute to get the GridView line number _ Practical skills

Source: Internet
Author: User
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.
Let's 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 with the same Control.id property values.
Each page of the ASP.net 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.
Creating a unique namespace for server controls is especially important 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.
You can use this property to determine the naming container where a particular server control resides.
----------------------------------
Especially the last sentence.
The following is a summary of the code for the SelectedIndexChanged event:
Copy Code code as follows:

DropDownList DDL = (DropDownList) sender;
GridViewRow GVR = (gridviewrow) DDL. NamingContainer;
int id = Int. Parse (GRIDVIEW1.DATAKEYS[GVR. Rowindex][0]. ToString ());
int num = Int. Parse (DDL. Text);

The first sentence is used to get the DropDownList control that triggers the event.
The second sentence takes advantage of the control's NamingContainer property to get its container, as well as the GridViewRow object I want.
With this, the rest is the usual use, the problem is solved.

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.