简体   繁体   English

如何在ASP.NET datalist中放置一个列表

[英]How can I place a list inside ASP.NET datalist

I apologize if the answer to this seems obvious but I just cannot figure it out myself. 如果对此的答案看起来很明显,我很抱歉,但我自己也无法弄明白。 I am building a web application and have come to the stage where I need to get data from the database and display it in a datalist. 我正在构建一个Web应用程序,并且已经到了我需要从数据库获取数据并将其显示在数据列表中的阶段。

I am aiming for the following output in my datalist: 我的目标是在我的datalist中输出以下内容:

Question 1 Name 问题1名称
Answer 1 答案1
Answer 2 答案2
... ...
Answer 8 答案8
Question 2 Name 问题2名称
Answer 1 答案1
Answer 2 答案2
... ...
Answer 8 答案8
Question 3 Name 问题3名称
Answer 1 答案1
.... ....

You get the idea. 你明白了。 I have written out a basic datalist starting with the questions (code below) which works to some extent,however I am not quite sure how to approach getting the answers out and then repeating the process until all the data has been fetched. 我已经写了一个基本的数据列表,从问题(下面的代码)开始,这在某种程度上起作用,但是我不太确定如何解决问题,然后重复这个过程直到获取所有数据。 Here is the code. 这是代码。 Confirm.aspx Confirm.aspx

<asp:DataList runat="server" id="dgQuestionnaire">
                    <ItemTemplate>
                        <asp:Label ID="Name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "QuestionText") %>' />
                    </ItemTemplate>
                </asp:DataList>

Confirm.aspx.cs Confirm.aspx.cs

public partial class Confirm_Questionnaire : System.Web.UI.Page
    {
        OsqarSQL GetData;
        DataTable DT;

        protected void Page_Load(object sender, EventArgs e)
        {
            GetData = new OsqarSQL();
            int questionnaireId = (int)Session["QuestionnaireID"];
            string questionnaireName = (string)Session["QuestionnaireName"];
            ReturnQnrName.Text = questionnaireName + "   (ID: " + questionnaireId.ToString() + ")";
            int questionId = GetData.GetQuestionID(questionnaireId);
            DT = GetData.GetQuestionName(questionnaireId);
            dgQuestionnaire.DataSource = DT;
            dgQuestionnaire.DataBind();

        } // End Page_Load

    } // Emd Class Confirm_Questionnaire

Questionnaire.cs (App_Code) Questionnaire.cs(App_Code)

public class OsqarSQL
    {
        private string _productConnectionString;
        private SqlConnection _productConn;

        public OsqarSQL()
        {
            _productConn = new SqlConnection();
            _productConnectionString += "data source=mssql.database.co.uk; Initial Catalog=devworks_oscar;User ID=me;Password=you";
            _productConn.ConnectionString = _productConnectionString;
        }
        public DataTable GetQuestionName(int QuestionnaireID)
        {
            string returnValue = string.Empty;
            SqlCommand myCommand = new SqlCommand("GetQuestion", _productConn);
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.Add(new SqlParameter("@QUEST_ID", SqlDbType.Int));
            myCommand.Parameters[0].Value = QuestionnaireID;
            return createDataTable(getData(myCommand));
        }
        public DataTable GetAnswerTitle(int QuestionnaireID)
        {
            string returnValue = string.Empty;
            SqlCommand myCommand = new SqlCommand("GetAnswer", _productConn);
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.Add(new SqlParameter("@QUEST_ID", SqlDbType.Int));
            myCommand.Parameters[0].Value = QuestionnaireID;
            return createDataTable(getData(myCommand));
        }

I know i am missing something but I am not sure what to include in order to display the answers with each question. 我知道我错过了一些东西,但我不知道要包括什么以显示每个问题的答案。

Thanks Idny, 谢谢Idny,

Basically, you can put a DataList inside the ItemTemplate of another DataList . 基本上,您可以将DataList放在另一个DataListItemTemplate中。

You link them using the DataKeyField property. 您使用DataKeyField属性链接它们。

This tutorial walks you through it. 教程将指导您完成它。

I am not sure if I understand your question correctly. 我不确定我是否正确理解你的问题。 I guess what you are trying to do is get all the questions from the DB and then display all answers corresponding to a question below that question. 我想你要做的是从数据库中获取所有问题,然后显示与该问题下面的问题相对应的所有答案。

You can try to use a repeater. 您可以尝试使用转发器。 Basically there would be two repeaters, one inside the another. 基本上会有两个中继器,一个在另一个中。 The outer repeater would display questions from the datasource and the inner one would display answers for that particular question. 外部中继器将显示来自数据源的问题,内部中继器将显示该特定问题的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM