【问题标题】:Determining a which checkbox is checked on a datagrid and updating DB确定在数据网格上选中了哪个复选框并更新数据库
【发布时间】:2011-11-12 11:27:00
【问题描述】:

我在这个字段中有一个带有 TemplateField 和复选框的数据网格。我将根据数据库中的 1 或 0 将这些复选框标记为选中或未选中。

<asp:TemplateField HeaderText="Normal User Logging">
        <ItemTemplate>
            <asp:CheckBox runat="server" ID="normalLogging" Checked='<%# Eval("normal_toggle") == 1 %>'
            AutoPostBack="true" />
        </ItemTemplate>
        </asp:TemplateField>

我将在这个数据网格中有多行。我想知道我将如何确定检查哪个复选框。比如,我怎么知道一个人点击了第三行的复选框?

【问题讨论】:

    标签: c# asp.net data-binding datagrid


    【解决方案1】:

    您使用 DataGridViewCheckBoxColumn 控件类型创建列,并使用 Click 事件和 CellContentClick,请参见下面的示例

        private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
            col.Name = "ColumnName";
            col.HeaderText = "HeaderTest";
            col.TrueValue = "True";
            col.FalseValue = "False";
            this.dataGridView1.Columns.Add(col);
            this.dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);
            this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
        }
    
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "ColumnName")
            {
                DataGridViewCheckBoxCell cell = this.dataGridView1.CurrentCell as DataGridViewCheckBoxCell;
                if (cell.Value == cell.TrueValue) 
                   //your code here            
            }
        }
    
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex >= 0 && this.dataGridView1.Columns[e.ColumnIndex].Name == "ColumnName")
            {
                DataGridViewCheckBoxCell cell = this.dataGridView1.CurrentCell as DataGridViewCheckBoxCell;
                if (cell.Value == cell.TrueValue) 
                 {
                    //your code here
                 }
            }
        }
    

    问候

    【讨论】:

      【解决方案2】:

      根据您所说的,执行回发的不是复选框,而是其他一些按钮,因此您可以一次检查整个选择。在这种情况下,复选框不应为 AutoPostBack="true"

      也就是说,您的 Button 代码应该是这样的:

      foreach (GridViewRow row in gv.Rows)
      {
          CheckBox cb = row.FindControl("cb") as CheckBox;
          if (cb != null)
          {
              if(cb.Checked)
              {
                  //Do your thing here
              }
          }
      }
      

      更新

      OP (Justin) 发布说他想为每次 CheckBox 点击更新数据库。在这种情况下,解决方案是处理 CheckBox 的OnCheckedChanged 事件:

      aspx代码:

      <asp:TemplateField HeaderText="Normal User Logging">
          <ItemTemplate>
              <asp:CheckBox runat="server" ID="normalLogging" 
                            Checked='<%# Eval("normal_toggle") == 1 %>'
                            AutoPostBack="true" 
                            OnCheckedChanged="cb_CheckedChanged" 
                            yourID='<%#Eval("yourIdField") %>'/>
          </ItemTemplate>
      </asp:TemplateField>
      

      C# 代码背后:

      protected void cb_CheckedChanged(object sender, EventArgs e)
      {
          Checkbox cb = sender as CheckBox;
          string yourID = cb.Attributes["yourID"];
          //Do your thing
      }
      

      【讨论】:

      • 不,只要选中复选框,我就会发回消息。我希望它更新数据库并将其反映给用户。
      • 好的,看起来不错。但是,我的问题仍然是如何确定选中了哪个框。所以在后面的代码中,我怎么知道它是第 4 行的复选框而不是第 2 行?我只是看到正在发送的复选框对象,但我没有看到确定它在哪一行的方法。
      • 哦,是的,你是对的。我更新了我的答案以包括在内。希望对您有所帮助!
      • 哦,让我知道结果如何。
      • 我现在正在处理这个问题,从我在输出中看到的内容来看,我在渲染的标记中得到了这个: ...checkbox input here.. . 所以我担心的是,当我的复选框回发时,它不会有我设置的这个自定义属性。浏览器甚至都不知道要发回它,因为它只知道是否选中了复选框。
      猜你喜欢
      • 2016-04-29
      • 2014-06-27
      • 2011-12-27
      • 2012-03-21
      • 1970-01-01
      • 2015-06-05
      • 2014-07-09
      • 2016-05-16
      • 1970-01-01
      相关资源
      最近更新 更多