【问题标题】:Find checkbox column in gridview OnCheckedChange event在gridview OnCheckedChange事件中查找复选框列
【发布时间】:2018-09-26 00:20:21
【问题描述】:

我有几个带有checkboxes control 的gridview 列有一个autopostback='true' 来更新数据库。

当我选中其中一个复选框时,整行都会更新,这让我很沮丧。

我正在尝试查找列的checkbox control不是整个gridview

我知道Mycheckbox.checked 总是正确的,因为它查看“所有的检查值”但是我希望它只查看指定的列

有没有可能做类似Checkbox MyCheckbox = findcontrol(*MyColumn*)......

protected void myCheckBox_OnCheckedChange(object sender, EventArgs e)
{    
    CheckBox myCheckBox = (CheckBox)sender;
    GridViewRow row = (GridViewRow)myCheckBox.NamingContainer;
    bool status = myCheckBox.Checked;   
    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        SqlCommand cmd = new SqlCommand("sp_tblPlanningChecked", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@PlanningID", row.Cells[1].Text);
        cmd.Parameters.AddWithValue("@ThisWeekMinus2", myCheckBox.Checked);
        cmd.Parameters.AddWithValue("@ThisWeekMinus1", myCheckBox.Checked);
        cmd.Parameters.AddWithValue("@ThisWeek", myCheckBox.Checked);
        cmd.Parameters.AddWithValue("@ThisWeekPlus1", myCheckBox.Checked);
        cmd.Parameters.AddWithValue("@ThisWeekPlus2", myCheckBox.Checked);
        cmd.Parameters.AddWithValue("@ThisWeekMinus3", myCheckBox.Checked);
        cmd.Parameters.AddWithValue("@ThisWeekMinus4", myCheckBox.Checked);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

}

【问题讨论】:

  • 我已经阅读了你的问题几次,但我仍然不明白你想要做什么。所以你有column1 并且想要得到所有CheckBoxes 是Checked == true
  • 这完全令人困惑。在您的图片中有 7 列,每列有 10 个 Checkboxes
  • 这到底是什么意思:iim trying to find out how i can use "findcontrol" of a column insted of using myCheckBox as the only checkbox sender?所以你想找到一个控件?请回答YesNo,以保持简单。此外,您不想使用每个 comboBoxs sender in its event as a converted ComboBox` 事件,那您还想使用什么?您的总体目标是什么?
  • 好吧,假设我们在Column column1。现在您想获取 column1 中所有已检查的CheckBoxes 吗? YesNo?
  • 但是现在你谈论一个(在这个例子中简化 one 这个)列。但是这一列有相当多的复选框。那么,当有不同的 CheckBox 带有已选中/未选中时,您如何想象得到真/假?

标签: c# asp.net gridview checkbox oncheckedchanged


【解决方案1】:

你能试试下面的代码吗

foreach (GridViewRow grow in GridView1.Rows)
{
    CheckBox chkStat = grow.FindControl("chkStatus") as CheckBox;
    int index = grow.RowIndex;
    if (chkStat.Checked)
    {
        //Write some code if checkbox is checked
    }
    else
    {
        //Write some code if checkbox is not checked
    }
}

【讨论】:

  • 我对您的代码进行了一些调整,但这一行: CheckBox chkStat = grow.FindControl("chkStatus") as CheckBox;是我追求的主线。
猜你喜欢
  • 2018-09-23
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多