简体   繁体   中英

Get a value of checked checkbox into a button from a Gridview

I have a Gridview Generated as follows :

<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.

(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.
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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