Select a row in the main table and obtain the details of the row from the table.

Source: Internet
Author: User
Effect: select a row in the main table and obtain the details of the row from the table.

Method 1: Code .
Put a gridview and a detailview on the page. Bind the data to the gridview and set the primary key. Then write the code in the selectedindexchanged event: When the selection changes, the detailview is also changed to the corresponding detail.
Code:
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;


Public partial class masterdetail2: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
String SQL = "select * from [orders]";
Gridview1.datasource = binding (SQL );
Gridview1.datakeynames = new string [] {"orderid "};
Gridview1.databind ();
}
}

Protected void gridview1_selectedindexchanged (Object sender, eventargs E)
{
String orderid = convert. tostring (gridview1.selectedvalue );
String SQL = "select * from [orderdetails] Where [orderid] = '" + orderid + "'";
Detailsview1.datasource = binding (SQL );
Detailsview1.databind ();
}


/** // <Summary>
/// Execute an SQL statement to return a data table
/// </Summary>
/// <Param name = "SQL"> SQL statement to be executed </param>
/// <Returns> datatable </returns>
Protected datatable binding (string SQL)
{
Sqlconnection myconn = new sqlconnection (configurationmanager. connectionstrings ["appconnectionstring1"]. connectionstring );
Datatable dt = new datatable ();
Sqldataadapter myadapter = new sqldataadapter (SQL, myconn );
Myadapter. Fill (DT );
Return DT;

}
}
Method 2: Set Control Properties
Put a gridview, A detailview, and each corresponding data source on the page. You only need to use selectedvalue of the gridview as the parameter in the selectcommand of the detailview data source.
<Selectparameters>
<Asp: controlparameter controlid = "employeesgridview" name = "addressid" propertyname = "selectedvalue"
Type = "int32"/>
</Selectparameters>
Both methods are simple. method 2 basically has no code implementation, and method 1 is more flexible.


To be honest, please note the power from cool (www. aspcool. com ).

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.