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

<%@ 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" %>



    代收代付客户信息
   
     
     


   


       
       

       
       
          
             
                  
                        公司名称:
                       
                           
                        公司简称:
                       
                           
                  
                    
                        公司地址:
                       
                           
                        公司性质:
                       
                           
                      
                    
                        联系人:
                       
                           
                        联系电话:
                       
                           
                        传真:
                       
                           
                  
                  
                        信用期限:
                       
                           
                           
                           
                        信用额度:
                       
                           
                       
                       
                  
                 
                     
                         
                         
                         
                       
                      
                 
              
              

                              ID="ASPxGridView1"
               Width="99%"
               KeyFieldName="ID"
               runat="server" AutoGenerateColumns="False" CssFilePath="~/App_Themes/Office2003 Olive/{0}/styles.css" CssPostfix="Office2003_Olive">
               
                  
                  

                  
                  
                  
                  

                  
                  
                  
                  

                  
                  
                  
                  

                  
                  
                  
                  

                  
                  
                  
                  

                  
                  
                  
                  

                  
                  
                  
                  

                  
                  
                  
                     
                  

                  
                  
                  
                     
                  

               

               
               
                   
                   

                   
                   

                   
                   

               

               
                  
                                                  Width="11px" />
                                                  Width="11px" />
                                                  Width="11px" />
                                                  Width="11px" />
                  

                  
                      
                      

                      

                      

                  

              

              
              

              
          

       

       
   

 

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

  
}

你可能感兴趣的:(C#,.Net)