2012-03-21 127 views
0

我有一個GridView生成如下:獲取選中的複選框的值成一個按鈕,從一個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" /> 

而且還具有上述按鈕單擊事件現在,當我點擊特定行復選框,我想,整個排在後面的代碼中的按鈕單擊事件中得到通電

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

你試圖在這裏實現什麼?你想爲多個複選框進行計算嗎? – 2012-03-21 14:14:11

回答

1

這是你想要的嗎?

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

(或應該計算立即發生在點擊複選框,比這將無法正常工作)

+1

單擊按鈕時不應計算 – 2012-03-21 14:40:14

1

假設這是你必須點擊做你的計算中唯一的按鈕,你可以做以下(請注意,我正在使用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 
        } 
       } 
      } 
     } 
    } 
0

使用下面的代碼:

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"]; 
     } 
    } 
} 
0
 foreach (GridViewRow r in GridView1.Rows) 
     { 
      if ((r.Cells[2].Controls.OfType<CheckBox>().ToList()[0]).Checked == true) 
      { 
       //your code. 
      } 
     } 
相關問題