TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Danielle Williams
NA
23
567
Radiobtn selection I make on question 1 is being duplicated
Mar 1 2018 10:08 AM
I am creating a quiz application in ASP.net (c#) and I currently have one question at a time displaying with the use of a
datapager
. However, If I chose radiobutton1 and then move on to question 2, It has radiobutton1 already selected, even though I am yet to answer that question. So for some reason the selection I make on question 1 is being duplicated to the other questions - I think it's obviously something to do with the setting and retreiving of the viewstates. Any help would be greatly appreciated!!
<body>
<form id=
"form1"
runat=
"server"
>
<div>
<asp:ListView ID=
"lvCustomers"
runat=
"server"
GroupPlaceholderID=
"groupPlaceHolder1"
ItemPlaceholderID=
"itemPlaceHolder1"
OnPagePropertiesChanging=
"OnPagePropertiesChanging"
OnPreRender=
"ListPager_PreRender"
>
<LayoutTemplate>
<div id=
"itemPlaceHolder1"
runat=
"server"
>
</div>
<asp:DataPager ID=
"DataPager1"
runat=
"server"
PagedControlID=
"lvCustomers"
PageSize=
"1"
>
<Fields>
<asp:NextPreviousPagerField runat=
"server"
ButtonType=
"Link"
ShowFirstPageButton=
"false"
ShowPreviousPageButton=
"true"
ShowNextPageButton=
"false"
/>
<asp:NumericPagerField ButtonType=
"Link"
/>
<asp:NextPreviousPagerField ButtonType=
"Link"
ShowNextPageButton=
"true"
ShowLastPageButton=
"false"
ShowPreviousPageButton=
"false"
/>
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<asp:Label ID=
"Label2"
runat=
"server"
Text=
'<%#Eval("QuestionID")%>'
></asp:Label><br />
<asp:Label ID=
"Label1"
runat=
"server"
Text=
'<%#Eval("QuestionText")%>'
></asp:Label><br />
<li>
<asp:RadioButton ID=
"Radio1"
Text=
'<%#Eval("Answer1") %>'
GroupName=
"radiobtns"
EnableViewState=
"true"
runat=
"server"
/>
</li>
<li>
<asp:RadioButton ID=
"Radio2"
runat=
"server"
GroupName=
"radiobtns"
EnableViewState=
"true"
Text=
'<%#Eval("Answer2") %>'
/>
</li>
<li>
<asp:RadioButton ID=
"Radio3"
runat=
"server"
GroupName=
"radiobtns"
EnableViewState=
"true"
Text=
'<%#Eval("Answer3") %>'
/>
</li>
<li>
<asp:RadioButton ID=
"Radio4"
runat=
"server"
GroupName=
"radiobtns"
EnableViewState=
"true"
Text=
'<%#Eval("Answer4") %>'
/>
</li>
<br />
</ItemTemplate>
</asp:ListView>
</div>
<p>
<asp:Button ID=
"Button1"
runat=
"server"
Text=
"Button"
OnClick=
"Button1_Click"
/>
</p>
</form>
</body>
</html>
namespace
WebApplication2.WebForms
{
public
partial
class
_1QuestionQuiz : System.Web.UI.Page
{
string
userAns;
int
QuestionID;
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
BindListView();
if
(Session[
"QuizID"
] !=
null
)
{
int
QuizID = Convert.ToInt32(Session[
"QuizID"
]);
}
}
}
protected
void
ListPager_PreRender(
object
sender, EventArgs e)
{
if
(Session[
"DTSource"
] !=
null
)
{
lvCustomers.DataSource = Session[
"DTSource"
];
lvCustomers.DataBind();
}
GetSelections();
}
private
void
BindListView()
{
SqlConnection conn =
new
SqlConnection();
string
connString = ConfigurationManager.ConnectionStrings[
"test1ConnectionString"
].ConnectionString;
SqlCommand Cmd =
new
SqlCommand();
conn.ConnectionString = connString;
conn.Open();
if
(lvCustomers !=
null
)
{
foreach
(ListViewItem item
in
lvCustomers.Items)
{
Label lbl = (Label)item.FindControl(
"Label2"
);
if
(lbl !=
null
)
{
QuestionID = Convert.ToInt32(lbl.Text);
ViewState[
"qstion"
] = QuestionID;
}
RadioButton rd1 = (RadioButton)item.FindControl(
"Radio1"
);
RadioButton rd2 = (RadioButton)item.FindControl(
"Radio2"
);
RadioButton rd3 = (RadioButton)item.FindControl(
"Radio3"
);
RadioButton rd4 = (RadioButton)item.FindControl(
"Radio4"
);
if
(rd1.Checked)
{
userAns = rd1.Text;
ViewState[
"Radio1"
] = rd1.Text;
}
else
if
(rd2.Checked)
{
userAns = rd2.Text;
ViewState[
"Radio2"
] = rd2.Text;
}
else
if
(rd3.Checked)
{
userAns = rd3.Text;
ViewState[
"Radio3"
] = rd3.Text;
}
else
if
(rd4.Checked)
{
userAns = rd4.Text;
ViewState[
"Radio4"
] = rd4.Text;
}
SqlCommand comm =
new
SqlCommand(
"InsertSelections"
, conn);
comm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter =
new
SqlDataAdapter(comm);
SqlParameter p1 =
new
SqlParameter(
"Answer"
, userAns);
SqlParameter p2 =
new
SqlParameter(
"QuestionID"
, QuestionID);
SqlParameter p3 =
new
SqlParameter(
"QuizID"
, (Session[
"QuizID"
]));
comm.Parameters.Add(p1);
comm.Parameters.Add(p2);
comm.Parameters.Add(p3);
comm.ExecuteNonQuery();
}
}
}
private
void
GetSelections()
{
foreach
(ListViewItem item
in
lvCustomers.Items)
{
Label lbl = (Label)item.FindControl(
"Label2"
);
if
(lbl !=
null
)
{
QuestionID = Convert.ToInt32(lbl.Text);
}
RadioButton rd1 = (RadioButton)item.FindControl(
"Radio1"
);
RadioButton rd2 = (RadioButton)item.FindControl(
"Radio2"
);
RadioButton rd3 = (RadioButton)item.FindControl(
"Radio3"
);
RadioButton rd4 = (RadioButton)item.FindControl(
"Radio4"
);
//Try radiobutton 1 as a tester
if
(rd1 !=
null
)
{
if
(lbl !=
null
&& ViewState[
"Radio1"
] !=
null
)
{
rd1.Checked =
true
;
}
}
if
(rd2 !=
null
)
{
if
(lbl !=
null
&& ViewState[
"Radio2"
] !=
null
)
{
rd2.Checked =
true
;
}
}
if
(rd3 !=
null
)
{
if
(lbl !=
null
&& ViewState[
"Radio3"
] !=
null
)
{
rd3.Checked =
true
;
}
}
if
(rd4 !=
null
)
{
if
(lbl !=
null
&& ViewState[
"Radio4"
] !=
null
)
{
rd4.Checked =
true
;
break
;
}
}
}
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
Response.Redirect(
"Score.aspx"
,
false
);
}
protected
void
OnPagePropertiesChanging(
object
sender, PagePropertiesChangingEventArgs e)
{
(lvCustomers.FindControl(
"DataPager1"
)
as
DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows,
false
);
BindListView();
}
}
}
Reply
Answers (
3
)
Package Manager Console not found in Tools menu in vs 2010
How to break a decimal value into integral and decimal part?