我点击按钮一个接一个地显示问题 [英] I have display questions one after the other by clicking button

查看:71
本文介绍了我点击按钮一个接一个地显示问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pageload中加载了问题1。点击按钮后,将显示问题2。现在我需要通过单击相同的按钮来显示问题3。但我无法做到。任何人都可以帮助我吗?



使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data;
使用System.Data.SqlClient;

public partial class Default6:System.Web.UI.Page
{
int i = 1;
SqlConnection con = new SqlConnection(Data Source = DS-70; Initial Catalog = model; Integrated Security = True;);

protected void Page_Load(object sender,EventArgs e)
{

SqlCommand cmd = new SqlCommand(select * from exam where qno = @ qno,con );

cmd.Parameters.AddWithValue(@ qno,i);
con.Open();

SqlDataReader sdr = cmd.ExecuteReader();

while(sdr.Read())
{
Label1.Text = sdr [0] .ToString()。Trim();
RadioButtonList1.Items [0] .Text = sdr [1] .ToString()。Trim();
RadioButtonList1.Items [1] .Text = sdr [2] .ToString()。Trim();
RadioButtonList1.Items [2] .Text = sdr [3] .ToString()。Trim();
RadioButtonList1.Items [3] .Text = sdr [4] .ToString()。Trim();
}
con.Close();



}
protected void Button1_Click(object sender,EventArgs e)
{
this.i = i + 1;

SqlCommand cmd = new SqlCommand(select * from exam where qno = @ qno,con);

cmd.Parameters.AddWithValue(@ qno,i);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();

while(sdr.Read())
{
Label1.Text = sdr [0] .ToString()。Trim();
RadioButtonList1.Items [0] .Text = sdr [1] .ToString()。Trim();
RadioButtonList1.Items [1] .Text = sdr [2] .ToString()。Trim();
RadioButtonList1.Items [2] .Text = sdr [3] .ToString()。Trim();
RadioButtonList1.Items [3] .Text = sdr [4] .ToString()。Trim();
}

con.Close();

}
}

解决方案

刚刚使用ViewState更新代码变量以保持状态问题索引。静态变量可能会导致问题,因此请尽量避免使用静态变量。



 public partial class Default6:System.Web.UI.Page 
{

int i = 1;

SqlConnection con = new SqlConnection(Data Source = DS-70; Initial Catalog = model; Integrated Security = True;);

protected void Page_Load(object sender,EventArgs e)
{

if(!IsPostBack)
{
ViewState [QuestionIndex ] ==我;
SqlCommand cmd = new SqlCommand(select * from exam where qno = @ qno,con);

cmd.Parameters.AddWithValue(@ qno,i);
con.Open();

SqlDataReader sdr = cmd.ExecuteReader();

while(sdr.Read())
{
Label1.Text = sdr [0] .ToString()。Trim();
RadioButtonList1.Items [0] .Text = sdr [1] .ToString()。Trim();
RadioButtonList1.Items [1] .Text = sdr [2] .ToString()。Trim();
RadioButtonList1.Items [2] .Text = sdr [3] .ToString()。Trim();
RadioButtonList1.Items [3] .Text = sdr [4] .ToString()。Trim();
}
con.Close();
}

}

protected void Button1_Click(object sender,EventArgs e)
{
this.i =(int)ViewState [ QuestionIndex];
this.i = i + 1;
ViewState [QuestionIndex] = this.i;

SqlCommand cmd = new SqlCommand(select * from exam where qno = @ qno,con);

cmd.Parameters.AddWithValue(@ qno,i);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();

while(sdr.Read())
{
Label1.Text = sdr [0] .ToString()。Trim();
RadioButtonList1.Items [0] .Text = sdr [1] .ToString()。Trim();
RadioButtonList1.Items [1] .Text = sdr [2] .ToString()。Trim();
RadioButtonList1.Items [2] .Text = sdr [3] .ToString()。Trim();
RadioButtonList1.Items [3] .Text = sdr [4] .ToString()。Trim();
}

con.Close();
}
}





[edit]增加索引以提高可读性[/编辑]


I have loaded the question 1 in pageload. After clicking the button question 2 will be displayed. Now I need to display the question 3 by clicking the same button. But I am unable to do it. Can any one help me ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default6 : System.Web.UI.Page
{
    int i = 1;
    SqlConnection con = new SqlConnection("Data Source=DS-70;Initial Catalog=model;Integrated Security=True;");
    
    protected void Page_Load(object sender, EventArgs e)
    {
        
            SqlCommand cmd = new SqlCommand("select * from exam where qno=@qno", con);

            cmd.Parameters.AddWithValue("@qno", i);
            con.Open();

            SqlDataReader sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {                
                Label1.Text = sdr[0].ToString().Trim();
                RadioButtonList1.Items[0].Text = sdr[1].ToString().Trim();
                RadioButtonList1.Items[1].Text = sdr[2].ToString().Trim();
                RadioButtonList1.Items[2].Text = sdr[3].ToString().Trim();
                RadioButtonList1.Items[3].Text = sdr[4].ToString().Trim();
            }
            con.Close();
        
        

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.i = i + 1;
                  
            SqlCommand cmd = new SqlCommand("select * from exam where qno=@qno", con);

            cmd.Parameters.AddWithValue("@qno", i);
            con.Open();
            SqlDataReader sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {
                Label1.Text = sdr[0].ToString().Trim();
                RadioButtonList1.Items[0].Text = sdr[1].ToString().Trim();
                RadioButtonList1.Items[1].Text = sdr[2].ToString().Trim();
                RadioButtonList1.Items[2].Text = sdr[3].ToString().Trim();
                RadioButtonList1.Items[3].Text = sdr[4].ToString().Trim();
            }

            con.Close();
        
    }
}

解决方案

Just updated your code with ViewState varaible to maintain the state of question index. Static variable can cause issue so try to avoid using static variables.

public partial class Default6 : System.Web.UI.Page
{

  int i = 1;

  SqlConnection con = new SqlConnection("Data Source=DS-70;Initial Catalog=model;Integrated Security=True;");

  protected void Page_Load(object sender, EventArgs e)
  {

    if(!IsPostBack)
    {
      ViewState["QuestionIndex"] ==i;
      SqlCommand cmd = new SqlCommand("select * from exam where qno=@qno", con);
 
      cmd.Parameters.AddWithValue("@qno", i);
      con.Open();
 
      SqlDataReader sdr = cmd.ExecuteReader();
 
      while (sdr.Read())
      {
        Label1.Text = sdr[0].ToString().Trim();
        RadioButtonList1.Items[0].Text = sdr[1].ToString().Trim();
        RadioButtonList1.Items[1].Text = sdr[2].ToString().Trim();
        RadioButtonList1.Items[2].Text = sdr[3].ToString().Trim();
        RadioButtonList1.Items[3].Text = sdr[4].ToString().Trim();
      }
      con.Close();
    }
 
  }

  protected void Button1_Click(object sender, EventArgs e)
  {
    this.i =(int) ViewState["QuestionIndex"]; 
    this.i = i + 1;
    ViewState["QuestionIndex"] = this.i;

    SqlCommand cmd = new SqlCommand("select * from exam where qno=@qno", con);
 
    cmd.Parameters.AddWithValue("@qno", i);
    con.Open();
    SqlDataReader sdr = cmd.ExecuteReader();
 
    while (sdr.Read())
    {
      Label1.Text = sdr[0].ToString().Trim();
      RadioButtonList1.Items[0].Text = sdr[1].ToString().Trim();
      RadioButtonList1.Items[1].Text = sdr[2].ToString().Trim();
      RadioButtonList1.Items[2].Text = sdr[3].ToString().Trim();
      RadioButtonList1.Items[3].Text = sdr[4].ToString().Trim();
    }
 
    con.Close();
  }
} 



[edit]indexation added to make more readable[/edit]


这篇关于我点击按钮一个接一个地显示问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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