Guest laplace82 Posted September 18, 2008 Share Posted September 18, 2008 Hi to all. I'm not able to use a DetailsView correlate to a Listbox: the ListBox shows values form a field of a table of a DB and when i select a value of the listbox the DetailsView should show all the fiels of that table record.my problem is that i don't know how to set the DataSource of the DetailsView that should be invoked every ListBox1_SelectedIndexChanged.I post my not working code. Option Strict OnImports System.Data.SqlClientImports System.DataPartial Class DettagliLibri Inherits System.Web.UI.Page Dim MioDataSet As DataSet Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim id As Integer = CInt(Request.QueryString("id")) If Not (Page.IsPostBack) Then ' Primo avvio dell'applicazione Dim stringaConnessioneDB As String = ConfigurationManager.AppSettings("pathConnessione1") Dim connessioneDB As SqlConnection = New SqlConnection(stringaConnessioneDB) MioDataSet = New DataSet() Dim DataTable_TitoliLibri As DataTable = New DataTable("TabellaTitoliLibri") Dim SqlDataAdapter_TabellaTitoliLibri As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM TitoliLibri WHERE id_autore=" & id, connessioneDB) Try ' Inserisco la DataTable nel DataSet SqlDataAdapter_TabellaTitoliLibri.Fill(MioDataSet, DataTable_TitoliLibri.TableName()) ' Riempo la Listbox con i valori della DataTable "TabellaAutori", ovvero con i valori della tabella ' "authors" del DB ListBox1.DataSource = MioDataSet.Tables("TabellaTitoliLibri") ListBox1.DataBind() Catch ex As Exception Throw New Exception(ex.Message) Finally connessioneDB.Close() connessioneDB.Dispose() End Try ' Inserisco il DataSet nell'oggetto Session in modo da tenerne memoria anche in caso di nuovi PageLoad Session("RicordaDataSet") = MioDataSet Else ' Postback della pagina MioDataSet = CType(Session("RicordaDataSet"), DataSet) End If End Sub Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged [b]' IN THIS WAY DOESN'T WORK[/b]DetailsView1.DataSource = MioDataSet.Tables("TabellaTitoliLibri").Rows(ListBox1.SelectedIndex) DetailsView1.DataBind() DetailsView1.Visible = True End SubEnd Class <%@ Page Language="VB" AutoEventWireup="false" CodeFile="DettagliLibri.aspx.vb" Inherits="DettagliLibri" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server" ><base target="_self" /> <title>Dettagli Libri</title></head><body> <form id="form1" runat="server"> <div> <asp:ListBox ID="ListBox1" runat="server" ToolTip="Scegli il libro di cui visualizzare i dettagli" onSelectedIndexChanged="ListBox1_SelectedIndexChanged" AutoPostBack="True" DataTextField="Titolo" DataValueField="id"> </asp:ListBox> <asp: DetailsView ID="DetailsView1" runat="server" DataKeyNames="id" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" Height="50px" Width="125px" Visible="False" AutoGenerateRows="False"> <FooterStyle BackColor="#CCCC99" /> <RowStyle BackColor="#F7F7DE" /> <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> <Fields> <asp:TemplateField> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Titolo") %>'></asp:TextBox> </EditItemTemplate> <HeaderTemplate> <asp:Label ID="TitoloLibroLabelHeader" runat="server" Text="Titolo Libro"></asp:Label> </HeaderTemplate> <InsertItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Titolo") %>'></asp:TextBox> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="TitoloLibroLabel" runat="server" Text='<%# Bind("Titolo") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Descrizione") %>'></asp:TextBox> </EditItemTemplate> <HeaderTemplate> <asp:Label ID="DescrizioneLibroLabelHeader" runat="server" Text="Descrizione Libro"></asp:Label> </HeaderTemplate> <InsertItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Descrizione") %>'></asp:TextBox> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="DescizioneLibroLabel" runat="server" Text='<%# Bind("Descrizione") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <EditItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Aggiorna"></asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Annulla"></asp:LinkButton> </EditItemTemplate> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Modifica"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Elimina"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <InsertItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Insert" Text="Inserisci"></asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Annulla"></asp:LinkButton> </InsertItemTemplate> <ItemTemplate> <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="New" Text="Nuovo"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Fields> <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> </asp: DetailsView> </div> </form></body></html> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.