如何在数组中绑定表的列并在按钮单击事件上逐个显示结果?

时间:2013-04-18 06:10:05

标签: c# asp.net sql-server ado.net arraylist

我创建了将Question的列值存储到数组中的方法,现在我想在按钮点击事件中逐个显示标签。

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 个答案:

答案 0 :(得分:0)

将列表保存到会话

初始化一个值为0的int,并将其存储在会话中。

单击按钮时

从会话中获取列表和int

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

将int递增1并在会话中重新保存。

答案 1 :(得分:0)

答案 2 :(得分: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;
}

答案 3 :(得分: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;
        }