asp:GridView-单击行中的任何位置时更改复选框状态
本文关键字:位置 状态 复选框 任何 GridView- 单击 asp | 更新日期: 2023-09-27 18:21:03
我有一个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"answers"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;
}
您可以使用GridView.SelectedRow属性,带有SelectedIndexChanged Event,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 + ".";
}
}