Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, 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.NET
<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 table which is to display the list of data:
C#
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();
}

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:
C#
protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
	//Gridview1.PageIndex = e.NewPageIndex;
	//BindGridView();
	
	Gridview1.PageIndex = e.NewPageIndex;
	Gridview1.SelectedIndex = -1;
	BindGridView();
}


p/s: help me please. totally dont know whats the problem and how to sloved. thanks :)
Posted
Updated 12-Sep-17 19:19pm
v5
Comments
Graeme_Grant 12-Sep-17 23:50pm    
"did not work". How? What happened? Were there any errors/exceptions. Have you set a breakpoint to check that the code that you're expecting to run is being called? Are the correct paging parameters being passed? Are the correct parameters being passed to the DB query? What is being returned in the DB Query? etc...

Please be specific as we can not run your code here.

Once you are ready update the question with clear and concise details, sample code, any error messages (including inner exception details), etc, please click on Improve question widget to add more info to the question.
Elaine94 12-Sep-17 23:53pm    
i already updated the question, and the sample code i already provided in what i have try. thanks
Graeme_Grant 12-Sep-17 23:56pm    
There are many questions asked above. Please take the time to read them and respond here or updated in your question (preferred).
Elaine94 12-Sep-17 23:58pm    
i updated the question. i added the aspx coding.
Graeme_Grant 13-Sep-17 0:04am    
More information is required. The questions are:
1. What happened? Were there any errors/exceptions.
2. Have you set a breakpoint to check that the code that you're expecting to run is being called?
3. Are the correct paging parameters being passed from the ASPX page to the code-behind?
4. Are the correct parameters being passed to the DB query?
5. What is being returned in the DB Query? 

Each question is addressing each step of the process. This is called debugging. Please take the time to work through these questions and you will find your answer.

try after remove
Gridview1.SelectedIndex = -1;
 
Share this answer
 
Comments
Elaine94 13-Sep-17 1:30am    
if i remove that, still not going to second page.
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:
C#
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):
ASP.NET
<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:
C#
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 = $"Last {i}", Age = i });
        }

        return persons;
    }
}

class person
{
    public string First { get; set; }
    public string Last { get; set; }
    public int Age { get; set; }
}

Works as expected.
 
Share this answer
 
v3
Comments
Elaine94 13-Sep-17 1:29am    
emm, if i run this coding, it will not display the table. my page become blank.
Graeme_Grant 13-Sep-17 1:31am    
Are you calling BindGridView from somewhere else?
Elaine94 13-Sep-17 1:32am    
i call it in page load.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
Graeme_Grant 13-Sep-17 1:38am    
Check update to solution.
Elaine94 13-Sep-17 1:45am    
emm, still cannot :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900