Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

I am aiming for the following output in my datalist:

Question 1 Name
Answer 1
Answer 2
...
Answer 8
Question 2 Name
Answer 1
Answer 2
...
Answer 8
Question 3 Name
Answer 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

<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

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)

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,

like image 382
HGomez Avatar asked Nov 24 '25 14:11

HGomez


1 Answers

Basically, you can put a DataList inside the ItemTemplate of another DataList.

You link them using the DataKeyField property.

This tutorial walks you through it.

like image 106
DOK Avatar answered Nov 26 '25 03:11

DOK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!