如何用于每个循环并显示存储在对象中的项目列表 [英] How to use for each loop and display the List of items in a stored in a object

查看:59
本文介绍了如何用于每个循环并显示存储在对象中的项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建在线测验.
我想从数据库中获取问题并将其显示在我的aspx页面中.
我需要使用每个循环来获取每个问题并显示它.
我将每个问题都存储在一个对象中.
如何显示它?
谢谢!

I am creating a online Quiz.
I want to get the Questions from the database and display it in my aspx page.
I need to use for each loop for getting each questions and displaying it.
I stored every question in a object.
How can i display it??
Thanks!

推荐答案

你好

我想你想做的是

你有

1-数据库
2-使用查询或存储过程获取数据的类
3形式显示问题.

使Class中的一个函数具有来自数据库的所有数据
使用类对象获取表单上的所有数据,并将其保存在一个List< class>中.

将数据源添加到要在其上显示数据的控件.


祝你好运..


您所做的操作无法进行迭代,因为foreach循环在可枚举类型上进行迭代.
并且您的对象将不会拥有所有数据.它将仅包含数据库中的最后一项.

因此,要获取所有数据,您将需要在类类型为List的对象中添加对象,然后可以在List上的每个循环中使用该对象.
Hello

i think u want to do is something like

u have

1 - database
2 - Class for fetching data using query or stored procedure
3 - form to display questions .

make One function in Class for having all data from database
get all that data on form using class object and save it in one List<class>

add datasource to the control on which you want to display you data .


good luck ..


what you did cannot iterate because foreach loop iterate on enumerable type .
and your object will not have all data .it will contain only last item from the database .

so for getting all data you will need to add object in a class type List after that you can use for each loop on the List


在为Question创建类时,我们应该将Questions存储在列表.
从问题列表中,我们可以使用foreach循环并逐个显示每个问题.
While creating class for the Question we should store the Questions in a list.
From the list of questions we can use foreach loop and display each question one by one..


{
       dt =HttpContext.Current.Session["Answered"] as DataTable;
       foreach(DataRow row in dt.Rows)
       {
           if(row["Id"].ToString()==Label2.Text)
           {
               row["Selected"]=Convert.ToInt32(RadioButtonList1.SelectedIndex)+1;
               HttpContext.Current.Session["Result"] = dt;
           }
       }
       dt = HttpContext.Current.Session["Result"] as DataTable;
       foreach (DataRow row in dt.Rows)
       {
           if (row["Selected"].ToString() == row["CorrectOption"].ToString())
           {
               marks = marks + 1;
           }
       }
       HttpContext.Current.Session["Marks"] = marks;
   }


这篇关于如何用于每个循环并显示存储在对象中的项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆