为什么不显示此页面上的问题?

时间:2013-07-16 18:52:38

标签: asp.net .net vb.net

关于这个问题,请放轻松。

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

QuestionId, int;


Questions nvarchar(355);


AnswerType char(1);

AnswerType可以M用于多项选择,T用于文本,S用于单个答案,例如单选按钮选项。

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

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

这是名为Survey的页面的标记。很抱歉长代码。

<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

提前多多感谢

1 个答案:

答案 0 :(得分:0)

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

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

(pseudocode)

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

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