数据控件的使用

本文展示了如何在ASP.NET中使用DataGrid控件进行数据展示,并提供了编辑功能。用户可以通过下拉列表选择查询时段,点击查询按钮获取数据。数据显示包括商家名称、市场负责人、排期名称等信息,以及各项统计数据。每个数据项还提供了编辑功能,用户可以编辑备注并保存或取消更改。

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

1、数据空间里的操作
截图
  点击编辑 
         出现输入框  取消和确定
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ad_EffectDetail.aspx.cs" Inherits="Admin_Ad_EffectDetail" %>

<HTML>
    <HEAD>
        <title>创意效果</title>
        <LINK href="../Style.css" type="text/css" rel="stylesheet">
    </HEAD>
    <body leftMargin="0"  topMargin="0" rightMargin="0"
        MS_POSITIONING="GridLayout" align="center">
        <form id="Form1" method="post" runat="server">
                <TABLE id="Table1" style="WIDTH: 70%" cellSpacing="0" cellPadding="0" border="0" align="center">
                <TR>
                    <td align="center"> <FONT class="title">创意效果</FONT></td>
                </TR>
                <TR>
                    <td> <FONT class="title">查询时段:</FONT>
                    <asp:DropDownList ID="ddlDate" runat="server">
                    <asp:ListItem Text="前7天" Value="7" Selected="True"></asp:ListItem>
                    <asp:ListItem Text="前40天" Value="40"></asp:ListItem>
                    </asp:DropDownList>
                    <asp:Button ID="btnQuery" runat="server" Text="查询" /></td>
                </TR>
                <tr>
                    <td align="center">                   
                    <table cellspacing="0" cellpadding="0" rules="all" bordercolor="#DDE1EC" border="1" id="Table3" style="border-color:#DDE1EC;border-width:1px;border-style:solid;width:100%;border-collapse:collapse;">
                    <tr align="center" style="background-color:#ffffff;height:27px;">
                        <td style="font-weight:bold;">商家名称</td>
                        <td>&nbsp;<%=__HostName %></td>
                    </tr>
                    <tr align="center" style="background-color:#ffffff;height:27px;">
                        <td style="font-weight:bold;">市场负责</td>
                        <td>&nbsp;<%=__SalerName %></td>
                    </tr>
                    <tr align="center" style="background-color:#ffffff;height:27px;">
                        <td style="font-weight:bold;">排期名称</td>
                        <td>&nbsp;<%=__ArrangeName %></td>
                    </tr>
                    <tr align="center" style="background-color:#ffffff;height:27px;">
                        <td style="font-weight:bold;">支付形式</td>
                        <td>&nbsp;联盟:<%=__ArrangePriceIn %>元,&nbsp;站长:<%=__ArrangePriceOut %>元</td>
                    </tr>
                    </table>
                <asp:datagrid id="dgShow" AllowSorting="True" Width="100%" BorderWidth="1px" CellPadding="0" AutoGenerateColumns="False"
                    ShowFooter="true" BorderColor="#dde1ec" CellSpacing="0" Runat="server" OnItemDataBound="dgShow_ItemDataBound">
                    <FooterStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" BackColor="Silver"></FooterStyle>
                    <ItemStyle HorizontalAlign="Center" Height="25px" BackColor="White"></ItemStyle>
                    <HeaderStyle HorizontalAlign="Center" Height="27px" BackColor="#DDE1EC"></HeaderStyle>
                    <AlternatingItemStyle HorizontalAlign="Center" BackColor=''></AlternatingItemStyle>
                    <SelectedItemStyle HorizontalAlign="Center"></SelectedItemStyle>
                    <Columns>
                        <asp:BoundColumn DataField="accesstime" HeaderText="日期" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:yyyy年MM月dd日}"></asp:BoundColumn>
                        <asp:BoundColumn DataField="clickrate" HeaderText="点击率" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
                        <asp:BoundColumn DataField="regrate" HeaderText="注册率" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
                        <asp:BoundColumn DataField="regcnt" HeaderText="注册数" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
                        <asp:BoundColumn DataField="regcost" HeaderText="注册成本" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
                        <asp:BoundColumn DataField="amount" HeaderText="日消耗" ItemStyle-HorizontalAlign="Center"></asp:BoundColumn>
                        <asp:TemplateColumn HeaderText="备注" ItemStyle-Width="300">
                            <ItemTemplate>
                                <%#DataBinder.Eval(Container.DataItem, "logcontent").ToString()%>&nbsp;
                                <span id="spanEdit<%#Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "accesstime")).Day.ToString()%>" style="display:none">
                                <input id="logcontent<%#Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "accesstime")).Day.ToString()%>" type="text" style="width:180px" />
                                <input type="button" value="确定" onclick="location.href='?adid=<%#DataBinder.Eval(Container.DataItem, "adid").ToString()%>&accesstime=<%#DataBinder.Eval(Container.DataItem, "accesstime","{0:yyyy-MM-dd}").ToString()%>&logcontent='+escape(document.getElementById('logcontent<%#Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "accesstime")).Day.ToString()%>').value)" />
                                &nbsp;
                                <input type="button" value="取消" onclick="document.getElementById('spanEdit<%#Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "accesstime")).Day.ToString()%>').style.display='none';document.getElementById('spanShow<%#Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "accesstime")).Day.ToString()%>').style.display='block';" />
                                </span>
                                <span id="spanShow<%#Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "accesstime")).Day.ToString()%>" style="display:block">
                                <a href="#" onclick="document.getElementById('spanEdit<%#Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "accesstime")).Day.ToString()%>').style.display='block';document.getElementById('spanShow<%#Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "accesstime")).Day.ToString()%>').style.display='none';">编辑</a&gt;
                                </span>
                            </ItemTemplate>
                        </asp:TemplateColumn>
                    </Columns>
                </asp:datagrid>
                    </td>
                </tr>
               
            </TABLE>
        </form>
    </body>
</HTML>
后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DataOperation;
using UnionLib;

public partial class Admin_Ad_EffectDetail : UnionLib.UnionPage
{

    public string __HostName = string.Empty;
    public string __SalerName = string.Empty;
    public string __ArrangeName = string.Empty;
    public string __ArrangePriceIn = string.Empty;
    public string __ArrangePriceOut = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["adid"] == null)
        {
            return;
        }
        else
        {
            string strAdid = Request.QueryString["adid"].ToString();
            if (Request.QueryString["accesstime"] != null && Request.QueryString["logcontent"] != null)
            {
                string strAccesstime = Request.QueryString["accesstime"].ToString();
                string strLogcontent = Request.QueryString["logcontent"].ToString();
                // 插入日志
                UnionLib.Common com = new UnionLib.Common();
                string strSql = string.Empty;
                strSql = @"insert into ad_adlog (adid,logtime,logtype,logcontent)
                (select @adid,to_date('@accesstime','yyyy-mm-dd'),13,'@logcontent' from dual)";
                strSql = strSql.Replace("\r\n", " ");
                strSql = strSql.Replace("@adid", strAdid);
                strSql = strSql.Replace("@accesstime", strAccesstime);
                strSql = strSql.Replace("@logcontent", strLogcontent);
                com.ExeSql(strSql);
                Response.Redirect("ad_effectdetail.aspx?adid=" + strAdid);
            }
            else
            {
                ShowArrangeInfo(strAdid);
                ShowArrangeDetail(strAdid, ddlDate.SelectedValue);
            }
        }

    }
    protected void ShowArrangeInfo(string strAdid)
    {

        UnionLib.Common com = new UnionLib.Common();
        DataTable dt = new DataTable();
        string strSql = string.Empty;
        strSql = @"select a.arrid,a.arrname,nvl(c.company,' ') company,nvl(d.salername,' ') salername,
        max(replace(f.inpaymethod,'/1000')) inpaymethod,max(replace(f.outpaymethod,'/1000')) outpaymethod
        from ad_adarrange a,ad_hostuser b,ad_host c,ad_union_saler d,ad_adset e,ad_statkind f
        where a.hostid = b.hostid and b.hostid = c.hostid and b.saler = d.salerid
        and a.arrid = e.arrid and e.adid = f.adid and e.adid=@adid
        group by a.arrid,a.arrname,c.company,d.salername";
        strSql = strSql.Replace("\r\n", " ");
        strSql = strSql.Replace("@adid", strAdid);
        dt = com.GetDataTable(strSql);
        __HostName = dt.Rows[0]["company"].ToString();
        __SalerName = dt.Rows[0]["salername"].ToString();
        __ArrangeName = dt.Rows[0]["arrname"].ToString();
        __ArrangePriceIn = dt.Rows[0]["inpaymethod"].ToString();
        __ArrangePriceOut = dt.Rows[0]["outpaymethod"].ToString();

    }
    protected void ShowArrangeDetail(string strAdid,string strDate)
    {
        UnionLib.Common com = new UnionLib.Common();
        DataTable dt = new DataTable();
        string strSql = string.Empty;
        strSql = @"select a.adid,a.title,a.accesstime,
        decode(d.shapeid,1,
        decode(decode(a.arrivalcnt,0,0,a.clickcnt),0,'-',to_char(round(a.clickcnt/a.arrivalcnt,4)*100,'fm99990.00')||'%'),
        2,decode(decode(a.showcnt,0,0,a.windowcnt),0,'-',to_char(round(a.windowcnt/a.showcnt,4)*100,'fm99990.00')||'%'),
        3,decode(decode(a.showcnt,0,0,a.windowcnt),0,'-',to_char(round(a.windowcnt/a.showcnt,4)*100,'fm99990.00')||'%'),
        decode(c.arrtype,4,decode(decode(a.arrivalcnt,0,0,a.clickcnt),0,'-',to_char(round(a.clickcnt/a.arrivalcnt,4)*100,'fm99990.00')||'%'),
        decode(decode(a.windowcnt,0,0,a.arrivalcnt),0,'-',to_char(round(a.arrivalcnt/a.windowcnt,4)*100,'fm99990.00')||'%'))
        ) clickrate,
        decode(decode(a.arrivalcnt,0,0,a.regcnt),0,'-',to_char(round(a.regcnt/a.arrivalcnt,4)*100,'fm99990.00')||'%') regrate,
        decode(a.regcnt,0,'-',to_char(round(a.amount/a.regcnt,2),'fm99990.00')) regcost,
        a.regcnt,
        to_char(a.amount,'fm99990.00') amount,
        nvl(b.logcontent,' ') logcontent
        from ad_adeffect a,
        (
        select adid,logtime,ltrim(max(sys_connect_by_path(logcontent,',')),',') logcontent
        from
          (
          select a.adid,to_date(to_char(b.logtime,'yyyy-mm-dd'),'yyyy-mm-dd') logtime,b.logcontent,
          (row_number() over(partition by a.arrid,to_date(to_char(b.logtime,'yyyy-mm-dd'),'yyyy-mm-dd') order by a.arrid,b.logtime desc)) numid
          from ad_adset a,ad_adlog b
          where a.adid = b.adid and b.logtype=13 and b.logtime>=trunc(sysdate-@date)
          )
        connect by adid = prior adid and logtime = prior logtime and numid - 1 = prior numid
        group by adid,logtime
        ) b,ad_adarrange c,
        (
           select a.shapeid,a.shapename,b.typeid,b.name from ad_shape a,ad_adtypeput b,ad_shaperelation c
              where  a.shapeid=c.shapeid
              and c.adtypeid=b.typeid
        ) d
        where a.accesstime>=trunc(sysdate-@date) and a.adid=@adid
        and a.adtype=d.typeid
        and a.adid = b.adid(+) and a.arrid=c.arrid(+)
        and a.accesstime = b.logtime(+)
        order by a.accesstime";

        strSql = strSql.Replace("\r\n", " ");
        strSql = strSql.Replace("@adid", strAdid);
        strSql = strSql.Replace("@date", strDate);
        dt = com.GetDataTable(strSql);
        // 给数据集赋值
        this.dgShow.DataSource = dt;
        this.dgShow.DataBind();
    }
    decimal avg_clickrate = 0, avg_regrate = 0, avg_regcnt = 0, avg_regcost = 0, avg_amount = 0;
    int records_clickrate = 0, records_regrate = 0, records_regcnt = 0, records_regcost = 0, records_amount = 0;
    protected void dgShow_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            try
            {
                avg_clickrate += Convert.ToDecimal(e.Item.Cells[1].Text.Replace("%", ""));
                records_clickrate++;
            }
            catch
            {
                avg_clickrate += 0;
            }
            try
            {
                avg_regrate += Convert.ToDecimal(e.Item.Cells[2].Text.Replace("%", ""));
                records_regrate++;
            }
            catch
            {
                avg_regrate += 0;
            }
            try
            {
                avg_regcnt += Convert.ToDecimal(e.Item.Cells[3].Text);
                records_regcnt++;
            }
            catch
            {
                avg_regcnt += 0;
            }
            try
            {
                avg_regcost += Convert.ToDecimal(e.Item.Cells[4].Text);
                records_regcost++;
            }
            catch
            {
                avg_regcost += 0;
            }
            try
            {
                avg_amount += Convert.ToDecimal(e.Item.Cells[5].Text);
                records_amount++;
            }
            catch
            {
                avg_amount += 0;
            }
        }
        if (e.Item.ItemType == ListItemType.Footer)
        {
            e.Item.Cells[0].Text = "日均";
            if (records_clickrate > 0)
            {
                e.Item.Cells[1].Text = Convert.ToDecimal(avg_clickrate / records_clickrate).ToString("0.000") + "%";
            }
            if (records_regrate > 0)
            {
                e.Item.Cells[2].Text = Convert.ToDecimal(avg_regrate / records_regrate).ToString("0.000") + "%";
            }
            if (records_regcnt > 0)
            {
                e.Item.Cells[3].Text = Convert.ToDecimal(avg_regcnt / records_regcnt).ToString("0.000");
            }
            if (records_regcost > 0)
            {
                e.Item.Cells[4].Text = Convert.ToDecimal(avg_regcost / records_regcost).ToString("0.000");
            }
            if (records_amount > 0)
            {
                e.Item.Cells[5].Text = Convert.ToDecimal(avg_amount / records_amount).ToString("0.000");
            }
        }
    }

}
、常用控件简介在数据库开发实践中,有些功能反复使用,如些字段的值,常常就那几个,这时,要程序记住这些值可提高用户的工作效率,这类控件有两个TCmbrec和TDBCmbrec,分别继承TCombox和TDBCombox,重载DblClick和DoEnter过程,并加了Filename、Section、Field和Caption属性。其使用方法参见“三、控件的使用”的说明。对于某些数据表来说,字段数很多,打开浏览时,屏幕放不下,需要用横向滚动条来移动查看,为了清楚浏览数据记录,希望在滚动过程中,某些字段不要移动;其二,如果数据表字段很少且比较规范,希望在数据表表格上编辑数据,编辑过程中每个字段都具有上述TDBCmbrec的功能;其三,对于综合查询或在个画面上个数据源对应于几个数据显示时,希望能够自动翻译其字段名。这些功能需求可使用TRxDBGrid和Tdbgrdrec控件来实现,都继承于TDBGrid控件,前者实现DBGrid的固定列显示、自动翻译和字段格编辑;后者实现字段格编辑。其中,TRxDBGrid增加了Filename、Section、Field、TranslateFileName、FixedCols等属性,重载DblClick和DoEnter等过程;Tdbgrdrec增加了Filename、Section、Field属性,重载DblClick和DoEnter过程。二、控件安装 上面介绍的控件,写在两个.pas文件中,DBCtrl.pas只包含TRxDBGrid控件;DBAdvanceComponent.pas包括Tcmbrec、TDBCmbrec、Tdbgrdrec等控件。安装步骤为:·第步,打开Delphi,然后选择“Component”|“Install Components”菜单项;·第二步,在“Install Component”对话框的“Unit file name”栏中,使用“Browse”按钮将控件文件加进来;·第三步,单击“OK”按钮;·第四步,在弹出的“Confirm”对话框中,单击“Yes”按钮,安装该控件文件;·第五步,在“Information”对话框,单击“OK”按钮;·重复第二至第五步,安装所有需要的控件。安装完成后,在控件面板上出现新的标签“DBAdvance”,上面有新安装的控件。之所以不写成包,是为了方便以后扩展和适应Delphi版本的更新。三、控件的使用1.Tcmbrec、TDBCmbrec和Tdbgrdrec控件 增加的保存内容放在当前目录下的“lst”子目录的Ini文件里,文件名为FileName属性的内容,新增属性Section让用户设置Init文件中“[ ]”段名,新增属性Field让用户设置数据表字段名。当然在Tdbgrdrec控件中,Field字段可不设置,控件自动根据当前选取的字段设置。子目录“lst”,控件可自动创建。2.TRxDBGrid控件 (1)FileName,Section,Field属性设置同上“1”; (2)FixedCols属性用来设置固定列数,从第列到第FixedCols列; (3)TranslateFileName属性用来设置翻译文件名,放在“txt”子目录下,是文本文件,文件格式为:字段英文名@字段中文名。特别说明:TRxDBGrid控件来源于著名的Rxlib2.75相关的控件,本人加了字段自动翻译的功能,该功能在综合查询时特别有用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值