简体   繁体   English

从Gridview中获取一个已选中复选框的值

[英]Get a value of checked checkbox into a button from a Gridview

I have a Gridview Generated as follows : 我有一个Gridview生成如下:

<asp:GridView ID="Cash_GridView" runat="server" CssClass="Grid" >
<Columns>
<asp:TemplateField>
     <ItemTemplate>
        <asp:CheckBox ID="MemberCheck" runat="server" />
     </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Loan_Acno" HeaderText="Loan A/C number" />
</Columns>
</asp:Gridview>
<asp:Button ID="CashPayButton" runat="server" Text="Pay Dividend" CssClass="bluesome" OnClick="CashPayButton_Click" />

And also having the above button click event now when i click checkbox on particular row i want that whole row to be get caluclated in the Button click event in the code behind 并且当我点击特定行上的复选框时,现在还有上面的按钮点击事件我希望整个行在后面的代码中的按钮点击事件中得到计算

protected void CashPayButton_Click(object sender, EventArgs e)
 { }

Is this what you want? 这是你想要的吗?

protected void CashPayButton_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in Cash_GridView.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox c = (CheckBox)row.FindControl("MemberCheck");
            if (c.Checked)
            {
                //do calculation with other controls in the row
            }
        }
    }           
}

(or should the calculation happen immediately on clicking the checkbox, than this will not work) (或者如果单击复选框立即进行计算,那么这将不起作用)

Assuming that's the only button you will have to click to do your calculations, you can do the following (note that I'm using the Load event), it will be called when you click your button and when the postback occurs. 假设这是您必须单击进行计算的唯一按钮,您可以执行以下操作(请注意我正在使用Load事件),当您单击按钮并发生回发时将调用它。

(Calculation will happen when you click the button because of the postback) (由于回发而单击按钮时会发生计算)

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            ProcessRows();
        }
    }

    private  void ProcessRows()
    {
        foreach (GridViewRow oneRow in Cash_GridView.Rows)
        {
            CheckBox checkBoxControl = oneRow.FindControl("MemberCheck") as CheckBox;

            if (checkBoxControl != null && checkBoxControl.Checked)
            {
                // You have a row with a 'Checked' checkbox.

                // You can access other controls like I have accessed the checkbox
                // For example, If you have a textbox named "YourTextBox":
                TextBox textBoxSomething = oneRow.FindControl("YourTextBox") as TextBox;
                if (textBoxSomething != null)
                {
                    // Use the control value for whatever purpose you want.
                    // Example:
                    if (!string.IsNullOrWhiteSpace(textBoxSomething.Text))
                    {
                        int amount = 0;
                        int.TryParse(textBoxSomething.Text, out amount);

                        // Now you can use the amount for any calculation
                    }
                }
            }
        }
    }

Use the following code: 使用以下代码:

protected void CashPayButton_Click(object sender, EventArgs e)
{


    foreach(Gridviewrow gvr in Cash_GridView.Rows)
    {
       if(((CheckBox)gvr.findcontrol("MemberCheck")).Checked == true)
       {

          int uPrimaryid= gvr.cells["uPrimaryID"];
       }
    }
}
        foreach (GridViewRow r in GridView1.Rows)
        {
            if ((r.Cells[2].Controls.OfType<CheckBox>().ToList()[0]).Checked == true)
            {
               //your code.
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM