代码改变世界

一次从GridView 获得多个指定行中多个指定控件的值

2009-01-20 07:33  闫妍  阅读(159)  评论(0编辑  收藏  举报

一次从GridView 获得多个指定行中多个指定控件的值,非常初级普通的代码,也没有什么技术难度。

 

.aspx

 <asp:GridView ID="GridAssisMater" runat="server" CssClass="gvtab" GridLines="None"
                                    AutoGenerateColumns
="False">
                                    
<PagerSettings Mode="NumericFirstLast" />
                                    
<Columns>
                                        
<asp:TemplateField HeaderText="序号">
                                            
<ItemTemplate>
                                                
<%#Container.DataItemIndex+1 %>
                                            
</ItemTemplate>
                                        
</asp:TemplateField>
                                        
<asp:BoundField DataField="M_MaterCoding" HeaderText="材料编码" />
                                        
<asp:BoundField DataField="M_Name" HeaderText="材料名称" />
                                        
<asp:BoundField DataField="M_Spec" HeaderText="规格型号" />
                                        
<asp:BoundField DataField="A_Quantity" HeaderText="订购数量" />
                                        
<asp:BoundField DataField="A_Price" HeaderText="单价" />
                                        
<asp:TemplateField HeaderText="入库数量">
                                            
<ItemTemplate>
                                                
<asp:TextBox ID="txt_Change" runat="server" CssClass="inputlineLong"></asp:TextBox>
                                            
</ItemTemplate>
                                        
</asp:TemplateField>
                                        
<asp:TemplateField HeaderText="&lt;input type='checkbox' id='chk' name='chk' onclick='checkJs(this.checked);'  /&gt;全选"
                                            FooterText
="全选">
                                            
<ItemTemplate>
                                                
<asp:CheckBox ID="checkboxname" ToolTip='<%# Eval( "CIDX")%>' runat="server" />
                                            
</ItemTemplate>
                                        
</asp:TemplateField>
                                    
</Columns>
                                    
<AlternatingRowStyle CssClass="tr2" />
                                
</asp:GridView>

 

.aspx.cs

以一个提交事件中,你可能需要的是获得当前页面下所有选中指定行的指定控件的值

 protected void Sub_Click(object sender, EventArgs e)

{
for (int i = 0; i < this.GridAssisMater.Rows.Count; i++)
                    {
                        ck 
= (CheckBox)this.GridAssisMater.Rows[i].FindControl("checkboxname");
                        
if (ck.Checked == true)
                        {
                            TextBox tbtemp 
= (TextBox)this.GridAssisMater.Rows[i].FindControl("txt_Change");

                            
using (SqlConnection cn = new SqlConnection(GetLastID.CnString)) //GetLastID.CnString 是自己写的用来获得Config中连接字符串的
                            {
                                cn.Open();
                                
using (SqlCommand cm = cn.CreateCommand())
                                {
                                    
                                    cm.CommandType 
= CommandType.Text;
                                    
//Response.Write("ckb[" + i + "]的值为:" + ckb[i] + "<br>");

                                    
string strSQL = " insert into dbo.SDS_AssistantInDetail(A_AIDID,A_AssistantInID,A_AuxiliaryDetailID,A_InQuantity) " +
                                                        
" values('" + Guid.NewGuid() + "','" + PKId.ToString() + "','" + ck.ToolTip + "','" + tbtemp.Text + "') ";
                                    cm.CommandText 
= strSQL;
                                    cm.ExecuteNonQuery();

                                }
                            }

                            
                        }
                    }
}