本人是初学者,用的是visual studio2005,c#语言,数据库是.mdb格式,利用gridview显示的数据库,表名为user,
下面是gridview的脚本:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" Width="1500px" DataKeyNames="序号" DataSourceID="AccessDataSource1"
ForeColor="#333333" GridLines="None" PageSize="25">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick="return confirm('确定要删除该记录吗?');" Text="删除" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="序号" HeaderText="序号" InsertVisible="False" ReadOnly="True"
SortExpression="序号" />
<asp:BoundField DataField="账号" HeaderText="账号" SortExpression="账号" />
<asp:BoundField DataField="密码" HeaderText="密码" SortExpression="密码" />
<asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />
<asp:BoundField DataField="备注" HeaderText="备注" SortExpression="备注" />
</Columns>
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Left" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<PagerTemplate>
<table width="100%">
<tr>
<td style="text-align:left; font-size:13px; width:260px; text-decoration: none;">
第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
共<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount %>' />页
<asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" />
<asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />
<asp:linkbutton id="btnNext" runat="server" causesvalidation="True" commandargument="Next" commandname="Page" text="下一页" />
<asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" />
</td>
<td style="text-align:left; font-size:14px;"></td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
下面是我查询的脚本:<asp:Label ID="Label1" runat="server" Height="21px" Text="请选择查询条件" Width="121px"></asp:Label>
<asp:DropDownList ID="Ddl_Object" runat="server"
Width="137px" OnSelectedIndexChanged="Ddl_Object_SelectedIndexChanged">
<asp:ListItem Value="0"><无></asp:ListItem>
<asp:ListItem Value="1">账号</asp:ListItem>
<asp:ListItem Value="2">姓名</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="Tb_Content" runat="server" Width="171px" OnTextChanged="Tb_Content_TextChanged"></asp:TextBox>
<asp:Button ID="Bt_Find" runat="server" OnClick="Bt_Find_Click" Text="查询" Width="61px" />下面是我后台查询的代码:
void SearchResult()
{
AccessDataSource1.SelectParameters.Clear();
switch (Convert.ToInt32(Ddl_Object.SelectedValue))
{
case 1: AccessDataSource1.SelectCommand = "SELECT * FROM [user] WHERE (账号 LIKE '%' + ? + '%' ) ORDER BY [序号]";
AccessDataSource1.SelectParameters.Add("账号", Tb_Content.Text);
GridView1.AllowPaging = false;
break;
case 2: AccessDataSource1.SelectCommand = "SELECT * FROM [user] WHERE ([姓名] LIKE '%'+ ? +'%' ) ORDER BY [序号]";
AccessDataSource1.SelectParameters.Add("姓名", Tb_Content.Text);
GridView1.AllowPaging = false;
break;
default:
AccessDataSource1.SelectCommand = "SELECT * FROM [user] ORDER BY [序号]";
// Response.Redirect("6master/main.aspx");
break; }
}
不查询的时候可以正常翻页,查询之后查询结果不会自动分页,全部显示在一页里!怎么办??!
下面是gridview的脚本:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" Width="1500px" DataKeyNames="序号" DataSourceID="AccessDataSource1"
ForeColor="#333333" GridLines="None" PageSize="25">
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick="return confirm('确定要删除该记录吗?');" Text="删除" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="序号" HeaderText="序号" InsertVisible="False" ReadOnly="True"
SortExpression="序号" />
<asp:BoundField DataField="账号" HeaderText="账号" SortExpression="账号" />
<asp:BoundField DataField="密码" HeaderText="密码" SortExpression="密码" />
<asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />
<asp:BoundField DataField="备注" HeaderText="备注" SortExpression="备注" />
</Columns>
<RowStyle BackColor="#E3EAEB" />
<EditRowStyle BackColor="#7C6F57" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Left" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<PagerTemplate>
<table width="100%">
<tr>
<td style="text-align:left; font-size:13px; width:260px; text-decoration: none;">
第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页
共<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount %>' />页
<asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" />
<asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />
<asp:linkbutton id="btnNext" runat="server" causesvalidation="True" commandargument="Next" commandname="Page" text="下一页" />
<asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" />
</td>
<td style="text-align:left; font-size:14px;"></td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
下面是我查询的脚本:<asp:Label ID="Label1" runat="server" Height="21px" Text="请选择查询条件" Width="121px"></asp:Label>
<asp:DropDownList ID="Ddl_Object" runat="server"
Width="137px" OnSelectedIndexChanged="Ddl_Object_SelectedIndexChanged">
<asp:ListItem Value="0"><无></asp:ListItem>
<asp:ListItem Value="1">账号</asp:ListItem>
<asp:ListItem Value="2">姓名</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="Tb_Content" runat="server" Width="171px" OnTextChanged="Tb_Content_TextChanged"></asp:TextBox>
<asp:Button ID="Bt_Find" runat="server" OnClick="Bt_Find_Click" Text="查询" Width="61px" />下面是我后台查询的代码:
void SearchResult()
{
AccessDataSource1.SelectParameters.Clear();
switch (Convert.ToInt32(Ddl_Object.SelectedValue))
{
case 1: AccessDataSource1.SelectCommand = "SELECT * FROM [user] WHERE (账号 LIKE '%' + ? + '%' ) ORDER BY [序号]";
AccessDataSource1.SelectParameters.Add("账号", Tb_Content.Text);
GridView1.AllowPaging = false;
break;
case 2: AccessDataSource1.SelectCommand = "SELECT * FROM [user] WHERE ([姓名] LIKE '%'+ ? +'%' ) ORDER BY [序号]";
AccessDataSource1.SelectParameters.Add("姓名", Tb_Content.Text);
GridView1.AllowPaging = false;
break;
default:
AccessDataSource1.SelectCommand = "SELECT * FROM [user] ORDER BY [序号]";
// Response.Redirect("6master/main.aspx");
break; }
}
不查询的时候可以正常翻页,查询之后查询结果不会自动分页,全部显示在一页里!怎么办??!
GridView1.AllowPaging = false;
把他给砍了第二个问题:
给GridView1加上PageIndexChanging事件:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
或者,你可以参考一下百度搜索的样子,点击搜索之后,url里面有一个经过编码的搜索关键字,你也可以采用这种方法,譬如你在a.aspx页面点击搜索按钮,直接通过Response.Redirect("a.aspx?key=经过编码的搜索关键字"),然后在cs的Page_Load里接收这个Request.QueryString["key"],如果不为空,则按关键字查询。
或者,你可以参考一下百度搜索的样子,点击搜索之后,url里面有一个经过编码的搜索关键字,你也可以采用这种方法,譬如你在a.aspx页面点击搜索按钮,直接通过Response.Redirect("a.aspx?key=经过编码的搜索关键字"),然后在cs的Page_Load里接收这个Request.QueryString["key"],如果不为空,则按关键字查询。[/q上一次修改数据库总数目的统计问题 的时候已经删除了Page_Load,下面括号里是上次你帮我修改的
(Page_Load里面的所有的代码统统删掉,用了AccessDataSource就没必要这样查询这样查询了
你给AccessDataSource1添加一个Selected事件:
AccessDataSource1.Selected += AccessDataSource1_Selected;
事件代码:
protected void AccessDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
Label11.Text = " 共" + e.AffectedRows.ToString() + "户";
}
完成了,不懂的自己再摸索一下。 )下面是cs页面所有的代码:
public partial class _Default : System.Web.UI.Page
{
protected void lb_bak_Click(object sender, EventArgs e)
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=yunwei.mdb");
string filename = Server.MapPath("~/App_Data/yunwei.mdb");
Response.TransmitFile(filename);
}
protected void Bt_Find_Click(object sender, EventArgs e)
{
SearchResult(); }
void SearchResult()
{
AccessDataSource1.SelectParameters.Clear();
switch (Convert.ToInt32(Ddl_Object.SelectedValue))
{
case 1:
AccessDataSource1.SelectCommand = "SELECT * FROM [user] WHERE (账号 LIKE '%' + ? + '%' ) ";
AccessDataSource1.SelectParameters.Add("账号", Tb_Content.Text);
break;
case 2:
AccessDataSource1.SelectCommand = "SELECT * FROM [user] WHERE ([姓名] LIKE '%'+ ? +'%' )";
AccessDataSource1.SelectParameters.Add("姓名", Tb_Content.Text);
break;
case 3:
AccessDataSource1.SelectCommand = "SELECT * FROM [user] WHERE ([小区] LIKE '%' + ? + '%' ) ";
AccessDataSource1.SelectParameters.Add("小区", Tb_Content.Text);
break;
case 4:
AccessDataSource1.SelectCommand = "SELECT * FROM [user] WHERE ([标识] LIKE '%' + ? + '%' ) ";
AccessDataSource1.SelectParameters.Add("标识", Tb_Content.Text);
break;
case 5:
AccessDataSource1.SelectCommand = "SELECT * FROM [user] WHERE ([交换机编号] LIKE '%' + ? + '%' ) ";
AccessDataSource1.SelectParameters.Add("交换机编号", Tb_Content.Text);
break;
default:
AccessDataSource1.SelectCommand = "SELECT * FROM [user]";
// Response.Redirect("6master/main.aspx");
break; }
}
protected void AccessDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
AccessDataSource1.Selected += AccessDataSource1_Selected; }
protected void AccessDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
Label11.Text = " 共" + e.AffectedRows.ToString() + "户";
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
} }怎么改,求大神指点啊 ??!!!