用于刷新网格视图的按钮
本文关键字:按钮 视图 网格 刷新 用于 | 更新日期: 2024-09-17 02:21:40
基本上,我有一个SQLDataSource,它是一个有两个参数(datetime)的存储过程。我有两个日历,可以在单击时设置这些参数。我已经检查了存储过程和参数的工作情况,但GridView没有显示存储过程的结果,因为用户必须单击日历才能为结果设置参数。我认为GridView只是显示没有任何参数的存储过程结果(所以它是空白的)。这是我的怀疑,因为我还有一个"导出到Excel"按钮,可以将GridView导出到Excel,下载后Excel文件也是空白的。
我想我需要实现一个刷新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 />
</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 />
</div>
</form>
</body>
</html>
- 您从不将Page_Load作为GridView事件处理程序调用。除了根据您的代码,它什么都不做之外,每当您的页面回发时,无论是哪个按钮导致回发,都会调用它
- 您的
SqlDataSource
选择参数应该是引用2个日历控件的Control Parameters
。这意味着您不需要处理日历的SelectionChanged事件,它将是自动的。但是 - 日历控制自动回发,这意味着您需要通过asp valdiation或在存储过程中验证日期,并且只有在日期有任何意义时才返回数据集
- 至于你的GridView,它总是会尝试刷新,因为你这样做:
DataSourceID="SqlDataSource1"
。在任何asp数据绑定控件中,当您设置DataSourceID
属性时,您的控件将在每次回发时始终调用DataBind()
更新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();
//###############################################################
//###############################################################
}