有一个界面有GridView1,FormView1, SqlDataSource1,刚开始FormView1跟Gridview1同时绑定sqlDatasource1,
但点击查找按钮时改变了sqlDataSource1.SelectCommand后,按钮click同时写了 FormView1.PageIndex = GridView1.SelectedIndex;
但为什么Formview1还是原来没有过滤之前的数据
但点击查找按钮时改变了sqlDataSource1.SelectCommand后,按钮click同时写了 FormView1.PageIndex = GridView1.SelectedIndex;
但为什么Formview1还是原来没有过滤之前的数据
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" MaintainScrollPositionOnPostback="true"%><!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:eral_baseConnectionString %>"
SelectCommand="select top 50 * from base_ygphoto"></asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ygnumber" DataSourceID="SqlDataSource1">
<EditItemTemplate>
ygnumber:
<asp:Label ID="ygnumberLabel1" runat="server" Text='<%# Eval("ygnumber") %>'></asp:Label><br />
photo:
<asp:TextBox ID="photoTextBox" runat="server" Text='<%# Bind("photo") %>'>
</asp:TextBox><br />
photo_kzm:
<asp:TextBox ID="photo_kzmTextBox" runat="server" Text='<%# Bind("photo_kzm") %>'>
</asp:TextBox><br />
photo_size:
<asp:TextBox ID="photo_sizeTextBox" runat="server" Text='<%# Bind("photo_size") %>'>
</asp:TextBox><br />
photo_name:
<asp:TextBox ID="photo_nameTextBox" runat="server" Text='<%# Bind("photo_name") %>'>
</asp:TextBox><br />
editsj:
<asp:TextBox ID="editsjTextBox" runat="server" Text='<%# Bind("editsj") %>'>
</asp:TextBox><br />
editr:
<asp:TextBox ID="editrTextBox" runat="server" Text='<%# Bind("editr") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update">
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
ygnumber:
<asp:TextBox ID="ygnumberTextBox" runat="server" Text='<%# Bind("ygnumber") %>'>
</asp:TextBox><br />
photo:
<asp:TextBox ID="photoTextBox" runat="server" Text='<%# Bind("photo") %>'>
</asp:TextBox><br />
photo_kzm:
<asp:TextBox ID="photo_kzmTextBox" runat="server" Text='<%# Bind("photo_kzm") %>'>
</asp:TextBox><br />
photo_size:
<asp:TextBox ID="photo_sizeTextBox" runat="server" Text='<%# Bind("photo_size") %>'>
</asp:TextBox><br />
photo_name:
<asp:TextBox ID="photo_nameTextBox" runat="server" Text='<%# Bind("photo_name") %>'>
</asp:TextBox><br />
editsj:
<asp:TextBox ID="editsjTextBox" runat="server" Text='<%# Bind("editsj") %>'>
</asp:TextBox><br />
editr:
<asp:TextBox ID="editrTextBox" runat="server" Text='<%# Bind("editr") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
ygnumber:
<asp:Label ID="ygnumberLabel" runat="server" Text='<%# Eval("ygnumber") %>'></asp:Label><br />
photo:
<asp:Label ID="photoLabel" runat="server" Text='<%# Bind("photo") %>'></asp:Label><br />
photo_kzm:
<asp:Label ID="photo_kzmLabel" runat="server" Text='<%# Bind("photo_kzm") %>'></asp:Label><br />
photo_size:
<asp:Label ID="photo_sizeLabel" runat="server" Text='<%# Bind("photo_size") %>'>
</asp:Label><br />
photo_name:
<asp:Label ID="photo_nameLabel" runat="server" Text='<%# Bind("photo_name") %>'>
</asp:Label><br />
editsj:
<asp:Label ID="editsjLabel" runat="server" Text='<%# Bind("editsj") %>'></asp:Label><br />
editr:
<asp:Label ID="editrLabel" runat="server" Text='<%# Bind("editr") %>'></asp:Label><br />
</ItemTemplate>
</asp:FormView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
___________________________________________
test.aspx.cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
FormView1.PageIndex = GridView1.SelectedIndex;
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "select * from base_ygphoto where ygnumber between 1000 and 2000";
GridView1.DataBind();
FormView1.DataBind();
}
}
你动态改变了sql语句,但按GridView1的SelectedIndexChanged事件时,还是原来的sql!!
将你动态改变了sql语句,放到Page_Load里,注意IsPstBack,而不是!IsPosBack!! protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
SqlDataSource1.SelectCommand = "select * from base_ygphoto where ygnumber between 1000 and 2000"; }