仅当用户从GridView中选择某些内容时,如何显示DetailView? [英] How to show the DetailView only when the user selects something from the GridView?

查看:53
本文介绍了仅当用户从GridView中选择某些内容时,如何显示DetailView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个向其用户提供简短测验的Web应用程序.我正在尝试遵循和利用有关Quiz Engine的ASP.NET网站中的解释示例.我现在正在同时显示GridView和DetailsView.


**除非用户选择已回答的问题之一,否则我现在要隐藏的是DetailsView(即已回答问题的详细信息).那么该怎么做呢?**



我的ASP.NET代码:

I am trying to develop a web application that provides its users with short quizzes. I am trying to follow and utilize the explained example in ASP.NET website that is about Quiz Engine. What I have now is showing the GridView and DetailsView simultaneously.


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



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

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





我拥有的代码背后是:





The Code-Behind that I have is:

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


            }

        }


    }

推荐答案

ConnectionStrings:testConnectionString%> SelectCommand ="SELECT [问题],[Answer1],[Answer2],[Answer3],[QuestionID],[QuestionOrder],[Answer4],[CorrectAnswer],[AnswerExplanation],[QuizID]来自[问题],其中[ 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 =服务器" 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" 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]"> <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>





我拥有的代码背后是:





The Code-Behind that I have is:

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


            }

        }


    }


这可以通过使用两个不同的数据源来实现,每个数据源分别用于gridview和detailsview.

单击网格视图的一行时,我们可以将行数据的ID传递给detailsview的select方法.

asp.net gridview和detailsview主详细信息实现的示例已在此处得到了很好的说明.
http://dotnetspidor.blogspot.com/2008/10/this- post-was-published-to-dotnetspidor.html [ ^ ]

看看它,您会找到解决方案...
This can be accomplished using two different datasources each for gridview and detailsview.

On click of a row of gridview we can pass the ID of the row data to the detailsview''s select method.

An example of asp.net gridview and detailsview master detail implementation has been well explained here....

http://dotnetspidor.blogspot.com/2008/10/this-post-was-published-to-dotnetspidor.html[^]

Have a look at it, you will find your solution...


这篇关于仅当用户从GridView中选择某些内容时,如何显示DetailView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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