用于刷新GridView的按钮?

时间:2015-03-02 16:47:59

标签: c# asp.net excel stored-procedures gridview

基本上,我有一个SQLDataSource,它是一个存储过程,有两个参数(datetime)。我有两个日历,可以在点击时设置这些参数。我检查了存储过程和参数是否正常,但GridView没有显示存储过程的结果,因为用户必须单击日历来设置结果的参数。我认为GridView只显示没有任何参数的存储过程结果(所以它是空白的)。这是我的怀疑,因为我还有一个“导出到Excel”按钮,将GridView导出到Excel,并且Excel文件在下载后也是空白的。

我认为我需要实现一个刷新GridView的按钮,以便用户可以使用两个日历为创建结果的存储过程选择日期参数。然后该按钮将刷新/更新GridView,该GridView仅显示具有新存储过程结果的初始空白结果。如何创建这个“刷新按钮”?

供参考, aspx.cs看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        DateTime fromDate = new DateTime();
        DateTime toDate = new DateTime();


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            GridView1.AllowPaging = false;
            GridView1.AllowSorting = false;
            GridView1.DataBind();

            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=gvtoexcel.xls");
            Response.ContentType = "application/excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridView1.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();

            GridView1.AllowPaging = true;
            GridView1.AllowSorting = true;
            GridView1.DataBind();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            fromDate = Calendar1.SelectedDate;
            SqlDataSource1.SelectParameters[0].DefaultValue = fromDate.ToString();


        }

        protected void Calendar2_SelectionChanged(object sender, EventArgs e)
        {
            toDate = Calendar2.SelectedDate;
            SqlDataSource1.SelectParameters[1].DefaultValue = toDate.ToString();
        }
    }
}

.aspx看起来像这样:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" EnableEventValidation="false" %>

<!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>
    <style type="text/css">
        .style1
        {
            text-align: center;
        }
        .style2
        {
            text-align: left;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="style1" style="margin-left: 40px">

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:BIZ_DBConnectionString %>" 
            SelectCommand="Payrolldeduction" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter DbType="DateTime" Name="fromDate" />
                <asp:Parameter DbType="DateTime" Name="toDate" />
            </SelectParameters>
        </asp:SqlDataSource>
        <div class="style1">

        Payroll Report<br />
        </div>
        <div class="style2">
            <br />
            From:</div>
        <asp:Calendar ID="Calendar1" runat="server" 
            onselectionchanged="Calendar1_SelectionChanged" style="text-align: left">
        </asp:Calendar>
        <div class="style2">
            To:</div>
        <asp:Calendar ID="Calendar2" runat="server" style="text-align: left">
        </asp:Calendar>
        <br />
        <br />
        <br />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
            Text="Export to Excel" />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
            onselectedindexchanged="Page_Load" AllowPaging="True">
            <Columns>
                <asp:BoundField DataField="INST_ID" HeaderText="INST_ID" 
                    SortExpression="INST_ID" />
                <asp:BoundField DataField="EMPLOYEE_ID" HeaderText="EMPLOYEE_ID" 
                    SortExpression="EMPLOYEE_ID" />
                <asp:BoundField DataField="HR_DEDUCTION_AND_BENEFITS_CODE" 
                    HeaderText="HR_DEDUCTION_AND_BENEFITS_CODE" 
                    SortExpression="HR_DEDUCTION_AND_BENEFITS_CODE" />
                <asp:BoundField DataField="Column1" HeaderText="Column1" ReadOnly="True" 
                    SortExpression="Column1" />
                <asp:BoundField DataField="WITHHOLDING_LIABILITY_ACCOUNT_MASK" 
                    HeaderText="WITHHOLDING_LIABILITY_ACCOUNT_MASK" 
                    SortExpression="WITHHOLDING_LIABILITY_ACCOUNT_MASK" />
                <asp:BoundField DataField="HR_DEDUCTION_AND_BENEFITS_ID" 
                    HeaderText="HR_DEDUCTION_AND_BENEFITS_ID" 
                    SortExpression="HR_DEDUCTION_AND_BENEFITS_ID" />
                <asp:BoundField DataField="CHECK_DATE" HeaderText="CHECK_DATE" 
                    SortExpression="CHECK_DATE" />
                <asp:BoundField DataField="CHECK_NO" HeaderText="CHECK_NO" 
                    SortExpression="CHECK_NO" />
                <asp:BoundField DataField="FIN_INST_ACCT_ID" HeaderText="FIN_INST_ACCT_ID" 
                    SortExpression="FIN_INST_ACCT_ID" />
                <asp:BoundField DataField="Column2" HeaderText="Column2" ReadOnly="True" 
                    SortExpression="Column2" />
                <asp:BoundField DataField="HR_DEDUCTION_AND_BENEFIT_CYCLE_CODE" 
                    HeaderText="HR_DEDUCTION_AND_BENEFIT_CYCLE_CODE" 
                    SortExpression="HR_DEDUCTION_AND_BENEFIT_CYCLE_CODE" />
                <asp:BoundField DataField="LENGTH" HeaderText="LENGTH" 
                    SortExpression="LENGTH" />
                <asp:BoundField DataField="EMPLOYEE_COMPUTED_AMOUNT" 
                    HeaderText="EMPLOYEE_COMPUTED_AMOUNT" 
                    SortExpression="EMPLOYEE_COMPUTED_AMOUNT" />
                <asp:BoundField DataField="EMPLOYEE_BANK_ROUTING_NUMBER" 
                    HeaderText="EMPLOYEE_BANK_ROUTING_NUMBER" 
                    SortExpression="EMPLOYEE_BANK_ROUTING_NUMBER" />
                <asp:BoundField DataField="EMPLOYEE_ACCOUNT_TYPE" 
                    HeaderText="EMPLOYEE_ACCOUNT_TYPE" SortExpression="EMPLOYEE_ACCOUNT_TYPE" />
                <asp:BoundField DataField="EMPLOYEE_ACCOUNT_NUMBER" 
                    HeaderText="EMPLOYEE_ACCOUNT_NUMBER" SortExpression="EMPLOYEE_ACCOUNT_NUMBER" />
                <asp:BoundField DataField="EMPLOYER_COMPUTED_AMOUNT" 
                    HeaderText="EMPLOYER_COMPUTED_AMOUNT" 
                    SortExpression="EMPLOYER_COMPUTED_AMOUNT" />
                <asp:BoundField DataField="EMPLOYEE_GROSS_AMOUNT" 
                    HeaderText="EMPLOYEE_GROSS_AMOUNT" SortExpression="EMPLOYEE_GROSS_AMOUNT" />
                <asp:BoundField DataField="EMPLOYER_GROSS_AMOUNT" 
                    HeaderText="EMPLOYER_GROSS_AMOUNT" SortExpression="EMPLOYER_GROSS_AMOUNT" />
                <asp:CheckBoxField DataField="PAYROLL_EXCLUDE" HeaderText="PAYROLL_EXCLUDE" 
                    SortExpression="PAYROLL_EXCLUDE" />
                <asp:BoundField DataField="VOID_DATE" HeaderText="VOID_DATE" 
                    SortExpression="VOID_DATE" />
                <asp:BoundField DataField="BATCH_QUEUE_ID" HeaderText="BATCH_QUEUE_ID" 
                    SortExpression="BATCH_QUEUE_ID" />
                <asp:BoundField DataField="BATCH_CODE" HeaderText="BATCH_CODE" 
                    SortExpression="BATCH_CODE" />
                <asp:BoundField DataField="FY" HeaderText="FY" SortExpression="FY" />
                <asp:BoundField DataField="END_DATE" HeaderText="END_DATE" 
                    SortExpression="END_DATE" />
                <asp:BoundField DataField="COMMENTS" HeaderText="COMMENTS" 
                    SortExpression="COMMENTS" />
                <asp:BoundField DataField="BATCH_CRITERIA_USED" 
                    HeaderText="BATCH_CRITERIA_USED" SortExpression="BATCH_CRITERIA_USED" />
                <asp:BoundField DataField="COLUMN_VALUE" HeaderText="COLUMN_VALUE" 
                    SortExpression="COLUMN_VALUE" />
                <asp:BoundField DataField="REPLACEMENT" HeaderText="REPLACEMENT" 
                    SortExpression="REPLACEMENT" />
                <asp:BoundField DataField="LAST_NAME" HeaderText="LAST_NAME" 
                    SortExpression="LAST_NAME" />
                <asp:BoundField DataField="FIRST_NAME" HeaderText="FIRST_NAME" 
                    SortExpression="FIRST_NAME" />
                <asp:BoundField DataField="MIDDLE_NAME" HeaderText="MIDDLE_NAME" 
                    SortExpression="MIDDLE_NAME" />
            </Columns>
        </asp:GridView>
        <br />
        <br />
&nbsp;&nbsp;

    </div>
    </form>
</body>
</html>

感谢。

更新

我设法让日历选择的日期保持不变并充当参数(用户现在选择日历日期,然后是确认按钮/按钮2,之前是导出到Excel按钮,现在是按钮3),但是即使设置了参数,GridView也不会显示存储过程的结果。也没有出现任何错误。发生了什么事?

以下是代码现在的样子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        DateTime fmDate = new DateTime();
        DateTime toDate = new DateTime();


        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            SqlDataSource SqlDataSource1 = new SqlDataSource();
            SqlDataSource1.ID = "SqlDataSource1";
            this.Page.Controls.Add(SqlDataSource1);
            SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["BIZ_DBConnectionString"].ConnectionString;
            SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
            SqlDataSource1.SelectCommand = "Payrolldeduction";
            SqlDataSource1.SelectParameters.Clear();

            FormParameter fmDate = new FormParameter("@Param1", Calendar1.SelectedDate.ToString());
            FormParameter toDate = new FormParameter("@Param2", Calendar2.SelectedDate.ToString());
            SqlDataSource1.SelectParameters.Add(fmDate);
            SqlDataSource1.SelectParameters.Add(toDate);

            GridView1.DataSource = SqlDataSource1;
            GridView1.DataBind();

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            GridView1.AllowPaging = true;
            GridView1.AllowSorting = false;
            GridView1.DataBind();

            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=newexcelreport.xls");
            Response.ContentType = "application/excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridView1.RenderControl(htw);
            Response.Write(sw.ToString());

            GridView1.AllowPaging = true;
            GridView1.AllowSorting = false;
            GridView1.DataBind();
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            fmDate = Calendar1.SelectedDate;
            fromDate.Text = Calendar1.SelectedDate.ToString();
            SqlDataSource1.SelectParameters[0].DefaultValue = fmDate.ToString();

        }

        protected void Calendar2_SelectionChanged(object sender, EventArgs e)
        {
            toDate = Calendar2.SelectedDate;
            SqlDataSource1.SelectParameters[1].DefaultValue = toDate.ToString();
        }

        }
    }

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" EnableEventValidation="false" %>

<!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>
    <style type="text/css">
        .style1
        {
            text-align: center;
        }
        .style2
        {
            text-align: left;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="style1" style="margin-left: 40px">

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:BIZ_DBConnectionString %>" 
            SelectCommand="Payrolldeduction" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter DbType="DateTime" Name="fromDate" />
                <asp:Parameter DbType="DateTime" Name="toDate" />
            </SelectParameters>
        </asp:SqlDataSource>
        <div class="style1">

        Payroll Report<br />
        </div>
        <div class="style2">
            <br />
            From:</div>

            <asp:TextBox runat="server" ID="fromDate" Text=""></asp:TextBox>
        <asp:Calendar ID="Calendar1" runat="server"  style="text-align: left"> </asp:Calendar>
        <div class="style2">
            To:</div>
        <asp:Calendar ID="Calendar2" runat="server" style="text-align: left">
        </asp:Calendar>
        <br />
        <br />
        <br />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Confirm" />
        <br />
        <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Export" />
        <br />
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="true">
        </asp:GridView>
        <br />
        <br />
&nbsp;&nbsp;

    </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

  1. 您永远不要将Page_Load作为GridView事件处理程序调用。除了基于您的代码之外,它无论如何都无所事事,每当您的页面回发时都会 ,而不管导致回发的按钮。
  2. 您的SqlDataSource选择参数应为引用2个日历控件的Control Parameters。这意味着您不需要处理日历的SelectionChanged事件,它将是自动的。但是...
  3. 日历无论如何都控制自动回发,这意味着您需要通过asp valdiation或存储过程验证日期,并且只有在日期有任何意义时才返回DataSet。
  4. 对于您的GridView - 它总是会尝试刷新,因为您执行此操作:DataSourceID="SqlDataSource1"。在任何asp数据绑定控件中,当您设置DataSourceID属性时,您的控件将始终在每次回发时调用DataBind()。

  5. 更新2015-03-02~1850EST

    protected void Button2_Click(object sender, EventArgs e)
    {
        // validate the dates here then:
    
        // If you MUST use Parameters over ControlParameters, 
        // then you need to assign the date here like this:
        SqlDataSource1.SelectParameters("fromDate").DefaultValue = Calendar1.SelectedDate.ToString()
        SqlDataSource1.SelectParameters("toDate").DefaultValue = Calendar2.SelectedDate.ToString()
    
        // No need to force databind because in the markup you set the property
        //     DataSourceID="SqlDataSource1"
        // This will force the Gridview to call DataBind() on each 
        // postback.  And Control changed events (button click in this case) 
        // occur before DataBind() events
    
        //###############################################################
        //###############################################################
    
        //None of this stuff is needed. It's already done in the markup:
    
        //###############################################################
        SqlDataSource SqlDataSource1 = new SqlDataSource();
        SqlDataSource1.ID = "SqlDataSource1";
        this.Page.Controls.Add(SqlDataSource1);
        SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["BIZ_DBConnectionString"].ConnectionString;
        SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
        SqlDataSource1.SelectCommand = "Payrolldeduction";
        SqlDataSource1.SelectParameters.Clear();
    
        FormParameter fmDate = new FormParameter("@Param1", Calendar1.SelectedDate.ToString());
        FormParameter toDate = new FormParameter("@Param2", Calendar2.SelectedDate.ToString());
        SqlDataSource1.SelectParameters.Add(fmDate);
        SqlDataSource1.SelectParameters.Add(toDate);
    
        GridView1.DataSource = SqlDataSource1;
        GridView1.DataBind();
        //###############################################################
        //###############################################################
    }