所以我试图启用一些下拉菜单,这些下拉菜单可以过滤gridview中显示的数据,因此它与下拉列表中的内容完全匹配。由于我如何在gridview上使用编辑/删除/更新功能。下面是我的gridview设置,以便您可以看到数据是如何被拉入的。
我正在寻找语法示例以尝试解决此问题:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Manager.aspx.cs" Inherits="com.tortoise.Admin.AdminViews.Manager" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
#form1 {
height: 311px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button Text="Upload" OnClick = "Upload" runat="server" />
<asp:SqlDataSource ID="SqlDataAdmins" runat="server" ConnectionString="<%$ ConnectionStrings:TortoiseDBConnectionString %>" SelectCommand="SELECT * FROM [Zoopla] WHERE (([Weeks] = @Weeks) AND ([PropStatus] = @PropStatus))" OldValuesParameterFormatString="original_{0}" OnSelecting="SqlDataAdmins_Selecting">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Weeks" PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="DropDownList2" Name="PropStatus" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="ZooplaURL" Type="string" />
<asp:Parameter Name="HouseNumber" Type="string" />
<asp:Parameter Name="PropAddress" Type="string" />
<asp:Parameter Name="Town" Type="string" />
<asp:Parameter Name="County" Type="string" />
<asp:Parameter Name="Postcode" Type="string" />
<asp:Parameter Name="Price" Type="Int32" />
<asp:Parameter Name="PropType" Type="string" />
<asp:Parameter Name="Beds" Type="Int32" />
<asp:Parameter Name="PropStatus" Type="string" />
<asp:Parameter Name="Weeks" Type="Int32" />
<asp:Parameter Name="original_ID" Type="Int32" />
<asp:Parameter Name="original_ZooplaURL" Type="String" />
<asp:Parameter Name="original_HouseNumber" Type="String" />
<asp:Parameter Name="original_PropAddress" Type="String" />
<asp:Parameter Name="original_Town" Type="String" />
<asp:Parameter Name="original_County" Type="String" />
<asp:Parameter Name="original_Postcode" Type="String" />
<asp:Parameter Name="original_Price" Type="Int32" />
<asp:Parameter Name="original_PropType" Type="String" />
<asp:Parameter Name="original_Beds" Type="Int32" />
<asp:Parameter Name="original_PropStatus" Type="String" />
<asp:Parameter Name="original_Weeks" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:Button ID="Logout" runat="server" OnClick="Logout_Click" Text="Logout" />
<br />
Filter By Weeks:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataTextField="Weeks" DataValueField="Weeks">
<asp:ListItem>0</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
<asp:ListItem>13</asp:ListItem>
<asp:ListItem>14</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>16</asp:ListItem>
<asp:ListItem>17</asp:ListItem>
<asp:ListItem>18</asp:ListItem>
<asp:ListItem>19</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>21</asp:ListItem>
<asp:ListItem>22</asp:ListItem>
<asp:ListItem>23</asp:ListItem>
<asp:ListItem>24</asp:ListItem>
<asp:ListItem>25</asp:ListItem>
<asp:ListItem>26</asp:ListItem>
<asp:ListItem>27</asp:ListItem>
<asp:ListItem>28</asp:ListItem>
<asp:ListItem>29</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
</asp:DropDownList>
Filter By Status:
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataTextField="PropStatus" DataValueField="PropStatus">
<asp:ListItem>For Sale</asp:ListItem>
<asp:ListItem>Withdrawn</asp:ListItem>
<asp:ListItem>Sold/SSTC</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="ZooplaProperties" runat="server" ConnectionString="<%$ ConnectionStrings:TortoiseDBConnectionString %>" SelectCommand="SELECT [PropStatus] FROM [Zoopla]"></asp:SqlDataSource>
<asp:SqlDataSource ID="TortoiseDBZoopla" runat="server" ConnectionString="<%$ ConnectionStrings:TortoiseDBConnectionString %>" SelectCommand="SELECT [Weeks] FROM [Zoopla]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" EmptyDataText="No records has been added." AllowSorting="True">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="ID" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtID" runat="server" Text='<%# Eval("ID") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ZooplaURL" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblZooplaURL" runat="server" Text='<%# Eval("ZooplaURL") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtZooplaURL" runat="server" Text='<%# Eval("ZooplaURL") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="HouseNumber" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblHouseNumber" runat="server" Text='<%# Eval("HouseNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtHouseNumber" runat="server" Text='<%# Eval("HouseNumber") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PropAddress" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblPropAddress" runat="server" Text='<%# Eval("PropAddress") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPropAddress" runat="server" Text='<%# Eval("PropAddress") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Town" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblTown" runat="server" Text='<%# Eval("Town") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTown" runat="server" Text='<%# Eval("Town") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="County" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblCounty" runat="server" Text='<%# Eval("County") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCounty" runat="server" Text='<%# Eval("County") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ZooplaURL" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblPostcode" runat="server" Text='<%# Eval("Postcode") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPostcode" runat="server" Text='<%# Eval("Postcode") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPrice" runat="server" Text='<%# Eval("Price") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PropType" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblPropType" runat="server" Text='<%# Eval("PropType") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPropType" runat="server" Text='<%# Eval("PropType") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Beds" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblBeds" runat="server" Text='<%# Eval("Beds") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBeds" runat="server" Text='<%# Eval("Beds") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PropStatus" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblPropStatus" runat="server" Text='<%# Eval("PropStatus") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPropStatus" runat="server" Text='<%# Eval("PropStatus") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weeks" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblWeeks" runat="server" Text='<%# Eval("Weeks") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWeeks" runat="server" Text='<%# Eval("Weeks") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150"/>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="ExportData" runat="server" OnClick="ExportData_Click" Text="Export Visible Data" />
</form>
</body>
</html>
这是背后的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CsvHelper;
using System.Text;
using System.Threading;
namespace com.tortoise.Admin.AdminViews
{
public partial class Manager : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["New"] != null)
{
Session["New"].ToString();
}
else
{
}
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["TortoiseDBConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UpdateZoopla"))
{
cmd.Parameters.AddWithValue("@Action", "SELECT");
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
protected void Logout_Click(object sender, EventArgs e)
{
Session["New"] = null;
Response.Redirect("AdminLogin.aspx");
}
protected void Upload(object sender, EventArgs e)
{
//Upload and save the file
string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(csvPath);
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[12] { new DataColumn("ID", typeof(int)),
new DataColumn("ZooplaURL", typeof(string)),
new DataColumn("HouseNumber",typeof(string)),
new DataColumn("PropAddress",typeof(string)),
new DataColumn("Town",typeof(string)),
new DataColumn("County",typeof(string)),
new DataColumn("Postcode",typeof(string)),
new DataColumn("Price",typeof(int)),
new DataColumn("PropType",typeof(string)),
new DataColumn("Beds",typeof(int)),
new DataColumn("PropStatus",typeof(string)),
new DataColumn("Weeks",typeof(int)) });
string csvData = File.ReadAllText(csvPath);
foreach (string row in csvData.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
dt.Rows.Add();
int i = 0;
foreach (string cell in row.Split(','))
{
dt.Rows[dt.Rows.Count - 1][i] = cell;
i++;
}
}
}
string consString = ConfigurationManager.ConnectionStrings["TortoiseDBConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
//Set the database table name
sqlBulkCopy.DestinationTableName = "dbo.Zoopla";
con.Open();
sqlBulkCopy.WriteToServer(dt);
con.Close();
}
}
}
protected void BindSubjectData()
{
string consString = ConfigurationManager.ConnectionStrings["TortoiseDBConnectionString"].ConnectionString;
using (SqlConnection sqlCon = new SqlConnection(consString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM Zoopla";
cmd.Connection = sqlCon;
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Rows[0].Visible = false;
}
sqlCon.Close();
}
}
}
//called on row edit command
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void ExportData_Click(object sender, EventArgs e)
{
string consString = ConfigurationManager.ConnectionStrings["TortoiseDBConnectionString"].ConnectionString;
StringBuilder sb = new StringBuilder();
using (SqlConnection con = new SqlConnection(consString))
{
con.Open();
string sql = ("SELECT [ID], [HouseNumber], [PropAddress], [Town], [County], [PostCode] FROM Zoopla;");
SqlCommand cmd = new SqlCommand(sql, con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataBind();
cmd.Dispose();
con.Close();
string filename = "filtered-addresses.xml";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
dgGrid.RenderControl(hw);
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string HouseNumber = (row.FindControl("txtHouseNumber") as TextBox).Text;
string constr = ConfigurationManager.ConnectionStrings["TortoiseDBConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UpdateZoopla"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "UPDATE");
cmd.Parameters.AddWithValue("@ID", ID);
cmd.Parameters.AddWithValue("@HouseNumber", HouseNumber);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void SqlDataAdmins_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
string constr = ConfigurationManager.ConnectionStrings["TortoiseDBConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UpdateZoopla"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "DELETE");
cmd.Parameters.AddWithValue("@ID", ID);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindGrid();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:0)
尝试将您的调用从Page_Load转移到this.BindGrid()到Page_PreRenderComplete。 Page_Load在DropDownList选择更改之前发生,让您滞后一个回发。