I'm very new to ASP.Net 3.5 and C# 3.0. I'm trying to understand why my code below is broken. I'm using a datafilter tool that I discovered here (http://ammar.tawabini.com/2010/09/gridview-ajax-filter.html). The reason why I'm using this solution is because I needed to add a search feature to the "GridView"control I have on the page. Before I used this solution I was able to execute the export command that is embedded in the "GridView"with no problem.
My question is if a place a "GridView" with Command buttons inside an "UpdatePanel" does it off set the code behind for the Command button I have (in my case that would be the Export button).
If so how can I work around it so I can provide both functionality?
The code behind is here:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections; using System.Data; using System.Data.SqlClient; using System.IO; using System.Drawing; using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html.simpleparser; using System.Configuration; namespace DataFilterDemo { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataFilter1.DataSource = SqlDataSource1; DataFilter1.DataColumns = GridView1.Columns; DataFilter1.FilterSessionID = "Default"; DataFilter1.OnDataBound += new DataFilter.BindDataGridView(DataFilter1_OnDataBound); } void DataFilter1_OnDataBound() { try { DataFilter1.FilterSessionID = "Default"; DataFilter1.FilterDataSource(); GridView1.DataBind(); } catch (Exception ex) { DataFilter1.Info = ex.Message; } } public override void VerifyRenderingInServerForm(Control control) { /*Verifies that the control is rendered */ } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { int eventid; eventid = Convert.ToInt32(GridView1.SelectedDataKey.Value); String strCon = ConfigurationManager.ConnectionStrings["CME_RFID"].ConnectionString; DataTable dtExport = new DataTable(); String strSQL = "SELECT PersonID, Person_Last, Person_First, Person_Title, Life_Number, Event_Name, EventID, CME_Event_Track_ID FROM vAttendanceList WHERE EventID = " + eventid; SqlConnection connect = new SqlConnection(strCon); SqlCommand command = new SqlCommand(strSQL, connect); SqlDataAdapter adExport = new SqlDataAdapter(command); adExport.Fill(dtExport); exportSpreadsheet(dtExport, "ExportReport"); } public static void exportSpreadsheet(DataTable dtTable, String strName) { HttpContext context = HttpContext.Current; context.Response.Clear(); foreach (DataColumn column in dtTable.Columns) { context.Response.Write(column.ColumnName + ","); } context.Response.Write(Environment.NewLine); foreach (DataRow drRow in dtTable.Rows) { for (int i = 0; i < dtTable.Columns.Count; i++) { context.Response.Write(drRow[i].ToString().Replace(",", string.Empty) + ","); } context.Response.Write(Environment.NewLine); } context.Response.ContentType = "text/csv"; context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + strName + ".csv"); context.Response.End(); } } }
The markup is below:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DataFilterDemo._Default" %><%@ Register src="DataFilter.ascx" tagname="DataFilter" tagprefix="uc1" %><!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></head><body><form id="form1" runat="server"><div></div></body> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager><uc1:DataFilter ID="DataFilter1" runat="server" /><asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate> <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="Black" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" DataKeyNames="EventID" CellSpacing="2" ForeColor="Black" Width="984px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"><RowStyle BackColor="White" /><Columns><asp:CommandField ButtonType="Button" CausesValidation="False" DeleteText="" EditText="Add Track ID" InsertText="" NewText="" SelectText="" ShowEditButton="True" UpdateText="Commit" /><asp:CommandField ButtonType="Button" DeleteText="" EditText="" InsertText="" NewText="" SelectText="Export" UpdateText="" InsertVisible="False" ShowCancelButton="False" ShowSelectButton="True" CancelText="" /> <asp:BoundField DataField="CME_Event_Track_ID" HeaderText="CME Track ID" SortExpression="CME_Event_Track_ID" /><asp:BoundField DataField="Event_Type" HeaderText="Event Type" SortExpression="Event_Type" Visible="False" /><asp:BoundField DataField="Event_Name" HeaderText="Event Name" SortExpression="Event_Name" /><asp:BoundField DataField="Event_Desc" HeaderText="Event Desc" SortExpression="Event_Desc" /><asp:BoundField DataField="Event_Date" HeaderText="Event Date" SortExpression="Event_Date" /> <asp:BoundField DataField="EventCount" HeaderText="Attendance Count" SortExpression="EventCount" /></Columns><FooterStyle BackColor="#CCCCCC" /><PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" /><EmptyDataTemplate> </EmptyDataTemplate><SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /><HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /></asp:GridView><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CME_RFID %>" ProviderName="System.Data.SqlClient" SelectCommand="SELECT CME_Events.EventID, CME_Events.Event_Name, CME_Events.Event_Desc, CME_Events.Dept_ID, CME_Events.Event_Date, CME_Events.Event_TimeStart, COUNT(CME_IDRegistration.recID) AS EventCount, CME_Events.CME_Event_Track_ID FROM CME_RFIDReader.CME_Events INNER JOIN CME_RFIDReader.CME_IDRegistration ON CME_RFIDReader.CME_Events.EventID = CME_RFIDReader.CME_IDRegistration.EventID GROUP BY CME_Events.EventID, CME_Events.Event_Name, CME_Events.Event_Desc, CME_Events.Dept_ID, CME_Events.Event_Date, CME_Events.Event_TimeStart, CME_Events.CME_Event_Track_ID ORDER BY Event_Date DESC" UpdateCommand="UPDATE CME_RFIDReader.CME_Events SET CME_Event_Track_ID = @CME_Event_Track_ID WHERE EventID = @EventID"> </asp:SqlDataSource></ContentTemplate></asp:UpdatePanel></form></html>