【问题标题】:How to select and unselect the row in the GridView without having select button?如何在没有选择按钮的情况下选择和取消选择 GridView 中的行?
【发布时间】:2012-01-30 10:40:57
【问题描述】:

我正在开发一个 Master-Details GridView 和 DetailsView。一切正常,但我希望它删除 GridView 中每一行中的选择按钮,并使用户能够通过单击行中的任意位置或单击行中的任何单元格来选择行。除此之外,我希望在他选择行时显示每行的详细信息,并在他再次单击同一行时隐藏详细信息。 那么该怎么做呢?

ASP.NET 代码:

<asp:GridView ID="resultGrid" runat="server" DataKeyNames="QuestionID" SelectedIndex="0" 
                    AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="resultGrid_SelectedIndexChanged" Width="555px">
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" HorizontalAlign="Center" />
                        <Columns>
                            <asp:CommandField ShowSelectButton="true" />
                            <asp:BoundField DataField="QuestionID" HeaderText="Question" />
                            <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>

代码隐藏代码:

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



}

【问题讨论】:

  • 您可以使用点击事件来选择行和更改此事件中的行颜色等。
  • 你能把代码提供给我吗?

标签: c# asp.net


【解决方案1】:

阿里,这是一件很平常的事情,简单的谷歌搜索就会返回几个关于如何实现它的优秀教程。

基本上,您可以将以下代码添加到您的 RowDataBound 事件中:

    if (e.row.RowType == DataControlRowType.DataRow) { 
        e.row.Attributes["onmouseover"] =  
           "this.style.cursor='hand';"; 
        e.row.Attributes["onmouseout"] =  
           "this.style.textDecoration='none';"; 
        // Set the last parameter to True 
        // to register for event validation. 
        e.row.Attributes["onclick"] =  
         ClientScript.GetPostBackClientHyperlink(CustomerGridView,  
            "Select$" + row.RowIndex, true); 
    } 

完整指南在这里:http://msmvps.com/blogs/deborahk/archive/2010/01/25/asp-net-selecting-a-row-in-a-gridview.aspx

【讨论】:

  • 感谢您的帮助。我使用了您的代码,但出现以下错误,我不知道为什么:RegisterForEventValidation can only be called during Render();
  • 终于,我明白了。我需要做的只是在 aspx 页面的页面指令中添加 EnableEventValidation = "false"。
猜你喜欢
  • 2011-09-09
  • 2020-11-30
  • 2012-12-23
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多