0

I have created method to store column value of Questions from Question table into array and now I wanted to display in a label one by one on button click event.

public ArrayList BindDataToArray()
{
    ArrayList list = new ArrayList();
    DataTable dt = new DataTable();
    con = new SqlConnection(str);
    cmd = new SqlCommand("select Question from  Questions", con);
    con.Open();
    adp = new SqlDataAdapter(cmd);
    adp.Fill(dt);

    foreach (DataRow dtrow in dt.Rows)
    {
        list.Add(dtrow);
    }
    return list;
}
4

4 回答 4

0
// in Aspx Page
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" 
            CommandArgument="1" /><asp:Label
           ID="Label1" runat="server" Text="Label"></asp:Label>
//in Aspx.cs

        protected void Button1_Click(object sender, EventArgs e)
        {
            ArrayList list = new ArrayList();
            list = BindDataToArray();
            int I = (Convert.ToInt32(Button1.CommandArgument.Count()));
            //int I = (Convert.ToInt32(Button1.CommandArgument));
            if (I < list.Count)
            {
                Label1.Text = list[I] as string; //Label in which u want to show message
                Button1.CommandArgument = (I + 1).ToString();
            }
            else
            {
                Label1.Text = "eND OF LIST";
                Button1.Enabled = false;
            }
        }
        public ArrayList BindDataToArray()
        {
            ArrayList list = new ArrayList();
            DataTable dt = new DataTable();
            //con = new SqlConnection(con);
            SqlCommand cmd = new SqlCommand("SELECT SurveyName FROM tblSurveyName", con);
            con.Open();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(dt);

            foreach (DataRow dtrow in dt.Rows) { list.Add(dtrow["SurveyName"].ToString()); }
            return list;
        }
于 2013-07-29T20:46:54.933 回答
0

将列表保存到会话中

用 value = 0 初始化一个 int 并将其也存储在会话中。

单击按钮时

从会话中获取列表和整数

使用 list[index] 值更新标签,其中 index 是 int。

将 int 加一并将其重新保存在会话中。

于 2013-04-18T06:25:20.533 回答
0

看来您正在寻找asp.net 的gridview控件。
示例http://www.dotnetfunda.com/articles/article1594-how-to-populate-gridview-from-code-behind.aspx

于 2013-04-18T06:25:59.337 回答
0

尝试这个....

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"  
            CommandArgument="0" />//dEFINATION OF BUTTON ON ASPX PAGE

protected void Button1_Click(object sender, EventArgs e)
{
     ArrayList list = new ArrayList();
     list=BindDataToArray();
     int I=( Convert.ToInt32(Button1.CommandArgument));
     if (I < list.Count)
     {
          Label1.Text = list[I] as string; //Label in which u want to show message
          Button1.CommandArgument = (I + 1).ToString();
     }
     else
     {
         Label1.Text = "eND OF LIST";
         Button1.Enabled = false;
     }
}

public ArrayList BindDataToArray()
{
    ArrayList list = new ArrayList();
    DataTable dt = new DataTable();
    con = new SqlConnection(str);
    cmd = new SqlCommand("select Question from  Questions", con);
    con.Open();
    adp = new SqlDataAdapter(cmd);
    adp.Fill(dt);

    foreach (DataRow dtrow in dt.Rows)
    {
        list.Add(dtrow["Question"].ToString());
    }
    return list;
}
于 2013-04-18T06:26:29.010 回答