简单的AspGridView 的添加、删除、保存操作

来源:互联网 发布:奉化淘宝司法拍卖网 编辑:程序博客网 时间:2024/04/30 22:25

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ColPaymentInfo.aspx.cs" Inherits="webpage_ColPaymentInfo" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v7.2.Export, Version=7.2.5.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1"
    Namespace="DevExpress.Web.ASPxGridView.Export" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v7.2, Version=7.2.5.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1" Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dxwgv" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v7.2, Version=7.2.5.0, Culture=neutral, PublicKeyToken=9b171c9fd64da1d1" Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Src="../ascx/SiteMap.ascx" TagName="SiteMap" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>代收代付客户信息</title>
    <link href="../css/Main.css" type="text/css"   rel="stylesheet"/>
    <link href="../css/style.css" type="text/css" rel="Stylesheet"/> 
    <script src="../js/ShowSearchMain.js" language="javascript" type="text/javascript"></script> 
</head>
<body style="margin:0px 0px 0px 5px">
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <uc1:SiteMap ID="SiteMap1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>
              <table  width="800" cellpadding="0" cellspacing="0" border="0" style="line-height:25px">
                   <tr>
                        <td style="text-align:right">公司名称:</td>
                        <td colspan="3">
                            <asp:TextBox ID="tbCompanyName" runat="server" Width="410px"></asp:TextBox></td>
                        <td style="text-align:right">公司简称:</td>
                        <td>
                            <asp:TextBox ID="tbAbbName" runat="server"></asp:TextBox></td>
                   </tr>
                     <tr>
                        <td style="text-align:right">公司地址:</td>
                        <td colspan="3">
                            <asp:TextBox ID="tbCompAddr" runat="server" Width="410px"></asp:TextBox></td>
                        <td style="text-align:right">公司性质:</td>
                        <td>
                            <asp:TextBox ID="tbCompNature" runat="server"></asp:TextBox></td>
                       </tr>
                     <tr>
                        <td style="text-align:right">联系人:</td>
                        <td>
                            <asp:TextBox ID="tbContactPerson" runat="server"></asp:TextBox></td>
                        <td style="text-align:right">联系电话:</td>
                        <td>
                            <asp:TextBox ID="tbTel" runat="server"></asp:TextBox></td>
                        <td style="text-align:right">传真:</td>
                        <td>
                            <asp:TextBox ID="tbFax" runat="server"></asp:TextBox></td>
                   </tr>
                   <tr>
                        <td style="text-align:right">信用期限:</td>
                        <td>
                            <asp:TextBox ID="tbExpireDate" runat="server" style="width:100px"></asp:TextBox>
                            <button  class="date_button" onclick="Drop('tbExpireDate')" type="button" style="width:20px"><img src="../images/date.gif" /></button>
                            </td>
                        <td style="text-align:right">信用额度:</td>
                        <td>
                            <asp:TextBox ID="tbCreditLine" runat="server"></asp:TextBox></td>
                        <td></td>
                        <td></td>
                   </tr>
                  <tr style="padding-top:20px">
                      <td colspan="6" style="text-align:center; height: 41px;">
                        <asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />&nbsp;
                        <asp:Button ID="btnEdit" runat="server" Text="修改" OnClick="btnEdit_Click" />&nbsp;
                        <asp:Button ID="btnSave" runat="server" Text="保存" OnClick="btnSave_Click" />&nbsp;
                        <asp:Button ID="btnDel" runat="server" Text="删除" OnClick="btnDel_Click" />
                       </td>
                  </tr>
               </table>
               <br />
               <dxwgv:ASPxGridView
               ID="ASPxGridView1"
               Width="99%"
               KeyFieldName="ID"
               runat="server" AutoGenerateColumns="False" CssFilePath="~/App_Themes/Office2003 Olive/{0}/styles.css" CssPostfix="Office2003_Olive">
                <Columns>
                   <dxwgv:GridViewDataTextColumn Caption="ID" FieldName="ID" Visible="False">
                   </dxwgv:GridViewDataTextColumn>
                  
                   <dxwgv:GridViewDataTextColumn Caption="公司名称" FieldName="Customer" VisibleIndex="0">
                   <HeaderStyle HorizontalAlign="center" />
                   </dxwgv:GridViewDataTextColumn>
                  
                   <dxwgv:GridViewDataTextColumn Caption="公司简称" FieldName="Abbv" VisibleIndex="1">
                   <HeaderStyle HorizontalAlign="center" />
                   </dxwgv:GridViewDataTextColumn>
                  
                   <dxwgv:GridViewDataTextColumn Caption="公司地址" FieldName="Address" VisibleIndex="2">
                   <HeaderStyle HorizontalAlign="center" />
                   </dxwgv:GridViewDataTextColumn>
                  
                   <dxwgv:GridViewDataTextColumn Caption="公司性质" FieldName="Nature" VisibleIndex="3">
                   <HeaderStyle HorizontalAlign="center" />
                   </dxwgv:GridViewDataTextColumn>
                  
                   <dxwgv:GridViewDataTextColumn Caption="联系人" FieldName="ContactPerson" VisibleIndex="4">
                   <HeaderStyle HorizontalAlign="center" />
                   </dxwgv:GridViewDataTextColumn>
                  
                   <dxwgv:GridViewDataTextColumn Caption="联系电话" FieldName="Tel" VisibleIndex="5">
                   <HeaderStyle HorizontalAlign="center" />
                   </dxwgv:GridViewDataTextColumn>
                  
                   <dxwgv:GridViewDataTextColumn Caption="传真" FieldName="Fax" VisibleIndex="6">
                   <HeaderStyle HorizontalAlign="center" />
                   </dxwgv:GridViewDataTextColumn>
                  
                   <dxwgv:GridViewDataTextColumn Caption="信用期限" FieldName="ExpiryDate" VisibleIndex="7">
                   <HeaderStyle HorizontalAlign="center" />
                      <PropertiesTextEdit DisplayFormatString="{0:d}"></PropertiesTextEdit>
                   </dxwgv:GridViewDataTextColumn>
                  
                   <dxwgv:GridViewDataTextColumn Caption="信用额度" FieldName="CreditLine" VisibleIndex="8">
                   <HeaderStyle HorizontalAlign="center" />
                      <PropertiesTextEdit DisplayFormatString="{0:N2}"></PropertiesTextEdit>
                   </dxwgv:GridViewDataTextColumn>
                </Columns>
                <Settings ShowVerticalScrollBar="True" VerticalScrollableHeight="400" />
                <SettingsPager>
                    <AllButton Text="All">
                    </AllButton>
                    <NextPageButton Text="Next &gt;">
                    </NextPageButton>
                    <PrevPageButton Text="&lt; Prev">
                    </PrevPageButton>
                </SettingsPager>
                <SettingsBehavior AllowFocusedRow="True" />
                   <Images ImageFolder="~/App_Themes/Office2003 Olive/{0}/">
                       <ExpandedButton Height="12px" Url="~/App_Themes/Office2003 Olive/GridView/gvExpandedButton.png"
                           Width="11px" />
                       <CollapsedButton Height="12px" Url="~/App_Themes/Office2003 Olive/GridView/gvCollapsedButton.png"
                           Width="11px" />
                       <DetailCollapsedButton Height="12px" Url="~/App_Themes/Office2003 Olive/GridView/gvCollapsedButton.png"
                           Width="11px" />
                       <DetailExpandedButton Height="12px" Url="~/App_Themes/Office2003 Olive/GridView/gvExpandedButton.png"
                           Width="11px" />
                   </Images>
                   <Styles CssFilePath="~/App_Themes/Office2003 Olive/{0}/styles.css" CssPostfix="Office2003_Olive">
                       <LoadingPanel ImageSpacing="10px">
                       </LoadingPanel>
                       <Header ImageSpacing="5px" SortingImageSpacing="5px">
                       </Header>
                   </Styles>
               </dxwgv:ASPxGridView>
               <dxwgv:ASPxGridViewExporter ID="ASPxGridViewExporter1" runat="server">
               </dxwgv:ASPxGridViewExporter>
               <asp:Button ID="btnExport" runat="server" Text="导出到Excel" OnClick="btnExport_Click" />
           </ContentTemplate>
        </asp:UpdatePanel>
        <iframe id="gToday:normal:agenda.js" style="display: none; left: 254px; position: absolute;
            top: 1215px;" name="gToday:normal:agenda.js" src="../js/date2/ipopeng.htm"
            frameborder="0" scrolling="no"></iframe>
    </form>
</body>
</html>

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class webpage_ColPaymentInfo : System.Web.UI.Page
{
    public string strOp
    {
        get
        {
            object o = ViewState["op"];
            return (o == null) ? "" : (string)o;
        }
        set
        {
            ViewState["op"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindDataToGridView();
        }

        if (Session["ColPaymentInfo"] != null)
        {
            this.ASPxGridView1.DataSource = (Session["ColPaymentInfo"] as DataTable);
            this.ASPxGridView1.DataBind();
        }

    }


    #region 新增
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        strOp = "Add";
       
    }
    #endregion


    #region 修改
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        BindDataToPageForm();
        strOp = "Edit";
    }
    #endregion


    #region 保存
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string ID = this.ASPxGridView1.GetRowValues(this.ASPxGridView1.FocusedRowIndex, "ID").ToString();
      
        if (string.IsNullOrEmpty(tbCompanyName.Text))
        {
            PubFunction.ShowMessage(this.Page, "公司名称不能为空!");
            return;
        }

        DataSet ds = new DataSet();
        DataAccessManager dataaccess = new DataAccessManager();
        if (strOp.Equals("Add"))
        {
            dataaccess.NTC.AddParam("@ID", null);
        }
        else
        {
            dataaccess.NTC.AddParam("@ID",ID);
        }
        dataaccess.NTC.AddParam("@Customer", tbCompanyName.Text);
        dataaccess.NTC.AddParam("@Abbv", string.IsNullOrEmpty(tbAbbName.Text) ? null : tbAbbName.Text);
        dataaccess.NTC.AddParam("@Address", string.IsNullOrEmpty(tbCompAddr.Text) ? null : tbCompAddr.Text);
        dataaccess.NTC.AddParam("@Nature", string.IsNullOrEmpty(tbCompNature.Text) ? null : tbCompNature.Text);
        dataaccess.NTC.AddParam("@Tel", string.IsNullOrEmpty(tbTel.Text) ? null : tbTel.Text);
        dataaccess.NTC.AddParam("@Fax", string.IsNullOrEmpty(tbFax.Text) ? null : tbFax.Text);
        dataaccess.NTC.AddParam("@ContactPerson", string.IsNullOrEmpty(tbContactPerson.Text) ? null : tbContactPerson.Text);
        dataaccess.NTC.AddParam("@Mobile", null);
        dataaccess.NTC.AddParam("@ExpiryDate", string.IsNullOrEmpty(tbExpireDate.Text) ? null : tbExpireDate.Text);
        dataaccess.NTC.AddParam("@CreditLine", string.IsNullOrEmpty(tbCreditLine.Text) ? null : tbCreditLine.Text);
        dataaccess.NTC.AddParam("@op", strOp);
        dataaccess.NTC.ExecuteProcedure("P_SaveCollectionPaymentInfo", ref ds);
        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            PubFunction.ShowMessage(this.Page, ds.Tables[0].Rows[0][0].ToString());
        }
        BindDataToGridView();
    }
    #endregion


    #region 删除
    protected void btnDel_Click(object sender, EventArgs e)
    {
        string ID=this.ASPxGridView1.GetRowValues(this.ASPxGridView1.FocusedRowIndex,"ID").ToString();
        string sql=string.Format("delete from T_CollectionPaymentInfo where id='{0}'",ID);
        int i = SqlHelper.ExecuteNonQuery(sql);
        BindDataToGridView();
    }
    #endregion

    #region 绑定数据到GridView
    public void BindDataToGridView()
    {
        string sql = "select * from T_CollectionPaymentInfo";
        DataSet ds = new DataSet();
        ds = SqlHelper.Query(sql);
        if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            Session["ColPaymentInfo"] = ds.Tables[0];
            this.ASPxGridView1.DataSource = (Session["ColPaymentInfo"] as DataTable);
            this.ASPxGridView1.DataBind();
        }

    }
    #endregion

    #region
    public void BindDataToPageForm()
    {
        string ID=this.ASPxGridView1.GetRowValues(this.ASPxGridView1.FocusedRowIndex,"ID").ToString();
        string sql =string.Format("select * from T_CollectionPaymentInfo where ID='{0}'",ID);
        System.Data.SqlClient.SqlDataReader dr = null;
        dr=SqlHelper.ExecuteReader(sql);
        if (dr.Read())
        {
            tbCompanyName.Text = dr["Customer"].ToString();
            tbAbbName.Text = dr["Abbv"].ToString();
            tbCompAddr.Text = dr["Address"].ToString();
            tbCompNature.Text = "";
            tbContactPerson.Text = dr["ContactPerson"].ToString();
            tbCreditLine.Text = string.IsNullOrEmpty(dr["CreditLine"].ToString()) ? null : Convert.ToDecimal(dr["CreditLine"].ToString()).ToString("#.##");
            tbTel.Text = dr["Tel"].ToString();
            tbFax.Text = dr["Fax"].ToString();
            tbExpireDate.Text = string.IsNullOrEmpty(dr["ExpiryDate"].ToString()) ? null : Convert.ToDateTime(dr["ExpiryDate"].ToString()).ToString("d");
        }
       
    }
    #endregion

    #region 导出到Excel
    protected void btnExport_Click(object sender, EventArgs e)
    {
        this.ASPxGridViewExporter1.GridViewID="ASPxGridView1";
        this.ASPxGridViewExporter1.WriteXlsToResponse();
    }
    #endregion

  
}

原创粉丝点击