简体   繁体   English

遍历SqlDataReader并将它们分配给页面(.aspx)控件

[英]Looping through SqlDataReader and assigning them to controls of the page (.aspx)

Since I am creating a blog slider, I want it to be dynamic so the latest blogs are retrieved from the database and shown on page. 由于我正在创建博客滑块,因此我希望它是动态的,以便从数据库中检索最新的博客并显示在页面上。 My issue is that I am able to retrieve 3 rows into reader() but unable to assign each row and its values to separate elements like header 'h3', 我的问题是我能够将3行检索到reader()中,但无法将每一行及其值分配给单独的元素,例如标头'h3',

while(dr.Read())
{
    string h1= (string)dr["Value1"]; 
    string p= (string)dr["Value2"]; 
}

but my main concern is that only one row will be run like this, after that how will I have the remaining rows assigned to controls such as 'h3' and 'p'. 但我主要担心的是,这样只会运行一行,然后我将如何将其余行分配给诸如“ h3”和“ p”之类的控件。 The first slider is getting the data from reader but what about the remaining two sliders and it's components such as 'h3' and 'p' 第一个滑块是从阅读器获取数据,但是其余两个滑块及其组件(例如“ h3”和“ p”)如何呢?

How do I assign the 2nd and 3rd rows to the other controls such as h3 and 'p' ? 如何将第二行和第三行分配给其他控件,例如h3和'p'?

Thanks in advance 提前致谢

create a bean class 创建一个bean类

public class headers
{
   public string head{get;set;}
   public string  p{get;set;}
}

//now change ur code //现在更改您的代码

 var objList = new List<headers>();
   while (dr.Read())
            {
                objDetails = new headers();
                objDetails .head = (string)dr["value1"];
                objDetails .p= (string)dr["value2"];
                objList.Add(objDetails);
            }

now u can do 现在你可以做

h1=objList[0].head;
h2=objList[1].head;
h3=objList[2].head;

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

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