将复选框的值从网格视图中获取到按钮中
本文关键字:获取 按钮 视图 网格 复选框 | 更新日期: 2023-09-27 18:28:26
我有一个网格视图生成如下:
<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" />
现在也有了上面的按钮点击事件,当我点击特定行上的复选框时,我希望整行都被计算在后面代码中的按钮点击活动中
protected void CashPayButton_Click(object sender, EventArgs e)
{ }
这就是您想要的吗?
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
}
}
}
}
(或者如果单击复选框后立即进行计算,则这将不起作用)
假设这是您进行计算时必须单击的唯一按钮,您可以执行以下操作(请注意,我使用的是Load事件),当您单击按钮和发生回发时,它将被调用。
(由于回发,当您单击按钮时将进行计算)
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
}
}
}
}
}
使用以下代码:
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.
}
}