Button to “Refresh” GridView?

Basically, I have a SQLDataSource that is a stored procedure that has two parameters (datetime). I have two calendars that set these parameters on click. I have checked the stored procedure and the parameters work, but the GridView is not showing the results of the stored procedure because the user has to click on the calendars to set parameters for a result. I think the GridView is just showing the stored procedure results without any parameters (so it's blank). This is my suspicion because I also have an "Export to Excel" button that exports the GridView to Excel and the Excel file is also blank after being downloaded.

I think I need to implement a button that refreshes the GridView so that the user can select the date parameters with the two calendars for the stored procedure which creates a result. The button would then refresh/update the GridView that only shows the initial blank result with the new stored procedure result. How do I create this "refresh button"?

For reference, aspx.cs looks like this:

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(); } } } 

And the .aspx looks like this:

<%@ 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://ift.tt/nuHeHq; <html xmlns="http://ift.tt/z3Mkxh; <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> 

Thanks.

by Emries via /r/csharp

Leave a Reply