How-to link 2 Detailsview

Discussion in 'ASP .Net Web Controls' started by bruno, Jul 18, 2006.

  1. bruno

    bruno Guest

    Sorry for a very basic question. I'm new in ASP.NET programming .
    Is there a way to link 2 different DetailsView controls, each one bound to a
    differet SQLServer table. Can I get an example in VB.NET?

    If I use a DropdownList instead of the first DetailsView I can reference the
    current
    selected value as <asp:parameter .... PropertyName="SelectedValue" ...
    But what about DetailsView controls?

    --
    bruno
    bruno, Jul 18, 2006
    #1
    1. Advertising

  2. Hi Bruno,

    Welcome to the ASP.NET newsgroup.

    From your description, I understand you're wondering how to link two
    detailsView so as to make one detailsView use another DetailsView's current
    DataItem's value as input parameter, correct?

    As for this question, I think it is certainly doable, however, how we do it
    depends on the column/field you want reference in the source DetailsView(as
    parameter). Is that field/column in the DetailsView (you want to query as
    input parameter for another DetailsView) the primarykey column? If so, we
    can also use the "DetailsView.SelectedValue" property to reference the
    current selected Row's primarykey value (just like the way we do for
    DropDownList). For example, here is a simple page which contains a GridView
    and DetailsView, the GridView use DetailsView's current selected Row's Key
    value as input parameter(in DataSource);

    =====================
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT [CategoryID], [CategoryName] FROM
    [Categories]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [CategoryID]
    FROM [Products] WHERE ([CategoryID] = @CategoryID)">
    <SelectParameters>
    <asp:ControlParameter ControlID="DetailsView1"
    Name="CategoryID" PropertyName="SelectedValue"
    Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>

    <table >
    <tr>
    <td>
    <asp:DetailsView ID="DetailsView1" runat="server"
    AllowPaging="True" AutoGenerateRows="False" DataKeyNames="CategoryID"
    DataSourceID="SqlDataSource1"
    Height="50px" Width="125px">
    <Fields>
    <asp:BoundField DataField="CategoryID"
    HeaderText="CategoryID" InsertVisible="False"
    ReadOnly="True" SortExpression="CategoryID" />
    <asp:BoundField DataField="CategoryName"
    HeaderText="CategoryName" SortExpression="CategoryName" />
    </Fields>
    </asp:DetailsView>

    </td>
    <td>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    AutoGenerateColumns="False" DataKeyNames="ProductID"
    DataSourceID="SqlDataSource2">
    <Columns>
    <asp:BoundField DataField="ProductID"
    HeaderText="ProductID" InsertVisible="False"
    ReadOnly="True" SortExpression="ProductID" />
    <asp:BoundField DataField="ProductName"
    HeaderText="ProductName" SortExpression="ProductName" />
    <asp:BoundField DataField="UnitPrice"
    HeaderText="UnitPrice" SortExpression="UnitPrice" />
    <asp:BoundField DataField="CategoryID"
    HeaderText="CategoryID" SortExpression="CategoryID" />
    </Columns>
    </asp:GridView>
    </td>
    </tr>
    </table>
    ===============================

    If the column/field you want to reference is not the primary key column,
    the "SelectedValue" approach won't work. In such case, we need to use code
    and programmatically get the value from DetailsView and set it to another
    intermediate control(such as a Label control's Text property). Then, the
    other DetailsView(or other databound control) and reference this value (in
    DataSource) from this intermediate Label control. For example:

    In the below page I changed to let the Detailsview1 assign its "CategoryID"
    field(programmatically get from table cell) into a Label Control(lblData)
    and let the GridView1's DataSource reference the Label control as input
    parameter:

    ====================aspx=========
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
    ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT [CategoryID], [CategoryName] FROM
    [Categories]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [CategoryID]
    FROM [Products] WHERE ([CategoryID] = @CategoryID)">
    <SelectParameters>
    <asp:ControlParameter ControlID="lblData" Name="CategoryID"
    PropertyName="Text" Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>

    <table >
    <tr>
    <td>
    <asp:DetailsView ID="DetailsView1" runat="server"
    AllowPaging="True" AutoGenerateRows="False" DataKeyNames="CategoryID"
    DataSourceID="SqlDataSource1"
    Height="50px" Width="125px"
    OnPreRender="DetailsView1_PreRender" >
    <Fields>
    <asp:BoundField DataField="CategoryID"
    HeaderText="CategoryID" InsertVisible="False"
    ReadOnly="True" SortExpression="CategoryID" />
    <asp:BoundField DataField="CategoryName"
    HeaderText="CategoryName" SortExpression="CategoryName" />
    </Fields>
    </asp:DetailsView>

    <asp:Label ID="lblData" runat="server"
    Visible="False"></asp:Label></td>
    <td>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    AutoGenerateColumns="False" DataKeyNames="ProductID"
    DataSourceID="SqlDataSource2">
    <Columns>
    <asp:BoundField DataField="ProductID"
    HeaderText="ProductID" InsertVisible="False"
    ReadOnly="True" SortExpression="ProductID" />
    <asp:BoundField DataField="ProductName"
    HeaderText="ProductName" SortExpression="ProductName" />
    <asp:BoundField DataField="UnitPrice"
    HeaderText="UnitPrice" SortExpression="UnitPrice" />
    <asp:BoundField DataField="CategoryID"
    HeaderText="CategoryID" SortExpression="CategoryID" />
    </Columns>
    </asp:GridView>
    </td>
    </tr>
    </table>
    ==================================

    ========code behind==============
    protected void DetailsView1_PreRender(object sender, EventArgs e)
    {

    lblData.Text = DetailsView1.Rows[0].Cells[1].Text;


    Response.Write("<br/>lblData: " + lblData.Text);

    GridView1.DataBind();
    }
    ==================================


    If you have anything unclear on the above things or there is any other
    concerns, please feel free to let me know.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to

    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial

    response from the community or a Microsoft Support Engineer within 1
    business day is

    acceptable. Please note that each follow up response may take approximately
    2 business days

    as the support professional working with you may need further investigation
    to reach the

    most efficient resolution. The offering is not appropriate for situations
    that require

    urgent, real-time or phone-based interactions or complex project analysis
    and dump analysis

    issues. Issues of this nature are best handled working with a dedicated
    Microsoft Support

    Engineer by contacting Microsoft Customer Support Services (CSS) at

    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Jul 19, 2006
    #2
    1. Advertising

  3. bruno

    bruno Guest

    That help me.
    Thank you very much.
    Have a nice day!
    --
    bruno


    "Steven Cheng[MSFT]" wrote:

    > Hi Bruno,
    >
    > Welcome to the ASP.NET newsgroup.
    >
    > From your description, I understand you're wondering how to link two
    > detailsView so as to make one detailsView use another DetailsView's current
    > DataItem's value as input parameter, correct?
    >
    > As for this question, I think it is certainly doable, however, how we do it
    > depends on the column/field you want reference in the source DetailsView(as
    > parameter). Is that field/column in the DetailsView (you want to query as
    > input parameter for another DetailsView) the primarykey column? If so, we
    > can also use the "DetailsView.SelectedValue" property to reference the
    > current selected Row's primarykey value (just like the way we do for
    > DropDownList). For example, here is a simple page which contains a GridView
    > and DetailsView, the GridView use DetailsView's current selected Row's Key
    > value as input parameter(in DataSource);
    >
    > =====================
    > <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    > ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    > SelectCommand="SELECT [CategoryID], [CategoryName] FROM
    > [Categories]"></asp:SqlDataSource>
    > <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    > ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    > SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [CategoryID]
    > FROM [Products] WHERE ([CategoryID] = @CategoryID)">
    > <SelectParameters>
    > <asp:ControlParameter ControlID="DetailsView1"
    > Name="CategoryID" PropertyName="SelectedValue"
    > Type="Int32" />
    > </SelectParameters>
    > </asp:SqlDataSource>
    >
    > <table >
    > <tr>
    > <td>
    > <asp:DetailsView ID="DetailsView1" runat="server"
    > AllowPaging="True" AutoGenerateRows="False" DataKeyNames="CategoryID"
    > DataSourceID="SqlDataSource1"
    > Height="50px" Width="125px">
    > <Fields>
    > <asp:BoundField DataField="CategoryID"
    > HeaderText="CategoryID" InsertVisible="False"
    > ReadOnly="True" SortExpression="CategoryID" />
    > <asp:BoundField DataField="CategoryName"
    > HeaderText="CategoryName" SortExpression="CategoryName" />
    > </Fields>
    > </asp:DetailsView>
    >
    > </td>
    > <td>
    > <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    > AutoGenerateColumns="False" DataKeyNames="ProductID"
    > DataSourceID="SqlDataSource2">
    > <Columns>
    > <asp:BoundField DataField="ProductID"
    > HeaderText="ProductID" InsertVisible="False"
    > ReadOnly="True" SortExpression="ProductID" />
    > <asp:BoundField DataField="ProductName"
    > HeaderText="ProductName" SortExpression="ProductName" />
    > <asp:BoundField DataField="UnitPrice"
    > HeaderText="UnitPrice" SortExpression="UnitPrice" />
    > <asp:BoundField DataField="CategoryID"
    > HeaderText="CategoryID" SortExpression="CategoryID" />
    > </Columns>
    > </asp:GridView>
    > </td>
    > </tr>
    > </table>
    > ===============================
    >
    > If the column/field you want to reference is not the primary key column,
    > the "SelectedValue" approach won't work. In such case, we need to use code
    > and programmatically get the value from DetailsView and set it to another
    > intermediate control(such as a Label control's Text property). Then, the
    > other DetailsView(or other databound control) and reference this value (in
    > DataSource) from this intermediate Label control. For example:
    >
    > In the below page I changed to let the Detailsview1 assign its "CategoryID"
    > field(programmatically get from table cell) into a Label Control(lblData)
    > and let the GridView1's DataSource reference the Label control as input
    > parameter:
    >
    > ====================aspx=========
    > <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
    > ConnectionStrings:NorthwindConnectionString %>"
    > SelectCommand="SELECT [CategoryID], [CategoryName] FROM
    > [Categories]"></asp:SqlDataSource>
    > <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    > ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    > SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [CategoryID]
    > FROM [Products] WHERE ([CategoryID] = @CategoryID)">
    > <SelectParameters>
    > <asp:ControlParameter ControlID="lblData" Name="CategoryID"
    > PropertyName="Text" Type="Int32" />
    > </SelectParameters>
    > </asp:SqlDataSource>
    >
    > <table >
    > <tr>
    > <td>
    > <asp:DetailsView ID="DetailsView1" runat="server"
    > AllowPaging="True" AutoGenerateRows="False" DataKeyNames="CategoryID"
    > DataSourceID="SqlDataSource1"
    > Height="50px" Width="125px"
    > OnPreRender="DetailsView1_PreRender" >
    > <Fields>
    > <asp:BoundField DataField="CategoryID"
    > HeaderText="CategoryID" InsertVisible="False"
    > ReadOnly="True" SortExpression="CategoryID" />
    > <asp:BoundField DataField="CategoryName"
    > HeaderText="CategoryName" SortExpression="CategoryName" />
    > </Fields>
    > </asp:DetailsView>
    >
    > <asp:Label ID="lblData" runat="server"
    > Visible="False"></asp:Label></td>
    > <td>
    > <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    > AutoGenerateColumns="False" DataKeyNames="ProductID"
    > DataSourceID="SqlDataSource2">
    > <Columns>
    > <asp:BoundField DataField="ProductID"
    > HeaderText="ProductID" InsertVisible="False"
    > ReadOnly="True" SortExpression="ProductID" />
    > <asp:BoundField DataField="ProductName"
    > HeaderText="ProductName" SortExpression="ProductName" />
    > <asp:BoundField DataField="UnitPrice"
    > HeaderText="UnitPrice" SortExpression="UnitPrice" />
    > <asp:BoundField DataField="CategoryID"
    > HeaderText="CategoryID" SortExpression="CategoryID" />
    > </Columns>
    > </asp:GridView>
    > </td>
    > </tr>
    > </table>
    > ==================================
    >
    > ========code behind==============
    > protected void DetailsView1_PreRender(object sender, EventArgs e)
    > {
    >
    > lblData.Text = DetailsView1.Rows[0].Cells[1].Text;
    >
    >
    > Response.Write("<br/>lblData: " + lblData.Text);
    >
    > GridView1.DataBind();
    > }
    > ==================================
    >
    >
    > If you have anything unclear on the above things or there is any other
    > concerns, please feel free to let me know.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    >
    > ==================================================
    >
    > Get notification to my posts through email? Please refer to
    >
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    >
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial
    >
    > response from the community or a Microsoft Support Engineer within 1
    > business day is
    >
    > acceptable. Please note that each follow up response may take approximately
    > 2 business days
    >
    > as the support professional working with you may need further investigation
    > to reach the
    >
    > most efficient resolution. The offering is not appropriate for situations
    > that require
    >
    > urgent, real-time or phone-based interactions or complex project analysis
    > and dump analysis
    >
    > issues. Issues of this nature are best handled working with a dedicated
    > Microsoft Support
    >
    > Engineer by contacting Microsoft Customer Support Services (CSS) at
    >
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >
    > ==================================================
    >
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    >
    >
    bruno, Jul 23, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    798
    Kevin Spencer
    May 17, 2005
  2. wardemon
    Replies:
    0
    Views:
    473
    wardemon
    Dec 26, 2006
  3. VB Programmer

    Lost link between GridView and DetailsView

    VB Programmer, Oct 8, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    405
    VB Programmer
    Oct 9, 2007
  4. Feege
    Replies:
    0
    Views:
    432
    Feege
    Dec 20, 2005
  5. mt
    Replies:
    2
    Views:
    1,282
Loading...

Share This Page