Gridview分页无法正常工作 [英] Gridview paging not working

查看:50
本文介绍了Gridview分页无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网格视图分页方面遇到了问题。我的分页控件不起作用。我正在显示一个表,但当我点击第二页时,它不会打开。没有显示错误。就在我进入第二页的时候,它不会去第二页。



我尝试了什么:



这是我尝试的:



这是aspx页面:

i have problem in doiing gridview paging. my paging control did not work. im displaying a table but when i clicked second page, it wont open. there is no error displayed. just when i go to second page, it wont go to second page.

What I have tried:

this is what i try:

this is the aspx page:

<asp:GridView ID="Gridview1" CssClass="Grid" runat="server"

                               AutoGenerateColumns="false" Width="100%"

                               AllowPaging="true" PageSize="10" DataKeyNames="post_id"

	                       OnRowCommand="GridView1_RowCommand" 

                               OnPageIndexChanging="Gridview1_PageIndexChanging"

                               EnableViewState="true">  
	<Columns> 
		<asp:TemplateField HeaderText="No." ItemStyle-Width ="3%">
			<ItemTemplate>
				<asp:Label ID="lblindex" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
			</ItemTemplate>                                                    
		</asp:TemplateField> 
		<asp:TemplateField ItemStyle-Width ="3%" Visible="false">
			<ItemTemplate>
				<asp:Label ID="lblnum" runat="server" Text='<%# Eval("post_id") %>'></asp:Label>
			</ItemTemplate>                                                    
		</asp:TemplateField>
		<asp:BoundField DataField="job" HeaderText="Job Title" ItemStyle-Width ="16%" />
			  <ItemTemplate>
				<%# Eval("job")%>
			  </ItemTemplate>
			  <EditItemTemplate>
				  <asp:TextBox runat="server" ID="txtjobtitle" Text='<%# Eval("job_title") %>'></asp:TextBox>
			  </EditItemTemplate>
		</asp:TemplateField>--%>
		<asp:BoundField DataField="name" HeaderText="Company Name" ItemStyle-Width ="18%" />
		
		<asp:BoundField DataField="Category" HeaderText="Job Category" ItemStyle-Width ="18%" />
		<asp:BoundField DataField="description" HeaderText="Job Post Status" ItemStyle-Width ="13%"/>
		<asp:TemplateField HeaderText="Job Applicant" ItemStyle-Width ="11%">
			  <ItemTemplate>
				<%# "Job Opening: " + Eval("amount") %>
				  <br />
				<%# "Job Applied: "+ Eval("applied_count")%>
			  </ItemTemplate>
			  <EditItemTemplate>
				<asp:Label runat="server" ID="lbljobstatus" Text='<%# "Job Opening: " + Eval("amount").ToString() +", Job Applied: "+ Eval("applied_count").ToString()%>'></asp:Label>
			  </EditItemTemplate>
		</asp:TemplateField>
		<asp:ButtonField CommandName="editRecord" ButtonType="Link" Text="Edit" ItemStyle-Width="4%" ControlStyle-CssClass="text-rose">
                                                </asp:ButtonField>
		
		
		<asp:TemplateField ItemStyle-Width="9%">
			<ItemTemplate>
				<asp:CheckBox ID="myCheckBox1" runat="server" Text="Suspended" Checked='<%# Eval("suspend") == DBNull.Value ? false : Convert.ToBoolean(Eval("suspend")) %>'

					 OnCheckedChanged="myCheckBox1_CheckedChanged" AutoPostBack="true"/>
			</ItemTemplate>
		</asp:TemplateField>
		<asp:TemplateField ItemStyle-Width="8%">
			<ItemTemplate>
				<asp:CheckBox ID="myCheckBox2" runat="server" Text="Featured" Checked='<%# Eval("is_feat") == DBNull.Value ? false : Convert.ToBoolean(Eval("is_feat")) %>'

					 OnCheckedChanged="myCheckBox2_CheckedChanged1" AutoPostBack="true"/>
			</ItemTemplate>
		</asp:TemplateField>
	</Columns>  
</asp:GridView>





BindGridView表显示数据列表:



BindGridView table which is to display the list of data:

private void BindGridView()
{
	MySqlConnection con = new MySqlConnection(constr);
	MySqlCommand cmd = new MySqlCommand("SELECT jpw.jpw_id, jpw.post_id, 
	GROUP_CONCAT(jpw.job_title SEPARATOR ', ') as job, " +
	"ub.name,cat.name as 'Category',jpp.description,sum(jpw.amount) as amount, " +   
	"sum(jpw.applied_count) as applied_count, jpw.suspend, jp.is_feat " +
	"FROM xtime.job_post_worker jpw " +
	"left join job_post jp on jpw.post_id = jp.post_id " +
	"left join users_business ub on jp.ub_id = ub.ub_id " +
	"left join job_post_processing jpp on jp.status = jpp.status " +
	"left join category cat on jp.category = cat.cat_id " +
	"GROUP BY jpw.post_id " +
	"ORDER BY jpw.jpw_id ASC;", con);

	MySqlDataAdapter da = new MySqlDataAdapter(cmd);

	DataTable dt = new DataTable();

	da.Fill(dt);
	Gridview1.DataSource = dt;
	Gridview1.DataBind();
}



然后,这是我的gridview页面索引。首先,我尝试了我评论的那个,但我不知道它不工作而其他人正在工作,我尝试了第二个但它也不起作用:


Then, this is my gridview page index. first, i tried the one that i commented but i dont know its not working while others are working and i tried the second one but its not working too:

protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
	//Gridview1.PageIndex = e.NewPageIndex;
	//BindGridView();
	
	Gridview1.PageIndex = e.NewPageIndex;
	Gridview1.SelectedIndex = -1;
	BindGridView();
}





p / s:请帮帮我。完全不知道问题是什么以及如何解决。谢谢:)



p/s: help me please. totally dont know whats the problem and how to sloved. thanks :)

推荐答案

删除
Gridview1.SelectedIndex = -1;


这似乎是使用DataTables和刷新绑定时遇到的常见问题: asp.net gridview不刷新更改网页时 - Google搜索 [ ^ ]



以下是使用上述Google搜索找到的解决方案示例: c# - GridV iew不会改变页面更改 - 堆栈溢出 [ ^ ]。根据这个,你的代码应该是:

This appears to be a common problem experienced when using DataTables and refreshing bindings: asp.net gridview not refreshing when changing pages - Google Search[^]

Here is an example of a solution found using the above Google Search: c# - GridView does not change on page change - Stack Overflow[^]. According to this, your code should be:
protected void Page_Load(object sender, EventArgs e)
{ 
    if (!IsPostBack)
    {
        BindGridView(); 
    }
}

protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    BindGridView(e.NewPageIndex);
}

private void BindGridView(int pageNumber = 0)
{
	MySqlConnection con = new MySqlConnection(constr);
	MySqlCommand cmd = new MySqlCommand("SELECT jpw.jpw_id, jpw.post_id, 
	GROUP_CONCAT(jpw.job_title SEPARATOR ', ') as job, " +
	"ub.name,cat.name as 'Category',jpp.description,sum(jpw.amount) as amount, " +   
	"sum(jpw.applied_count) as applied_count, jpw.suspend, jp.is_feat " +
	"FROM xtime.job_post_worker jpw " +
	"left join job_post jp on jpw.post_id = jp.post_id " +
	"left join users_business ub on jp.ub_id = ub.ub_id " +
	"left join job_post_processing jpp on jp.status = jpp.status " +
	"left join category cat on jp.category = cat.cat_id " +
	"GROUP BY jpw.post_id " +
	"ORDER BY jpw.jpw_id ASC;", con);
 
	MySqlDataAdapter da = new MySqlDataAdapter(cmd);
 
	DataTable dt = new DataTable();
 
	da.Fill(dt);
	
    Gridview1.PageIndex = pageNumber;
	Gridview1.DataSource = dt;
	Gridview1.DataBind();
}



I know that this looks like semantics however indications are that it is the wrong location to call this. This minor change apparently works.



Also, as a side-note, to stop SQL injection attacks[^] you need to use command parameters and not string concatenation for building SQL queries.



UPDATE: Here is a Mock test (as I don’t have your DB):


I know that this looks like semantics however indications are that it is the wrong location to call this. This minor change apparently works.

Also, as a side-note, to stop SQL injection attacks[^] you need to use command parameters and not string concatenation for building SQL queries.

UPDATE: Here is a Mock test (as I don't have your DB):

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <asp:GridView ID="Gridview1" runat="server"

                  OnPageIndexChanging="Gridview1_PageIndexChanging"

                  EnableViewState="true" PageSize="10" AllowPaging="true" />
</asp:Content>



And the Code-behind:


And the Code-behind:

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }

    protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        BindGridView(e.NewPageIndex);
    }

    private void BindGridView(int pageNumber = 0)
    {
        Gridview1.PageIndex = pageNumber;
        Gridview1.DataSource = GetData();
        Gridview1.DataBind();
    }

    private static List<person> GetData()
    {
        // should add caching support
        var persons = new List<person>();
        for (int i = 0; i < 100; i++)
        {
            persons.Add(new person { First =


\"First {i}\", Last =


这篇关于Gridview分页无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆