0

请让我在这个问题上轻松一点。

我有一个名为 SurveyQuestions 的表,其中包含以下列

QuestionId, int;


Questions nvarchar(355);


AnswerType char(1);

AnswerType可以是M多项选择,T文本和S单选按钮选择等单一答案。

下面的代码旨在以这些格式呈现问题。但是,由于某种原因,当我运行代码时,只有图像显示,页面的其余部分为空白。

我不得不承认这不是我的代码,但从外观上看,它看起来并不复杂。

这是名为“调查”的页面的标记。对不起,长代码。

<form id="form1" runat="server">
<div>
    <table style="width: 100%">
        <tr>
            <td>
                <asp:HyperLink ID="HyperLink1" runat="server" ImageUrl="~/Images/logo.gif"></asp:HyperLink></td>
        </tr>
        <tr>
            <td>
                <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" Width="100%">
                    <ItemTemplate>
                        <table style="width: 100%">
                            <tr>
                                <td style="height: 21px">
                                    <asp:Label ID="Label3" runat="server" Font-Bold="True" Text='<%# Eval("QuestionID") %>'></asp:Label>
                                    <strong>)</strong>
                                    <asp:Label ID="Label2" runat="server" Font-Bold="True" Text='<%# Eval("Question") %>'></asp:Label></td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                                    </asp:RadioButtonList>
                                    <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                                    </asp:CheckBoxList>
                                    <asp:TextBox ID="TextBox1" runat="server" Columns="30" Font-Bold="False" Rows="5"
                                        TextMode="MultiLine"></asp:TextBox></td>
                            </tr>
                        </table>
                        <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("AnswerType") %>' />
                    </ItemTemplate>
                    <HeaderTemplate>
                        <asp:Label ID="Label4" runat="server" Font-Bold="True" Text="Please answer the following survey questions :"></asp:Label>
                    </HeaderTemplate>
                    <FooterTemplate>
                        <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
                    </FooterTemplate>
                </asp:DataList>
                <asp:Label ID="Label5" runat="server" EnableViewState="False" Font-Bold="True" ForeColor="Red"></asp:Label></td>
        </tr>
        <tr>
            <td align="center">
                <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Copyright (C) 2006. All rights reserved."></asp:Label></td>
        </tr>
    </table>
</div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
        SelectCommand="SELECT * FROM [SurveyQuestions] WHERE ([SurveyID] = @SurveyID)">
        <SelectParameters>
            <asp:QueryStringParameter Name="SurveyID" QueryStringField="id" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
</form>

然后后面的相关代码:

Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim anstype As HiddenField = e.Item.FindControl("HiddenField1")
        Dim questionid As Label = e.Item.FindControl("Label3")
        Dim rbl As RadioButtonList = e.Item.FindControl("RadioButtonList1")
        Dim cbl As CheckBoxList = e.Item.FindControl("CheckBoxList1")
        Dim txt As TextBox = e.Item.FindControl("TextBox1")
        Dim ds As DataSet = GetDataSet(questionid.Text)
        Select Case anstype.Value
            Case "S"
                rbl.Visible = True
                cbl.Visible = False
                txt.Visible = False
                rbl.DataSource = ds
                rbl.DataTextField = "Choice"
                rbl.DataValueField = "ChoiceID"
                rbl.DataBind()
            Case "M"
                rbl.Visible = False
                cbl.Visible = True
                txt.Visible = False
                cbl.DataSource = ds
                cbl.DataTextField = "Choice"
                cbl.DataValueField = "ChoiceID"
                cbl.DataBind()
            Case "T"
                rbl.Visible = False
                cbl.Visible = False
                txt.Visible = True
        End Select
    End If
End Sub

非常感谢提前

4

1 回答 1

0

这只是我,但我不会使用<asp:SqlDatasource />

在你后面的代码中Page_Load你应该这样做:

(pseudocode)

If Not IsPostBack
    DataList1.DataSource = dataSource
    DataList1.DataBind()
End

您的dataSource变量将是您运行 SQL 查询的结果。

于 2013-07-16T19:14:42.620 回答