【问题标题】:asp:GridView - change checkbox status when click anywhere in the lineasp:GridView - 单击行中的任意位置时更改复选框状态
【发布时间】:2012-01-23 11:16:24
【问题描述】:

我有一个 Web 应用程序并使用 gridview 来显示一些 SQL 数据。在 GV 中,我在第一列中有一个复选框。正如标题中所建议的,当用户单击该行中的任意位置时,我想选中该复选框。我怎样才能做到这一点。谢谢。

这是我的 GV;

<asp:GridView ID="myGV" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
            OnRowDataBound="myGV_OnRowDataBound">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="myCB" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />

当鼠标指针悬停在行上时,我使用此代码突出显示该行;

protected void myGV_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#D9ECFB'");
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
    }
}

编辑:

我在 GridView 中添加了“OnSelectedIndexChanging”和“OnSelectedIndexChanged”,然后尝试了这个(如 Ravi 所建议的那样),但无法让它工作。

protected void myGV_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
    GridViewRow row = myGV.Rows[e.NewSelectedIndex];

    CheckBox chk = (CheckBox)myGV.FindControl("cbIzpis");

    if (chk.Checked == true)
    {
        chk.Checked = false;
    }
    else if (chk.Checked == false)
    {
        chk.Checked = true;
    }
}

protected void myGV_SelectedIndexChanged(Object sender, EventArgs e)
{
    GridViewRow row = nyGV.SelectedRow;
}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您可以使用 GridView.SelectedRow 属性和 SelectedIndexChanged 事件,Gridview Selected Row

     void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
      {
    
        // Get the currently selected row using the SelectedRow property.
        GridViewRow row = CustomersGridView.SelectedRow;
        MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
    
      }
    
      void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
      {
    
        // SelectedIndexChanging event occurs before the select operation in the GridView control, the
        // SelectedRow property cannot be used. Instead, use the Rows collection
        // and the NewSelectedIndex property of the e argument passed to this 
        // event handler.
        GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];
        // here you can check the checkbox, by accessing it in template column using findControl method 
         CheckBox chk = (CheckBox) CustomersGridview.FindControl("chkboxID"); 
        chk.checked;    
    
    
        if (row.Cells[1].Text == "SomeCondition")
        {
    
          e.Cancel = true;
          MessageLabel.Text = "You cannot select " + row.Cells[2].Text + ".";
    
        }
    
      }
    

    【讨论】:

    • 它没有用。 :( 我用我试过的代码编辑了这个问题。你能检查一下你是否看到问题吗?
    • 没有错误。当我点击任何一行时,什么都没有发生。没有错误或任何东西
    • 您尝试在 Gridview 中设置 autogenerateselectbutton=true。正如我在这里检查的那样,它正在工作。
    • 抱歉,正忙于其他事情,无法尽快回复。没问题,即使我找不到让它工作的方法,它也只是一个额外的功能。 :) 感谢您的帮助,我有时间时会尝试实现它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多