如何在Master-Details GridView&中隐藏和显示DetailsView DetailsView? [英] How to hide and show the DetailsView in the Master-Details GridView & DetailsView?

查看:69
本文介绍了如何在Master-Details GridView&中隐藏和显示DetailsView DetailsView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循和利用有关Quiz Engine的ASP.NET网站中的解释示例.我在结果页面中有一个Master-Details,在该页面中,当用户在GridView中选择他回答的问题之一时,该问题的详细信息将显示在GridView下方的DetailsView中.一切正常.我现在要隐藏的是DetailsView(即已回答问题的详细信息),除非用户选择已回答的问题之一. **那怎么办?**

我将DetailsView的可见性设置为false,但是我不知道如何基于用户单击SELECT选项来隐藏/显示.

我的ASP.NET代码:

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]" OnSelected="SqlDataSource1_Selected">
                        <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:

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


        }

    }


}

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



我尝试使用以下方法解决问题,但失败了:



I tried to solve with using the following, but I failed:

protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        if (e.AffectedRows >= 1)
        {

            answerDetails.Visible = true;

        }
    }

推荐答案

ConnectionStrings:testConnectionString%> SelectCommand ="SELECT [问题],[Answer1],[Answer2],[Answer3],[QuestionID],[QuestionOrder],[Answer4],[CorrectAnswer],[AnswerExplanation],[QuizID]来自[问题],其中[ QuizID] = @QuizID)ORDER BY [QuestionOrder]"OnSelected =" SqlDataSource1_Selected> < SelectParameters> < asp:SessionParameter Name ="QuizID" SessionField ="QuizID" Type ="Int32"/> </SelectParameters> </asp:SqlDataSource> </td> </tr> < tr> < td> < asp:DetailsView ID ="answerDetails" runat =服务器" 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" Horizo​​ntalAlign ="Center"/> < HeaderStyle BackColor =#5D7B9D" Font-Bold ="True" ForeColor ="White"/> < EditRowStyle BackColor =#999999"/> < AlternatingRowStyle BackColor ="White" ForeColor =#284775"/> <字段> < asp:BoundField DataField ="Question" HeaderText ="Question" SortExpression =问题"/> < 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 =正确答案" SortExpression ="CorrectAnswer" HeaderStyle-BackColor ="lightgreen"/> < asp:BoundField DataField ="AnswerExplanation" HeaderText ="Explanation" SortExpression ="AnswerExplanation" HeaderStyle-BackColor ="lightgreen"/> </字段> </asp:DetailsView> </td> </tr>
ConnectionStrings:testConnectionString %>" SelectCommand="SELECT [Question], [Answer1], [Answer2], [Answer3], [QuestionID], [QuestionOrder], [Answer4], [CorrectAnswer], [AnswerExplanation], [QuizID] FROM [Question] WHERE ([QuizID] = @QuizID) ORDER BY [QuestionOrder]" OnSelected="SqlDataSource1_Selected"> <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:

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


        }

    }


}

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



我尝试使用以下方法解决问题,但失败了:



I tried to solve with using the following, but I failed:

protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        if (e.AffectedRows >= 1)
        {

            answerDetails.Visible = true;

        }
    }


最后,我可以解决它.解决方法如下:

Finally, I could be able to solve it. Here''s the solution:

public partial class Results : System.Web.UI.Page
{
    bool 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;
    }



}


这篇关于如何在Master-Details GridView&amp;中隐藏和显示DetailsView DetailsView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆