VB.net DetailsView使用多个数据源?

时间:2012-01-05 18:45:37

标签: vb.net datasource detailsview

我有一个看起来非常完美的DetailsView。唯一的事情是,有一个字段实际上从另一个表中获取其信息。它是一个ID字段,链接到数据库中的另一个表,我希望有一些方法可以在DetailsView中获取该字段以从另一个表中提取信息。

有没有办法在PicklistID旁边添加一个+(加号),然后让它显示另一个查询的结果? (从PICKLIST中选择文本,其中PicklistID = @PicklistID ???)

更新2012年1月11日:表PICKLISTID中的PICKLIST不是唯一的或主键,因此我无法从SURVEY表中引用它。有没有办法给这两个列建立关系,这样我才能在SELECT中更改DetailsView语句以包含PICKLISTID

更新2012年1月23日:我仍然坚持这一点。我根本没有得到如何完成这项工作。帮助将不胜感激。我还是新人....

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="Label2" runat="server" Text="<h3>Manage Questions</h3>"></asp:Label>
<table style="width: 100%">
<tr>
<td align="right">
<asp:Label ID="Label1" runat="server" Text="Select Survey:"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
    DataSourceID="dsSurvey" DataTextField="SurveyName" DataValueField="SurveyID">
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
<asp:DetailsView ID="dvSurveyQuestions" runat="server" AllowPaging="True" 
 AutoGenerateRows="False" CellPadding="4" DataKeyNames="QuestionID" 
 DataSourceID="dsSurveyQuestions" ForeColor="#333333" GridLines="None" 
 Height="50px" Width="100%">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<Fields>
    <asp:BoundField DataField="QuestionID" HeaderText="QuestionID" 
     InsertVisible="False" ReadOnly="True" SortExpression="QuestionID" />
    <asp:TemplateField HeaderText="Question" SortExpression="Question">
        <EditItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" 
             Text='<%# Bind("Question") %>'></asp:TextBox>
        </EditItemTemplate>
        <InsertItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" 
             Text='<%# Bind("Question") %>'></asp:TextBox>
        </InsertItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblQuestion" runat="server" Text='<%# Bind("Question") %>'>
            </asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Answer Type" SortExpression="AnswerType">
        <EditItemTemplate>
            <asp:DropDownList ID="DropDownList2" runat="server" 
                SelectedValue='<%# Bind("AnswerType") %>'>
                <asp:ListItem Value="S">Single Choice (radio button)</asp:ListItem>
                <asp:ListItem Value="M">Multiple Choices (checkboxes)</asp:ListItem>
                <asp:ListItem Value="T">Text (textbox)</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
        <InsertItemTemplate>
            <asp:DropDownList ID="DropDownList3" runat="server" 
                SelectedValue='<%# Bind("AnswerType") %>'>
                <asp:ListItem Value="S">Single Choice (radio button)</asp:ListItem>
                <asp:ListItem Value="M">Multiple Choices (checkboxes)</asp:ListItem>
                <asp:ListItem Value="T">Text (textbox)</asp:ListItem>
            </asp:DropDownList>
        </InsertItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblAnswerType" runat="server" 
             Text='<%# Bind("AnswerType") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Question Type" SortExpression="QType">
        <EditItemTemplate>
            <asp:DropDownList ID="DropDownList4" runat="server" 
                SelectedValue='<%# Bind("QType") %>'>
                <asp:ListItem Value="Picklist">Picklist</asp:ListItem>
                <asp:ListItem Value="Text">Text</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
        <InsertItemTemplate>
            <asp:DropDownList ID="DropDownList5" runat="server" 
                SelectedValue='<%# Bind("QType") %>'>
                <asp:ListItem Value="Picklist">Picklist</asp:ListItem>
                <asp:ListItem Value="Text">Text</asp:ListItem>
            </asp:DropDownList>
        </InsertItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblQType" runat="server" Text='<%# Bind("QType") %>'>
            </asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Answer" SortExpression="TEXT">
        <EditItemTemplate>

        </EditItemTemplate>
        <InsertItemTemplate>

        </InsertItemTemplate>
        <ItemTemplate>
            <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("PicklistID") %>' />
            <asp:SqlDataSource ID="dsPicklist" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>" 
    SelectCommand="SELECT DISTINCT p.TEXT, p.PICKLISTID 
                   FROM PICKLIST p JOIN C_Survey_Questions c 
                   ON c.PicklistID = p.PICKLISTID 
                   AND c.SurveyID = @SurveyID 
                   ORDER BY p.TEXT">
                <SelectParameters>
                    <asp:ControlParameter ControlID="HiddenField1" Name="PicklistID" 
            PropertyName="SelectedValue" Type="String" />

                </SelectParameters>
            </asp:SqlDataSource>
            <asp:Label ID="lblTEXT" runat="server" Text=""></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:BoundField DataField="QuestionNum" HeaderText="Question Number" 
        SortExpression="QuestionNum" />
    <asp:BoundField DataField="Subsequence" HeaderText="Subsequence" 
        SortExpression="Subsequence" />
    <asp:CheckBoxField DataField="Active" HeaderText="Active" 
            SortExpression="Active" />

    <asp:BoundField DataField="Script" HeaderText="Script" 
        SortExpression="Script" />

    <asp:CheckBoxField DataField="Question_Locked" HeaderText="Question Locked" 
            SortExpression="Question_Locked" />

    <asp:BoundField DataField="QHelp" HeaderText="Question Help" 
        SortExpression="QHelp" />
    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
        ShowInsertButton="True" />
    </Fields>
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" Width="10%" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:DetailsView>
</td>
</tr>
</table>

<asp:SqlDataSource ID="dsSurvey" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>" 
    SelectCommand="SELECT [SurveyID], [SurveyName] 
                   FROM [C_Survey] 
                   ORDER BY [SurveyName]">
</asp:SqlDataSource>

<asp:SqlDataSource ID="dsSurveyQuestions" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>" 
    DeleteCommand="DELETE FROM [C_Survey_Questions] WHERE [QuestionID] = @QuestionID" 
    InsertCommand="INSERT INTO [C_Survey_Questions] ([SurveyID], [Question], [QType],
                   [PickListID], [QuestionNum], [Subsequence], [Active], [Script], 
                   [Question_Locked], [QHelp], [Createdate], 
                   [Modifydate], [AnswerType]) 
                   VALUES (@SurveyID, @Question, @QType, @PickListID, @QuestionNum, 
                   @Subsequence, @Active, @Script, @Question_Locked, @QHelp, 
                   getdate(), getdate(), @AnswerType)" 
    SelectCommand="SELECT * FROM [C_Survey_Questions] 
                   WHERE ([SurveyID] = @SurveyID)" 
    UpdateCommand="UPDATE [C_Survey_Questions] SET [SurveyID] = @SurveyID, 
                   [Question] = @Question, [QType] = @QType, 
                   [PickListID] = @PickListID, [QuestionNum] = @QuestionNum,
                   [Subsequence] = @Subsequence, [Active] = @Active, 
                   [Script] = @Script, [Question_Locked] = @Question_Locked, 
                   [QHelp] = @QHelp, [Modifydate] = getdate(), 
                   [AnswerType] = @AnswerType 
                   WHERE [QuestionID] = @QuestionID">
    <DeleteParameters>
        <asp:Parameter Name="QuestionID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="SurveyID" Type="Int32" />
        <asp:Parameter Name="Question" Type="String" />
        <asp:Parameter Name="QType" Type="String" />
        <asp:Parameter Name="PickListID" Type="String" />
        <asp:Parameter Name="QuestionNum" Type="Int32" />
        <asp:Parameter Name="Subsequence" Type="Int32" />
        <asp:Parameter Name="Active" Type="Boolean" />
        <asp:Parameter Name="Script" Type="String" />
        <asp:Parameter Name="Question_Locked" Type="Boolean" />
        <asp:Parameter Name="QHelp" Type="String" />
        <asp:Parameter Name="Createdate" Type="DateTime" />
        <asp:Parameter Name="Modifydate" Type="DateTime" />
        <asp:Parameter Name="AnswerType" Type="String" />
    </InsertParameters>
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" Name="SurveyID" 
            PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="SurveyID" Type="Int32" />
        <asp:Parameter Name="Question" Type="String" />
        <asp:Parameter Name="QType" Type="String" />
        <asp:Parameter Name="PickListID" Type="String" />
        <asp:Parameter Name="QuestionNum" Type="Int32" />
        <asp:Parameter Name="Subsequence" Type="Int32" />
        <asp:Parameter Name="Active" Type="Boolean" />
        <asp:Parameter Name="Script" Type="String" />
        <asp:Parameter Name="Question_Locked" Type="Boolean" />
        <asp:Parameter Name="QHelp" Type="String" />
        <asp:Parameter Name="Modifydate" Type="DateTime" />
        <asp:Parameter Name="AnswerType" Type="String" />
        <asp:Parameter Name="QuestionID" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>
</asp:Content>



Partial Class Admin_ManageQuestions
Inherits System.Web.UI.Page
Protected Sub dsPicklist_ItemSelecting(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles Me.Load
    Dim myControl1 As Control = FindControl("lblTEXT")
    If (Not myControl1 Is Nothing) Then
        myControl1.DataBind()
    Else
        'Control not found
    End If
End Sub
Protected Sub dvSurveyQuestions_ItemInserting(ByVal sender As Object, ByVal e As 
System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles 
dvSurveyQuestions.ItemInserting
    'The DetailsView does not include SurveyID column...we need to set this column 
     during INSERT operations because each question must belong to some survey.
    e.Values("SurveyID") = DropDownList1.SelectedValue
End Sub
Protected Sub dvSurveyQuestions_ItemUpdating(ByVal sender As Object, ByVal e As 
System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles 
dvSurveyQuestions.ItemUpdating
    'The DetailsView does not include SurveyID column...we need to set this column 
     during UPDATE operations because each question must belong to some survey.
    e.NewValues("SurveyID") = DropDownList1.SelectedValue
End Sub
Protected Sub dvSurveyQuestions_DataBound(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles dvSurveyQuestions.DataBound
    'The event handler checks the row count of the DetailsView control. 
     If it is zero then the mode of the DetailsView is changed to Insert using 
     ChangeMode() method.
    If dvSurveyQuestions.Rows.Count = 0 Then
        dvSurveyQuestions.ChangeMode(DetailsViewMode.Insert)
    End If
End Sub
End Class  

1 个答案:

答案 0 :(得分:1)

你可以这样做,但也许不是你想要的方式。

最简单的方法是在DetailsView上使用DataBinding事件。然后使用代码运行查询。

另一种方法是将您自己的控件创建为subclass of BoundField并覆盖OnDataBindField方法

最高效的方法是在DataView或其他构造中加入源中的数据。理想情况下,数据的连接应尽可能接近数据的实际来源(例如,在SQL查询或视图中),以避免不必要的跨网络跳转到数据库。对于单行来说,这不是一个大问题,但这通常是你应该做的。