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

本文介绍了一种从ASP.NET GridView控件中获取多个指定行内特定控件值的方法,并演示了如何通过CheckBox选择行来批量更新数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

2009012007314515.jpg

 

.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' οnclick='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();

                                }
                            }

                            
                        }
                    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值