简体   繁体   中英

How to hide and show the DetailsView in the Master-Details GridView & DetailsView?

I am trying to follow and utilize the explained example in ASP.NET website that is about Quiz Engine. I have a Master-Details in the result page where when the user selects one of his answered quesitons in the GridView, the details of that question will be displayed in the DetailsView underneath the GridView. Everything works fine. What I want now is just making the DetailsView (which is the details of the answered question) hidden unless the user selects one of the answered questions. So how to do that?

I set visibility of the DetailsView to false, but I don't know how to hide/show based on the user click on the SELECT option.

My ASP.NET code:

<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" Visible="false">

                        <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>

Code-Behind code:

public partial class Results : System.Web.UI.Page
{
    Object bShowDetailsView;
    protected void Page_Load(object sender, EventArgs e)
    {
       bShowDetailsView = false;

        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.";
            }

        }


    }


    protected void resultGrid_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataSource1.FilterExpression = "QuestionOrder=" + resultGrid.SelectedValue;
        bShowDetailsView = true;
        answerDetails.Visible = bShowDetailsView;
    }



}

为什么不默认选择resultGrid中的第一条记录,因为您处理的事件是SelectedIndexChanged而不是Selected / Unselected(我不知道这些是否存在)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM