在Gridview中实现多选

WEB页:

 <script type="text/javascript" language="javascript"> 

function doSelect() {
            var dom = document.all;
            var el = event.srcElement;
            if (el.id.indexOf("chkHeader") >= 0 && el.tagName == "INPUT" && el.type.toLowerCase() == "checkbox") {
                var ischecked = false;
                if (el.checked)
                    ischecked = true;
                for (i = 0; i < dom.length; i++) {
                    if (dom[i].type == undefined) continue;
                    if (dom[i].id.indexOf("chkSelect") >= 0 && dom[i].tagName == "INPUT" && dom[i].type.toLowerCase() == "checkbox")
                        dom[i].checked = ischecked;
                }
            }
        } 

</script>

<div id="divList" class="divList" style="overflow: auto">
        <asp:GridView ID="grvArea" runat="server" AutoGenerateColumns="False" EnableModelValidation="True"
            DataKeyNames="Pkid" Width="98%" CssClass="grid" OnRowDataBound="grvArea_RowDataBound">

            <Columns>
            <asp:BoundField DataField="" HeaderText="序号">
                        <HeaderStyle Width="3%" />
                        <ItemStyle Width="3%" />
                    </asp:BoundField>
              <asp:TemplateField >  
                <HeaderTemplate>  
                <asp:CheckBox ID="chkHeader" runat="server" onclick="doSelect();"/>  
                </HeaderTemplate>  
                <ItemTemplate>  
                <asp:CheckBox ID="chkSelect" runat="server" />  
               </ItemTemplate>  
               <ItemStyle HorizontalAlign="Center" />  
                </asp:TemplateField>  
                <asp:BoundField DataField="CustomName" HeaderText="客户名称" ItemStyle-Width="6%">
                </asp:BoundField>
                <asp:BoundField DataField="Phone" HeaderText="客户电话" ItemStyle-Width="6%">
                </asp:BoundField>
                 <asp:BoundField DataField="ContractId" HeaderText="金融单号" ItemStyle-Width="8%">
                </asp:BoundField>
                 <asp:BoundField DataField="LeftMoney" HeaderText="剩余本金" ItemStyle-Width="6%">
                </asp:BoundField>
                 <asp:BoundField DataField="qc" HeaderText="期次" ItemStyle-Width="6%">
                </asp:BoundField>                
                  <asp:BoundField DataField="PaymentTime" HeaderText="应付日期" DataFormatString="{0:yyyy-MM-dd}" ItemStyle-Width="10%">
                </asp:BoundField>
                  <asp:BoundField DataField="PaymentMoney" HeaderText="本金" ItemStyle-Width="8%">
                </asp:BoundField>
                
                <asp:BoundField DataField="lx" HeaderText="利息" ItemStyle-Width="6%">
                </asp:BoundField>
                <asp:BoundField DataField="PaymentTotal" HeaderText="本息合计" ItemStyle-Width="6%">
                </asp:BoundField>
                 <asp:BoundField DataField="LeftBehove" HeaderText="剩余金额" ItemStyle-Width="6%"  ItemStyle-Font-Size="Medium" ItemStyle-ForeColor="Red">
                </asp:BoundField>
                 <asp:BoundField DataField="FactBehove" HeaderText="实付金额" ItemStyle-Width="6%"  ItemStyle-Font-Size="Medium" ItemStyle-ForeColor="Red">
                </asp:BoundField>
                 <asp:BoundField DataField="LastTime" HeaderText="实付日期" DataFormatString="{0:yyyy-MM-dd}" ItemStyle-Width="6%">
                </asp:BoundField>

                 <asp:BoundField DataField="BankName" HeaderText="还款银行" ItemStyle-Width="6%">
                </asp:BoundField>
                 <asp:BoundField DataField="LeftFX" HeaderText="累计罚息" ItemStyle-Width="6%">
                </asp:BoundField>
                <asp:BoundField DataField="Remark" HeaderText="备注" ItemStyle-Width="10%">
                </asp:BoundField>
              
                <asp:BoundField DataField="PkId" HeaderText="标识">
                    <ItemStyle CssClass="hidden" />
                    <HeaderStyle CssClass="hidden" />
                </asp:BoundField>
                 
                <asp:BoundField DataField="state" HeaderText="状态">
                       <ItemStyle CssClass="hidden" />
                    <HeaderStyle CssClass="hidden" />
                </asp:BoundField>
                <asp:BoundField DataField="Mpkid" HeaderText="关键">
                      <ItemStyle CssClass="hidden" />
                    <HeaderStyle CssClass="hidden" />
                </asp:BoundField>
            </Columns>
            <HeaderStyle CssClass="grid_head" />
            <RowStyle CssClass="rowStyle" Wrap="False" />
            <AlternatingRowStyle CssClass="alterStyle" Wrap="False" />
            <FooterStyle CssClass="grid_foot" />
        </asp:GridView>
        <asp:HiddenField ID="hidPkId" runat="server"></asp:HiddenField>
        <asp:HiddenField ID="hdnState" runat="server"></asp:HiddenField>
         <asp:HiddenField ID="hdnMpkid" runat="server"></asp:HiddenField>
          <asp:HiddenField ID="hdnqc" runat="server"></asp:HiddenField>
    </div>

 

-------------------------------------------------------------------------

 

designer页:

  protected void btnPlan_Click(object sender, EventArgs e)
        {
            //string sdate=Sdate.Text.ToString();
            //string edate=Edate.Text.ToString();
            string strPkId = "";
            for (int i = 0; i < grvArea.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)(grvArea.Rows[i].Cells[1].FindControl("chkSelect"));
                if (chk.Checked == true)
                {
                    //string pkid = hidPkId.Value;
                    //GridView1.DataKeys[i].Value.ToString()
                    string pkid=grvArea.DataKeys[i].Value.ToString();
                   

                    strPkId += grvArea.DataKeys[i].Value.ToString() + ",";
                }
            }
            strPkId = strPkId.Substring(0, (strPkId.Length-1));
            if (!new SqlCustomOrder().companyToBankPay(strPkId))
            {
                ShowMessage("付款失败!");
                return;
            }
            else
            {
                ShowMessage("付款成功!");
                GridBind();
            }
            //RedirectPage("CustomPaymentBank.aspx?PkId=" + hidPkId.Value + "&Mpkid=" + hdnMpkid.Value + "&qc=" + hdnqc.Value);
        }
------------------------------------------

 #region  银行付款
        public bool companyToBankPay( string Pkid)
        {
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        string sql = "update CustomOrderPayment  set State=2,FactBJ=LeftBJ,FactLX=LeftLX,FactBehove=LeftBehove,LastTime=getDate() where PkId in(" + Pkid + ") ";

                        if (SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sql) <= 0) //失败抛出异常
                        {
                            throw new Exception("付款失败!");
                        }
                        SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sql);
                         
                        trans.Commit();
                        return true;
                    }
                    catch
                    {
                        trans.Rollback();
                        return false;
                    }
                }
            }
 
        }
        #endregion

posted @ 2016-09-23 10:36  会一  阅读(2228)  评论(0编辑  收藏  举报