asp.net - Pagination Not Working On Gridview -
i have gridview control
contains label template field. label values filled during rowdatabound. when pagination except template field , paginated. think, when pagination, each time label template field filled on rowdatabound
.
now not able pagination
. please help
this code
<asp:updatepanel id="updatepanel1" runat="server"> <contenttemplate> <asp:gridview id="gridview1" runat="server" allowpaging="true" backcolor="white" bordercolor="#cccccc" borderstyle="none" borderwidth="1px" cellpadding="3" ondatabound="gridview1_databound" onpageindexchanging="gridview1_pageindexchanging1" onrowdatabound="gridview1_rowdatabound1" pagesize="3"> <columns> <asp:templatefield headertext="first row"> <itemtemplate> <asp:label id="label1" runat="server">text</asp:label> </itemtemplate> </asp:templatefield> </columns> <footerstyle backcolor="white" forecolor="#000066" /> <headerstyle backcolor="#006699" font-bold="true" forecolor="white" /> <pagerstyle backcolor="white" forecolor="#000066" horizontalalign="left" /> <rowstyle forecolor="#000066" /> <selectedrowstyle backcolor="#669999" font-bold="true" forecolor="white" /> <sortedascendingcellstyle backcolor="#f1f1f1" /> <sortedascendingheaderstyle backcolor="#007dbb" /> <sorteddescendingcellstyle backcolor="#cac9c9" /> <sorteddescendingheaderstyle backcolor="#00547e" /> </asp:gridview> </contenttemplate> </asp:updatepanel>
gridview.cs
protected void page_load(object sender, eventargs e) { getdata(); } public void getdata() { sqlconnection con = new sqlconnection("data source=cmh-sosql\\sq1;initial catalog=rpt2020_dev;integrated security=true"); con.open(); string qry = "use pid2020_dev select * batch_void_rsn_cd"; sqldataadapter da = new sqldataadapter(qry, con); datatable dt = new datatable(); da.fill(dt); gridview1.datasource = dt; gridview1.databind(); } protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { gridview1.columns[0].headertext = "first row"; label lblkey = (label)e.row.findcontrol("label1"); lblkey.text = e.row.rowindex.tostring(); } } protected void gridview1_databound(object sender, eventargs e) { } protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e) { gridview1.pageindex = e.newpageindex; getdata(); }
try below code
code behind file
using system; namespace asp.netforums.gridview { public partial class gridviewtemp : system.web.ui.page { protected void page_load(object sender, eventargs e) { try { if (!page.ispostback) { getdata(); viewstate["initialpage"] = 3;//page count } } catch (exception) { } } public void getdata() { sqlconnection sqlcon = new sqlconnection(configurationmanager.connectionstrings["northwindconstring"].tostring()); string query = "select * products"; dataset ds = new dataset(); sqldataadapter sqladp = new sqldataadapter(query, sqlcon); sqladp.fill(ds); gridview1.datasource = ds; gridview1.databind(); } protected void gridview1_rowdatabound(object sender, gridviewroweventargs e) { int count = 0; if (e.row.rowtype == datacontrolrowtype.datarow) { if (viewstate["count"] != null) { count = convert.toint32(viewstate["count"]); } if (viewstate["initialpage"] != null) { count = (int)viewstate["initialpage"]; } gridview1.columns[0].headertext = "first row"; label lblkey = (label)e.row.findcontrol("label1"); lblkey.text = count.tostring(); count--; if (viewstate["initialpage"] != null) { viewstate["initialpage"] = count; } else { viewstate["count"] = count; } } } protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e) { gridview1.pageindex = e.newpageindex; viewstate["count"] = e.newpageindex; viewstate["initialpage"] = null; getdata(); } }
}
use viewstate solve problem.
hopefully, answers question
Comments
Post a Comment