In the previous article, we already knew how to associate the ControlParameter (control parameter) with the SelectedValue property of the GridView to implement a master-slave data table. The SelectedValue property returns the value of the first field specified by the DataKeyNames property. You can also specify multiple comma-separated field values for the DataKeyNames property. For example, you may need to pass multiple values to the data source from the table. These additional key field values are exposed through the SelectedDataKey property, which returns a DataKey object containing the name/value pair of the key field. ControlParameter can even reference these keys by setting the PropertyName property in the expression (for example, SelectedDataKey.Values("title_id")).
<asp:ControlParameter Name="FirstKey" ControlID="MasterGrid" PropertyName="SelectedDataKey.Values[0]"/>
<asp:ControlParameter Name="SecondKey" ControlID="MasterGrid" PropertyName="SelectedDataKey.Values[1]"/>
The following example demonstrates the code that enumerates the DataKeys collection and obtains the value of the key field from the SelectedDataKey of the GridView:
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("<b>SelectedDataKey.Value: </b>" & Server.HtmlEncode(GridView1.SelectedDataKey.Value) & "<br />")
Response.Write("<b>DataKey Field 1: </b>" & Server.HtmlEncode(GridView1.SelectedDataKey.Values("au_id")) & "<br />")
Response.Write("<b>DataKey Field 2: </b>" & Server.HtmlEncode(GridView1.SelectedDataKey.Values("title_id")) & "<br />")
End Sub
Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
Dim key As DataKey
Response.Write("<b>GridView DataKeys: </b><br />")
For Each key In GridView1.DataKeys
Response.Write(Server.HtmlEncode(key.Values(0)) & ", ")
Response.Write(Server.HtmlEncode(key.Values(1)) & "<br/>")
Next
End Sub
displays the data from the slave table in an independent control of the form, but sometimes we want the slave table control to be nested in the master table control and displayed as part of the master table. . In order to achieve this function, you must include the slave table control and related data sources into the template of the main table control, and have a data source parameter to get the value from the field of the main table data source. Since there is no declarative parameter object to associate with this approach, you must set the parameter value programmatically in code. When the data items of the main table control are bound, you can set the parameter value in an event handler of the main table control (such as the DataBound event of FormView). The following example demonstrates this technique.
<script runat="server">
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
OrderDetailsDataSource.SelectParameters("OrderID").DefaultValue = FormView1.DataItem("OrderID")
End Sub
</script>
<asp:FormView DataSourceID="OrdersDataSource"……>
<ItemTemplate>
<b>OrderID:</b>
<asp:Label ID="OrderIDLabel" runat="server" Text='<%# Eval("OrderID") %>'></asp:Label><br />
…
<asp:GridView DataSourceID="OrderDetailsDataSource" ……>
…
</asp:GridView>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:Northwind %>" ID="OrdersDataSource" runat="server" SelectCommand="SELECT [OrderID], [OrderDate], [ShipCity], [ShipCountry" ] FROM [Orders]">
</asp:SqlDataSource>
<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:Northwind %>" ID="OrderDetailsDataSource" runat="server" SelectCommand="SELECT [Order Details].OrderID, Products.ProductName, [Order Details].UnitPrice, [Order Details].Quantity FROM [Order Details] INNER JOIN Products ON [Order Details].ProductID = Products.ProductID WHERE [Order Details].OrderID = @OrderID">
<SelectParameters>
<asp:Parameter Name="OrderID" />
</SelectParameters>
</asp:SqlDataSource>
The following example demonstrates a similar technique, which uses a DataList and handles the ItemDataBound event of the DataList to set the data source parameter value.
<script runat="server">
Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
Dim s As SqlDataSource = e.Item.FindControl("OrderDetailsDataSource")
s.SelectParameters("OrderID").DefaultValue = e.Item.DataItem("OrderID")
End Sub
</script>