Access outer binding data in nested data controls

Last post 12-06-2009 3:01 AM by decker dong - msft. 6 replies.

Sort Posts:

  • Access outer binding data in nested data controls

    11-22-2009, 11:05 PM

    Hi

    Suppose I want to show Orders and the items in it in Northwind database. I use a FormView and inside it a GridView to show its line items. Is it possible to use data that is provided by FormView datasource in GridView with Eval, Bind or some other ways?

    Thanks

  • Re: Access outer binding data in nested data controls

    11-23-2009, 12:21 AM
    • All-Star
      25,062 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,569

    here's a sample (I'm assuming you're using a LinqDataSource - but even if not the same can be done with any other datasource)

    replace your DataContext in the ContextTypeName for the datasources.

            <asp:FormView ID="FormView1" runat="server" AllowPaging="True" 
                DataSourceID="LinqDataSource1">
                <ItemTemplate>
                    ProductName:
                    <asp:Label ID="ProductNameLabel" runat="server" 
                        Text='<%# Bind("ProductName") %>' />
                    <br />
                    ProductID:
                    <asp:Label ID="ProductIDLabel" runat="server" Text='<%# Bind("ProductID") %>' />
                    <br />
                    UnitsOnOrder:
                    <asp:Label ID="UnitsOnOrderLabel" runat="server" 
                        Text='<%# Bind("UnitsOnOrder") %>' />
                    <br />
                    Order_Details:
                    <asp:Label ID="Order_DetailsLabel" runat="server" 
                        Text='<%# Bind("Order_Details") %>' />
                    <br />
                    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("ProductID") %>' />
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                        DataSourceID="LinqDataSource2">
                        <Columns>
                            <asp:BoundField DataField="Quantity" HeaderText="Quantity" ReadOnly="True" 
                                SortExpression="Quantity" />
                            <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True" 
                                SortExpression="OrderID" />
                            <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" 
                                SortExpression="ProductID" />
                        </Columns>
                    </asp:GridView>
                    <asp:LinqDataSource ID="LinqDataSource2" runat="server" 
                        ContextTypeName="LinqConcepts.NWindTestDataContext" 
                        Select="new (Quantity, OrderID, ProductID, Product, Order)" 
                        TableName="Order_Details" Where="ProductID = @ProductID">
                        <WhereParameters>
                            <asp:ControlParameter ControlID="HiddenField1" Name="ProductID" 
                                PropertyName="Value" Type="Int32"/>
                        </WhereParameters>
                    </asp:LinqDataSource>
                </ItemTemplate>
            </asp:FormView>
            <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
                ContextTypeName="LinqConcepts.NWindTestDataContext" 
                Select="new (ProductName, ProductID, UnitsOnOrder, Order_Details)" 
                TableName="Products">
            </asp:LinqDataSource>
    

    also, I've taken products and the orders for each for an example.

    Regards,
    Peter
  • Re: Access outer binding data in nested data controls

    11-24-2009, 8:32 PM

    In fact, it seems that you'd like Master/Details with Gridview and FormView. Here's a demo suggesting you using GridView with DetailsView: http://msdn.microsoft.com/en-us/library/aa581795.aspx 

    I'll be very glad to be here to answer different kinds of questions though I'm new here. And please mark the answer as "Answered" --no matter who made the answer to you. Let's hand in hand to create a wonderful world of coding!


    Lanugate Conversion: http://www.developerfusion.com/tools/convert/csharp-to-vb/
  • Re: Access outer binding data in nested data controls

    12-03-2009, 11:02 PM

    Hi

    Sorry for being late.

    What I need is a way to bind data of FormView in the GridView(in the sample you provided). For example in the GridView I want to have a templated field and use '<%# Bind("UnitsOnOrder") %>'. Is any way to use data of outer binding?

    Thanks

  • Re: Access outer binding data in nested data controls

    12-04-2009, 12:54 AM
    Answer
    • All-Star
      25,062 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,569

    IranianCuriousBoy:
    a way to bind data of FormView in the GridView(in the sample you provided)

    I don't think that you could use it directly as both controls (FormView & GridView) would bind to their own datasources. what you could do is extract data from the FormView and show it in the GridView. Again, you need to be aware that since this is a master-details scenario the FormView data would be repeated IF there are multiple rows for the GridView for the respective FormView DataItem. Also, since the sources are different they could be in different physical tables in the database and you would have to write separate queries to update any changes from the GridView to the FormView table for the particular value that you want to show in the GridView (or use stored procedures etc)

    wire up the ItemCreated event of the FormView:

    <asp:FormView ID="FormView1" runat="server" AllowPaging="True"
                DataSourceID="LinqDataSource1" OnDataBound="FormView1_DataBound" OnItemCreated="FormView1_ItemCreated">

    and the RowDataBound of the GridView, also add the TemplateField to show the label:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                        DataSourceID="LinqDataSource2" OnRowDataBound="GridView1_RowDataBound">
                        <Columns>
                            <asp:BoundField DataField="Quantity" HeaderText="Quantity" ReadOnly="True"
                                SortExpression="Quantity" />
                            <asp:BoundField DataField="OrderID" HeaderText="OrderID" ReadOnly="True"
                                SortExpression="OrderID" />
                            <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True"
                                SortExpression="ProductID" />
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>

    here's the codebehind:

    string UnitsOnOrder = String.Empty;
            protected void Page_Load(object sender, EventArgs e)
            {
            }

            protected void FormView1_ItemCreated(object sender, EventArgs e)
            {
                if (FormView1.DataItem != null)
                {
                    UnitsOnOrder = DataBinder.Eval(FormView1.DataItem, "UnitsOnOrder").ToString();
                }
            }

            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label lbl = e.Row.FindControl("Label1") as Label;
                    if (lbl != null)
                    {
                        lbl.Text = UnitsOnOrder;
                    }
                }
            }

    if you work with VB.NET convert here: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Regards,
    Peter
  • Re: Access outer binding data in nested data controls

    12-06-2009, 2:17 AM

    Hi

    I tried to use the declarative approach and used the < %# DataBinder.Eval(FormView1.DataItem, "UnitsOnOrder") %>. The error message says It doesn't know about FormView1. I think the reason is that binding occurs in the current data item of the naming container and so FormView1 is unknown. If there is a way to reference FormView1 in the binding, It will be very helpful.

    Is'nt any way to do that?

    Regards

  • Re: Access outer binding data in nested data controls

    12-06-2009, 3:01 AM

    I think you should write something like:

    < %# DataBinder.Eval(Container.DataItem, "UnitsOnOrder") %>

    I'll be very glad to be here to answer different kinds of questions though I'm new here. And please mark the answer as "Answered" --no matter who made the answer to you. Let's hand in hand to create a wonderful world of coding!


    Lanugate Conversion: http://www.developerfusion.com/tools/convert/csharp-to-vb/
Page 1 of 1 (7 items)