Click here to Skip to main content
15,888,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
thins is gridview code in my webPage
ASP.NET
<asp:GridView ID="GridView1" runat="server" BorderStyle="None" OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="True">
           
            <Columns>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:CheckBox ID="IdSelector" runat="server" OnCheckedChanged="Button1_Click"/>
                    </ItemTemplate>
                </asp:TemplateField>
               <asp:HyperLinkField DataTextField="title" DataNavigateUrlFields="SubjectId" DataNavigateUrlFormatString="~/Topic/{0}" HeaderText="العنوان"/>
           </Columns>
            
        </asp:GridView>


C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        if (Page.RouteData.Values["Id"] != null /*&& Page.RouteData.Values["index"] != null*/)
        {
            int id;
            if (int.TryParse(Page.RouteData.Values["Id"].ToString(), out id) == true /*&& int.TryParse(Page.RouteData.Values["index"].ToString(), out index) == true*/)
            {
                Adapter = new SqlDataAdapter("SELECT Subjects.SubjectId,Subjects.Title,AgePeriods.Period,Users.UserName From Subjects INNER JOIN AgePeriods ON Subjects.AgeId = AgePeriods.AgePeriodId INNER JOIN Users ON Subjects.UserId = Users.UserId WHERE CId = '" + id + "' ORDER BY Subjects.SubjectId DESC ", con);
                Adapter.Fill(ds, "Subjects");
                GridView1.DataSource = ds.Tables["Subjects"];
                GridView1.DataBind();
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    GridView1.Rows[i].Cells[1].Visible = false;
                    GridView1.Rows[i].Cells[2].Visible = false;
                }
            }
            else
            {
                Response.Redirect("homepage.aspx");
            }

        }

    }

}


C#
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {        
        int newPage = e.NewPageIndex;
        GridView1.DataSource = ds.Tables["Subjects"];
        GridView1.DataBind();
    }

paging is enabled, but the problem that when i add binding code into !postback block
paging does not work, either paging or using !postback, i use !postBack to be able to get selected checked checkboxes. Is there possible way to use paging & !postBack ?
thanks in advance
Posted
Updated 6-May-13 10:19am
v2
Comments
Richard C Bishop 6-May-13 16:11pm    
Your gridview does not show that paging is enabled.
Salah Abualrob 6-May-13 16:20pm    
sorry, i posted it after changing, it's modified now :)
Richard C Bishop 6-May-13 16:22pm    
No worries, why are you even using !Page.IsPostBack? You use that if you want to perform something if the page is not posting back. The gridview cannot get updated on the page unless you post back or do an async post back.
Salah Abualrob 6-May-13 16:25pm    
their is a checkBox in gridView, when i want to get checked ones, it does not work without using !Page.IsPostBack(), i tried many way but useless !
db7uk 6-May-13 17:12pm    
your pageindexchanging does not look right. ds.tables["subjects"]... where is this kept? viewstate, session? at the moment your pageindexchanging just fires a postback and does not bind to the original datasource.

1 solution

Ok, try:

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Page.RouteData.Values["Id"] != null /*&& Page.RouteData.Values["index"] != null*/)
            {
                int id;
                if (int.TryParse(Page.RouteData.Values["Id"].ToString(), out id) == true /*&& int.TryParse(Page.RouteData.Values["index"].ToString(), out index) == true*/)
                {
                    Adapter = new SqlDataAdapter("SELECT Subjects.SubjectId,Subjects.Title,AgePeriods.Period,Users.UserName From Subjects INNER JOIN AgePeriods ON Subjects.AgeId = AgePeriods.AgePeriodId INNER JOIN Users ON Subjects.UserId = Users.UserId WHERE CId = '" + id + "' ORDER BY Subjects.SubjectId DESC ", con);
                    Adapter.Fill(ds, "Subjects");
                    GridView1.DataSource = ds.Tables["Subjects"];
                    GridView1.DataBind();

                    // add data to session to persist data
                    Session["SubjectsData"] = ds.Tables["Subjects"];

                    for (int i = 0; i < GridView1.Rows.Count; i++)
                    {
                        GridView1.Rows[i].Cells[1].Visible = false;
                        GridView1.Rows[i].Cells[2].Visible = false;
                    }
                }
                else
                {
                    Response.Redirect("homepage.aspx");
                }
 
            }
 
        }
        
    }



And then in the page index changing method:

C#
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {       
           // set your page index on the grid. you were missing this. The grid did not know which page to navigate to. 
           GridView1.PageIndex = e.NewPageIndex;

           // get your data back out of session.
           GridView1.DataSource = Session["SubjectsData"];
           GridView1.DataBind();
    }
 
Share this answer
 
Comments
Salah Abualrob 7-May-13 6:28am    
thanks so much with 5 stars :)
db7uk 7-May-13 6:32am    
No Problem! Have a good day!

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



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