如何在数组中绑定表的列,并在按钮单击事件上逐个显示结果
本文关键字:事件 单击 按钮 结果 显示 数组 绑定 | 更新日期: 2023-09-27 18:09:51
我已经创建了将Question表中的Questions列值存储到数组中的方法,现在我想在一个标签中逐个显示按钮单击事件。
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;
}
试试这个....
<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;
}
Save list in to the session
初始化一个value = 0的int类型,并将其保存在会话中。
当按钮被点击
从会话
中获取列表和int用list[index]的值更新标签,其中index为整型。
似乎你正在寻找asp.net的gridview控件。
例如http://www.dotnetfunda.com/articles/article1594-how-to-populate-gridview-from-code-behind.aspx
// 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;
}