Accumulation of ASP. NET series (1): retrieving data from selected rows in the gridview

Source: Internet
Author: User

When I made a website last time, I encountered a problem: In the gridview bound with data, I need to read the data in the selected row and save it to a struct to pass the data to the next page. I asked some friends about the data and found no result, A friend said that he could pass the ID of the Data row to the next page and then bind it. At that time, he thought that this method would work, however, when I do this, I know that it is too troublesome to bind the ID passed on the page above. I believe it is more convenient to transfer the ID to the next page through the structure, so I have been searching for methods. This morning, I found that it is not difficult to obtain a row of data:

First, add a template column to select or obtain the row number of the selected row, and then use gridview1.rows [E. newselectedindex]. cells [0]. Text; to obtain the desired data unit. You only need to save no unit data as a struct.

Note that when setting the template column, I set the commandname attribute of the gridview to select (this is the default value) to get the row number.

Protected void gridviewincluselectedindexchanging (Object sender, gridviewselecteventargs E)
...{
Providerdetail pdetail = new providerdetail ();
Pdetail. sgamename = gridview1.rows [E. newselectedindex]. cells [0]. text;
Pdetail. sareaname = gridview1.rows [E. newselectedindex]. cells [1]. text;
Pdetail. sservername = gridview1.rows [E. newselectedindex]. cells [2]. text;
Pdetail. nmaxnum = gridview1.rows [E. newselectedindex]. cells [3]. text;
Pdetail. sprice = gridview1.rows [E. newselectedindex]. cells [4]. text;
Pdetail. smindeal = gridview1.rows [E. newselectedindex]. cells [5]. text;

Session ["PD"] = pdetail;
Response. Redirect ("~ /Providerdetails. aspx ");
}

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.