Asp.net2.0 a simple method of realizing master-slave data table

Source: Internet
Author: User
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.







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.