mardi 26 décembre 2017

display dynamic view in asp

i try to make an examining system so the admin up lode the question and the choices and the answers i wont to display in user side the question and the chooses in Multi View each view have a question and 3 Radio Button one for each choice in Radio Button List but i get only the first question in the first view when display the other view i get error (ActiveViewIndex is being set to '1'. It must be smaller than the current number of View controls '1'. For dynamically added views, make sure they are added before or in Page_PreInit event. Parameter name: value)

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            m1.ActiveViewIndex = 0;
        }
        if (m1.ActiveViewIndex==0)
        {
            Button2.Visible = false;
        }

    }


protected void Button1_Click(object sender, EventArgs e)
{

    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT * FROM exam WHERE exam_name='" + Request.QueryString["examid"] + "'";
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);

    foreach (DataRow dr in dt.Rows)
    {
        View view = new View();
        Label label = new Label();
        RadioButtonList list = new RadioButtonList();
        label.Text = dr["qustion"].ToString();
        list.Items.Add(new ListItem(dr["cha"].ToString(), "a"));
        list.Items.Add(new ListItem(dr["chb"].ToString(), "b"));
        list.Items.Add(new ListItem(dr["chc"].ToString(), "c"));
        view.Controls.Add(label);
        view.Controls.Add(list);
        m1.Views.Add(view);
    }
    m1.ActiveViewIndex = 1;
    Button2.Visible = true;
    Button1.Visible = false;

}

protected void Button2_Click(object sender, EventArgs e)
{
    if (m1.ActiveViewIndex < m1.Views.Count)
    {
        m1.ActiveViewIndex += 1;
    }

}




Aucun commentaire:

Enregistrer un commentaire