【发布时间】:2015-11-08 00:39:48
【问题描述】:
我正在尝试使用 sqldatasource 使用文本框在 GridView 中进行搜索,并启用了 edit、update 和 paging;但是,当我单击 update 成功搜索后,过滤后的 GridView 并没有显示,而是显示了每个数据的整个 GridView。谢谢,请指教。
aspx.cs 页面:
namespace Inventory
{
public partial class Results : System.Web.UI.Page
{
//SqlConnection conn = new SqlConnection("Data Source=10.10.101.188;Initial Catalog=ActioNetITInventory;User ID=rails.sa;Password=ActioNet1234");
private String ConnString = ConfigurationManager.ConnectionStrings["ActioNetITInventoryConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//BindData();
//BindData2();
//BindData3();
}//end if
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
BindData();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}//end page load
protected void BindData()
{
using (SqlConnection sqlCon = new SqlConnection(ConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM [Inventory]";
cmd.Connection = sqlCon;
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSourceID = null;
GridView1.DataSource = dt;
GridView1.DataBind();
sqlCon.Close();
}//end using
}//end using
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
var serial = GridView1.DataKeys[e.RowIndex].Value;
GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
//getting row field details
TextBox Assigned = row.FindControl("txtAssigned") as TextBox;
TextBox Location = row.FindControl("txtLocation") as TextBox;
TextBox Notes = row.FindControl("txtNotes") as TextBox;
using (SqlConnection sqlCon = new SqlConnection(ConnString))
{
string sql = "UPDATE Inventory SET Assigned=@Assigned, " + "Location=@Location, Notes=@Notes" + " WHERE Serial = @Serial";
using (SqlCommand cmd = new SqlCommand(sql, sqlCon))
{
cmd.Parameters.AddWithValue("@Assigned", Assigned.Text.Trim());
cmd.Parameters.AddWithValue("@Location", Location.Text.Trim());
cmd.Parameters.AddWithValue("@Notes", Notes.Text);
cmd.Parameters.AddWithValue("@Serial", serial);
sqlCon.Open();
cmd.ExecuteNonQuery();
sqlCon.Close();
}//end using
}//end using
status.Visible = true;
status.Text = "" + Assigned.Text + "has been added successfully!";
status.ForeColor = System.Drawing.Color.Green;
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
HTML页面:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="Serial" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None" Width="1575px"
OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowCancelButton="true" ShowEditButton="true" />
<asp:TemplateField HeaderText="Type" SortExpression="Type">
<ItemTemplate>
<asp:Label ID="lblType" runat="server" Text='<%#Eval("Type") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnInsert" runat="server" Text="Insert" CommandName="Add" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Make" SortExpression="Make">
<ItemTemplate>
<asp:Label ID="lblMake" runat="server" Text='<%#Eval("Make") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model" SortExpression="Model">
<ItemTemplate>
<asp:Label ID="lblModel" runat="server" Text='<%#Eval("Model") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Serial" SortExpression="Serial">
<ItemTemplate>
<asp:Label ID="lblSerial" runat="server" Text='<%#Eval("Serial") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assigned" SortExpression="Assigned">
<ItemTemplate>
<asp:Label ID="lblAssigned" runat="server" Text='<%#Eval("Assigned") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAssigned" runat="server" Text='<%#Bind("Assigned") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAssigned" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="Location">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLocation" runat="server" Text='<%#Bind("Location") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLocation" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes" SortExpression="Notes">
<ItemTemplate>
<asp:Label ID="lblNotes" runat="server" Text='<%#Eval("Notes") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNotes" runat="server" TextMode="MultiLine" Text='<%#Bind("Notes") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNotes" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ActioNetITInventoryConnectionString %>" DeleteCommand="DELETE FROM [Inventory] WHERE [Serial] = @Serial" InsertCommand="INSERT INTO [Inventory] ([Type], [Make], [Model], [Serial], [Assigned], [Location], [Notes]) VALUES (@Type, @Make, @Model, @Serial, @Assigned, @Location, @Notes)" SelectCommand="SELECT * FROM [Inventory] WHERE ([Type] = @Type)" UpdateCommand="UPDATE [Inventory] SET [Type] = @Type, [Make] = @Make, [Model] = @Model, [Assigned] = @Assigned, [Location] = @Location, [Notes] = @Notes WHERE [Serial] = @Serial">
<DeleteParameters>
<asp:Parameter Name="Serial" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Type" Type="String" />
<asp:Parameter Name="Make" Type="String" />
<asp:Parameter Name="Model" Type="String" />
<asp:Parameter Name="Serial" Type="String" />
<asp:Parameter Name="Assigned" Type="String" />
<asp:Parameter Name="Location" Type="String" />
<asp:Parameter Name="Notes" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:FormParameter FormField="typeTextBox" Name="Type" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Type" Type="String" />
<asp:Parameter Name="Make" Type="String" />
<asp:Parameter Name="Model" Type="String" />
<asp:Parameter Name="Assigned" Type="String" />
<asp:Parameter Name="Location" Type="String" />
<asp:Parameter Name="Notes" Type="String" />
<asp:Parameter Name="Serial" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
【问题讨论】:
标签: c# html sql asp.net gridview