asp.net| Data
Implementation effect: Select a row in the main table and draw details of the row from the table.
Method 1: Code implementation.
Put a GridView on the page, a detailview. Data binding GridView and to set the primary key, and then write code in the SelectedIndexChanged event: When the selection changes, the DetailView also changes to the corresponding detail.
Specific 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 SQL statement returns a datasheet
// /</summary>
///<param name= sql > execution </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 property implementation
Put a GridView on the page, a detailview, and then each corresponds to a data source. As long as the selectedvalue of the GridView is used as a parameter in the SelectCommand of the DetailView data source, it can be implemented.
<SelectParameters>
<asp:controlparameter controlid= "Employeesgridview" name= "Addressid" propertyname= "SelectedValue"
Type= "Int32"/>
</SelectParameters>
Both methods are simple, Method 2 basically no code implementation, Method 1 control more flexible.