我正在尝试开发一个为用户提供简短测验的Web应用程序。我试图关注并利用ASP.NET网站中关于测验引擎的解释示例。我在结果页面中有一个Master-Details,当用户在GridView中选择一个已回答的问题时,该问题的详细信息将显示在GridView下面的DetailsView中。一切正常。
我现在想要的只是隐藏DetailsView(这是已回答问题的详细信息),除非用户选择了一个已回答的问题。那怎么做呢?
另外,如果我使用Ajax而不是PostBack来显示这个DetailsView,那将是非常好的。
我的ASP.NET代码:
<tr>
<td>
<asp:GridView ID="resultGrid" runat="server" DataKeyNames="QuestionID" SelectedIndex="0"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateSelectButton="True" OnSelectedIndexChanged="resultGrid_SelectedIndexChanged" Width="555px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="QuestionID" HeaderText="Question" />
<%--<asp:BoundField DataField="CorrectAnswer" HeaderText="Correct Answer" />--%>
<asp:BoundField DataField="UserAnswer" HeaderText="Your Answer" />
<asp:BoundField DataField="Result" HeaderText="Result" />
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" CssClass="boldtext" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [Question], [Answer1], [Answer2], [Answer3], [QuestionID], [QuestionOrder], [Answer4], [CorrectAnswer], [AnswerExplanation], [QuizID] FROM [Question] WHERE ([QuizID] = @QuizID) ORDER BY [QuestionOrder]">
<SelectParameters>
<asp:SessionParameter Name="QuizID" SessionField="QuizID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td>
<asp:DetailsView ID="answerDetails" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Height="45px" Width="552px" DataSourceID="SqlDataSource1"
AutoGenerateRows="False" DataKeyNames="QuestionID">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" CssClass="boldtext" Width="100px" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Fields>
<asp:BoundField DataField="Question" HeaderText="Question"
SortExpression="Question" />
<asp:BoundField DataField="Answer1" HeaderText="A"
SortExpression="Answer1" />
<asp:BoundField DataField="Answer2" HeaderText="B"
SortExpression="Answer2" />
<asp:BoundField DataField="Answer3" HeaderText="C"
SortExpression="Answer3" />
<asp:BoundField DataField="Answer4" HeaderText="D"
SortExpression="Answer4" />
<asp:BoundField DataField="CorrectAnswer" HeaderText="Correct Answer"
SortExpression="CorrectAnswer" HeaderStyle-BackColor="lightgreen" />
<asp:BoundField DataField="AnswerExplanation" HeaderText="Explanation"
SortExpression="AnswerExplanation" HeaderStyle-BackColor="lightgreen" />
</Fields>
</asp:DetailsView>
</td>
</tr>
修改
我所拥有的守则是:
protected void Page_Load(object sender, EventArgs e)
{
ArrayList al = (ArrayList)Session["AnswerList"];
if (al == null)
{
Response.Redirect("default.aspx");
}
resultGrid.DataSource = al;
resultGrid.DataBind();
// Save the results into the database.
if (IsPostBack == false)
{
// Calculate score
double questions = al.Count;
double correct = 0.0;
for (int i = 0; i < al.Count; i++)
{
Answer a = (Answer)al[i];
if (a.Result == Answer.ResultValue.Correct)
correct++;
}
double score = (correct / questions) * 100;
string username = HttpContext.Current.User.Identity.Name.ToString().Replace("ARAMCO\\", "");
SqlDataSource userQuizDataSource = new SqlDataSource();
userQuizDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ToString();
userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [Username]) VALUES (@QuizID, @DateTimeComplete, @Score, @Username)";
userQuizDataSource.InsertParameters.Add("QuizID", Session["QuizID"].ToString());
userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());
// "N4" is for displaying four decimal places, regardless of what the value is
userQuizDataSource.InsertParameters.Add("Score", score.ToString("N4"));
userQuizDataSource.InsertParameters.Add("Username", username);
int rowsAffected = userQuizDataSource.Insert();
if (rowsAffected == 0)
{
// Let's just notify that the insertion didn't
// work, but let' s continue on ...
errorLabel.Text = "There was a problem saving your quiz results into our database. Therefore, the results from this quiz will not be displayed on the list on the main menu.";
}
}
}
答案 0 :(得分:0)
我认为您需要更新SqlDataSource1以便从GridView中获取其参数值,这样除非用户在GridView中选择一行,否则detailsView将不会出现。试试这个。
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="
<%$ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [Question], [Answer1], [Answer2], [Answer3], [QuestionID],
[QuestionOrder], [Answer4], [CorrectAnswer], [AnswerExplanation], [QuizID] FROM
[Question] WHERE ([QuestionID] = @QuestionID) ORDER BY [QuestionOrder]">
<SelectParameters>
<asp:ControlParameter ControlID="resultGrid" Name="QuestionID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
我希望它能解决你的问题