FormView与GridView使用问题

hannah_happy 2009-08-03 09:01:29
有一个界面有GridView1,FormView1, SqlDataSource1,刚开始FormView1跟Gridview1同时绑定sqlDatasource1,
但点击查找按钮时改变了sqlDataSource1.SelectCommand后,按钮click同时写了 FormView1.PageIndex = GridView1.SelectedIndex;
但为什么Formview1还是原来没有过滤之前的数据
...全文
148 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
koukoujiayi 2009-08-03
  • 打赏
  • 举报
回复
你这样编有问题!!
你动态改变了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";

}
Adechen 2009-08-03
  • 打赏
  • 举报
回复
你应该没有触发formview1的onSelectedIndexChanged事件
hannah_happy 2009-08-03
  • 打赏
  • 举报
回复
test.aspx代码:
<%@ 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();
}
}

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧