asp.net 2.0 master Data sheet for Advanced data processing

Source: Internet
Author: User
Asp.net| Advanced | Data in the previous article, we already know how to associate ControlParameter (control parameters) with the SelectedValue property of the GridView to implement the Master-from datasheet. The SelectedValue property returns the value of the first field specified by the DataKeyNames property. You can also specify multiple field values that are comma-delimited for the DataKeyNames attribute, for example, you might want to pass multiple values to a data source from a table. These additional key field values are exposed through the SelectedDataKey property, which returns a DataKey object that contains the name/value pair (pair) of the key field. ControlParameter can even refer to these keys by setting the PropertyName property (for example, Selecteddatakey.values ("title_id") in an expression.

<asp:controlparameter name= "Firstkey" controlid= "Mastergrid" propertyname= "selecteddatakey.values[0" "/"
<asp:controlparameter name= "Secondkey" controlid= "Mastergrid" propertyname= "selecteddatakey.values[1" "/"
The following example illustrates the code that enumerates the DataKeys collection and gets 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
The preceding master-from example displays the data from the table in a separate control on the form, but sometimes we want the table control to be nested on the main Table control as part of the main table. To achieve this, you must include both the table control and the associated data source in the template for the primary table control, with a data source parameter that gets the value from the field in the primary table data source. Because there is no declarative parameter object to associate with this approach, you must set the parameter value by programming in code. When the data item for the primary table control is bound, you can set the parameter value in an event handling of the primary table control (for example, a FormView databound event). The following example illustrates 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 The [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 that uses DataList to handle DataList ItemDataBound events to set data source parameter values.

<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>

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.