How-to link 2 Detailsview

B

bruno

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

Steven Cheng[MSFT]

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

bruno

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


Steven Cheng said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top