Autogenerate gridview's next page when operation on first page is completed,on button click event

Posted by Mishti Choudhary under ASP.NET on 9/12/2014 | Points: 10 | Views : 611 | Status : [Member] | Replies : 2
I am using a gridview for generating question with its four options. Calculating the score by comparing the correct ans from the table. But this operation is successfully executing for indivisual pages,when moving to next page,operation of calculating score is taking place but only for the question on that particular page,its not calculating the ultimate score i.e summing up to the score previously obtained. Along with carrying and summing up the result of one page I want that on the same button click new page of the gridview autogenerates without using the paging property of the gridview. My Code So far: C# Code Behind:
 protected void btn_Click(object sender, EventArgs e)
{
string selans = "-1";


for (int i = 0; i < GridView1.Rows.Count; i++)
{

RadioButton r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");
RadioButton r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");
RadioButton r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");
RadioButton r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");
HiddenField hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");
if (r1.Checked)
{
selans = "1";

}
else if (r2.Checked)
{
selans = "2";

}
else if (r3.Checked)
{
selans = "3";

}
else if (r4.Checked)
{
selans = "4";


}
if (hdn.Value==selans)
{
score = score + 1;
com = new SqlCommand("update Exam set Score='"+score+"' where PCode='"+ Label14.Text+"'", con);
con.Open();
com.ExecuteNonQuery();
con.Close();


}
else
{
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
private void GetData()
{
str = ("select Branch_Code from Branches where Branch_Name='" + Label1.Text + "'");
con.Open();
com = new SqlCommand(str, con);
count = Convert.ToInt16(com.ExecuteScalar());
con.Close();
con.Open();
SqlDataAdapter da1 = new SqlDataAdapter("select Question,opt1,opt2,opt3,opt4,c_ans from Qbank where Branch_Code='" + count + "'", con);
DataSet ds = new DataSet();
da1.Fill(ds, "Qbank");
DataTable dt = ds.Tables[0];
Session["da"] = da1;
Session["dt"] = dt;
con.Close();
BindData();
}
private void BindData()
{
GridView1.DataSource = (DataTable)Session["dt"];
GridView1.DataBind();
}





Responses

Posted by: Naveenhcl on: 9/12/2014 [Member] Starter | Points: 25

Up
0
Down
Some what confused about your question can you please elaborate it in Microsoft terminology, as you said that's totally you are explained in your project scenario. you just elaborate it in MS technology for getting better answers.

Mishti Choudhary, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mishti Choudhary on: 9/13/2014 [Member] Starter | Points: 25

Up
0
Down
Am using the following code for calculation score of question paper containing multiple choice question. But am facing problem that,the score is correct when all the questions of the paper is answered. Suppose if one doesn't ans any of the question then its showing wrong score. Want that score is updated for each correct answer.
protected void RadioButton_CheckedChanged(object sender, EventArgs e)
{
string selans = "-1";
int Score = 0;
foreach (GridViewRow row in GridView1.Rows)
{
RadioButton r1 = ((RadioButton)row.FindControl("rad1"));
RadioButton r2 = ((RadioButton)row.FindControl("rad2"));
RadioButton r3 = ((RadioButton)row.FindControl("rad3"));
RadioButton r4 = ((RadioButton)row.FindControl("rad4"));
HiddenField hdn = ((HiddenField)row.FindControl("hf"));
if (r1.Checked)
{
selans = "1";
}
else if (r2.Checked)
{
selans = "2";
}
else if (r3.Checked)
{
selans = "3";
}
else if (r4.Checked)
{
selans = "4";
}
if (selans == hdn.Value)
{
Score = Score + 1;
com = new SqlCommand("update Exam set Score='" + Score + "' where PCode='" + Label14.Text + "'", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
}
}


Mishti Choudhary, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response