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

📄 datagridmasterdetail.aspx

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

<Script Runat="Server">

Sub Page_Load
  If Not IsPostBack Then
    BindMasterGrid
  End If
End Sub

Sub BindMasterGrid
  Dim conNorthwind As SqlConnection
  Dim cmdSelect As SqlCommand

  conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Northwind" )
  cmdSelect = New SqlCommand( "Select * From Categories", conNorthwind )
  conNorthwind.Open()
  dgrdCategories.DataSource = cmdSelect.ExecuteReader()
  dgrdCategories.DataBind()
  conNorthwind.Close()
End Sub

Sub BindDetailGrid( intCatID As Integer )
  Dim conNorthwind As SqlConnection
  Dim strSelect As String
  Dim cmdSelect As SqlCommand

  conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;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()
End Sub

Sub dgrdCategories_ItemCommand( s As Object, e As DataGridCommandEventArgs )
  Dim intCatID As Integer

  intCatID = dgrdCategories.DataKeys( e.Item.ItemIndex )
  dgrdCategories.SelectedIndex = e.Item.ItemIndex
  BindDetailGrid( intCatID )
End Sub

</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='<%# 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 + -