⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagridmasterdetail.aspx

📁 asp.net技术内幕的书配源码
💻 ASPX
字号:
<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat=server>
void Page_Load(Object sender, EventArgs e)
{ 
	BindMasterGrid();
}

void BindMasterGrid (){
	SqlConnection conNorthwind;
	SqlCommand cmdSelect;

	conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
	cmdSelect = new SqlCommand( "Select * From Categories", conNorthwind );
	conNorthwind.Open();
	dgrdCategories.DataSource = cmdSelect.ExecuteReader();
	dgrdCategories.DataBind();
	conNorthwind.Close();
}

void BindDetailGrid( int intCatID ) {
	SqlConnection conNorthwind;
	string  strSelect;
	SqlCommand cmdSelect;

	conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
	strSelect = "Select * From Products Where CategoryID=@catID";
	cmdSelect = new SqlCommand( strSelect, conNorthwind );
	cmdSelect.Parameters.Add( "@catID", intCatID );
	conNorthwind.Open();
	dgrdProducts.DataSource = cmdSelect.ExecuteReader();
	dgrdProducts.DataBind();
	conNorthwind.Close();
}

void dgrdCategories_ItemCommand( object s, DataGridCommandEventArgs e ) {
	int intCatID;

	intCatID = Convert.ToInt32(dgrdCategories.DataKeys[ e.Item.ItemIndex ]);
	dgrdCategories.SelectedIndex = e.Item.ItemIndex;
	BindDetailGrid( intCatID );
}

</Script>

<html>
<head><title>DataGridMasterDetail.aspx</title></head>
<body>
<form Runat="Server">

<asp:DataGrid
  ID="dgrdCategories"
  OnItemCommand="dgrdCategories_ItemCommand"
  DataKeyField="CategoryID"
  AutoGenerateColumns="False"
  SelectedItemStyle-BackColor="LightGreen"
  ShowHeader="False"
  Runat="Server">
<Columns>
  <asp:TemplateColumn>
  <ItemTemplate>
  <asp:LinkButton
    Text='<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>'
    Runat="Server"/>
  </ItemTemplate>
  </asp:TemplateColumn>
  <asp:BoundColumn DataField="Description" />
</Columns>
</asp:DataGrid>

<p>

<asp:DataGrid
  ID="dgrdProducts"
  HeaderStyle-BackColor="yellow"
  Runat="Server" />

</form>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -