【问题标题】:Get checkbox value in grid view instead of using foreach loop在网格视图中获取复选框值而不是使用 foreach 循环
【发布时间】:2016-08-30 22:40:31
【问题描述】:

我正在使用 foreach 循环从网格视图中获取复选框的值,并通过在复选框 CheckedChanged 事件上获取当前用户 regID 来更新记录。

在 ASPX 中

   <Columns>
      <asp:TemplateField >
                            <ItemTemplate>
                                <asp:CheckBox ID="cbPaid" runat="server" AutoPostBack="true" OnCheckedChanged="cbPaid_CheckedChanged" Checked='<%# bool.Parse(Eval("status").ToString() == "Paid" ? "True": "False") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>

aspx.cs

protected void cbPaid_CheckedChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in GdParticipants.Rows)
    {
        CheckBox cbPaid = (CheckBox)row.FindControl("cbPaid");

        if (cbPaid!=null && cbPaid.Checked == true)
        {
            string status = "Paid";
            int amount = Convert.ToInt32(ViewState["EventFee"]);
            DateTime date = DateTime.Now;
            string user = Session["user_id"].ToString();
            string regID = row.Cells[2].Text;

            string type = "Deposit";
            string account = "Participation";
            int balance = BAL.fetch_availableBalance();
            int total_balance = amount + balance;
            string detail = "Participant: "+regID+" fee has been Recieved";
            BAL.updateParticipants(regID, status, amount, user, date);
            BAL.saveBudgetTracking(type,account,amount,0,total_balance,detail,date);

        }
    }
    Response.Redirect("~/show_members.aspx");
}

这种方法的问题是,每当新用户通过选中复选框更新新记录时,它将循环遍历整个记录并更新所有先前记录的当前更改。

请指导我如何从选中复选框的行和checkbox-CheckedChanged事件上的复选框值中仅获取当前的regID,而不是循环?

【问题讨论】:

    标签: c# asp.net checkbox datagridview


    【解决方案1】:

    发送者是 CheckBox 对象,它的第 2 级部分是 GridViewRow :

    protected void cbPaid_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cbPaid = sender as CheckBox;
    
        if (cbPaid != null && cbPaid.Parent != null)
        {
            GridViewRow row = cbPaid.Parent.Parent as GridViewRow;
    
            if (row != null )
            {
                string status = "Paid";
                int amount = Convert.ToInt32(ViewState["EventFee"]);
                DateTime date = DateTime.Now;
                string user = Session["user_id"].ToString();
                string regID = row.Cells[2].Text;
    
                string type = "Deposit";
                string account = "Participation";
                int balance = BAL.fetch_availableBalance();
                int total_balance = amount + balance;
                string detail = "Participant: " + regID + " fee has been Recieved";
                BAL.updateParticipants(regID, status, amount, user, date);
                BAL.saveBudgetTracking(type, account, amount, 0, total_balance, detail, date);
                Response.Redirect("~/show_members.aspx");
            }
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      使用下面的代码,它将避免通过网格循环。

      protected void cbPaid_CheckedChanged(object sender, EventArgs e)
      {
            int selRowIndex = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex;
            CheckBox cb = (CheckBox)GdParticipants.Rows[selRowIndex].FindControl("cbPaid");
            if (cb.Checked)
            {
                   //respective cell's value
                  string regID = GdParticipants.Rows[selRowIndex].Cells[2].Text;
      
                  //Perform your rest of the logic
             }
      }
      

      【讨论】:

      • 感谢您的回复。这段代码也有效,但我认为@Mathieu 的方法更易于理解...... :)
      【解决方案3】:

      您可以按父级搜索兄弟控件。

      我个人喜欢使用 Label 控件,以便我可以调用 FindControl,因为如果您对列重新排序,row.Cells[2].Text 将崩溃应用程序在运行时。

      <asp:GridView ID="GridView1" runat="server">
          <Columns>
              <asp:TemplateField>
                  <ItemTemplate>
                      <asp:Label runat="server"
                          ID="RegIDLabel"
                          Text='<%# Eval("RegID") %>' />
                  </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField>
                  <ItemTemplate>
                      <asp:CheckBox ID="cbPaid" runat="server"
                          AutoPostBack="true"
                          OnCheckedChanged="cbPaid_CheckedChanged"
                          Checked='<%# bool.Parse(Eval("status").ToString() == "Paid" ? "True": "False") %>' />
                  </ItemTemplate>
              </asp:TemplateField>
          </Columns>
      </asp:GridView>
      

      代码隐藏

      public partial class Default : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              if (!IsPostBack)
              {
                  GridView1.DataSource = new List<Item>
                  {
                      new Item {RegID = "1", Status = "Paid"},
                      new Item {RegID = "2", Status = "Other"},
                      new Item {RegID = "3", Status = "Paid"},
                  };
                  GridView1.DataBind();
              }
          }
      
          protected void cbPaid_CheckedChanged(object sender, EventArgs e)
          {
              var cbPaid = sender as CheckBox;
              var cell = cbPaid.Parent as DataControlFieldCell;
              var row = cell.Parent as GridViewRow;
              var regIDLabel = row.FindControl("RegIDLabel") as Label;
              var regId = regIDLabel.Text;
      
              if (cbPaid.Checked)
              {
                  // Do something
              }
          }
      }
      

      【讨论】:

      • 感谢您的回复。我是初学者,这种方法对我来说很难理解。但是谢谢:)
      猜你喜欢
      • 1970-01-01
      • 2020-12-09
      • 2011-03-02
      • 1970-01-01
      • 2015-01-29
      • 2018-08-08
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多