Gridview PageIndexChanging event not firing - asp.net

I hav a gridview inside a usercontrol
<asp:GridView ID="grdMissingFilterData" runat="server" AllowPaging="True" Width="100%"
AllowSorting="True" AutoGenerateColumns="False" GridLines="None"
PageSize="30" OnPageIndexChanging="grdMissingFilterData_PageIndexChanging">
<Columns>
<asp:BoundField DataField="Varenummer" HeaderText="Varenummer" ItemStyle-Width="25%" >
<ItemStyle Width="25%" />
</asp:BoundField>
<asp:BoundField DataField="Varenavn" HeaderText="Varenavn" ItemStyle-Width="15%" >
<ItemStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="Producentvarenummer" HeaderText="Producent varenummer" ItemStyle-Width="15%" >
<ItemStyle Width="15%" />
</asp:BoundField>
</Columns>
<AlternatingRowStyle CssClass="altrow" />
<PagerSettings FirstPageText="First" LastPageText="Last" PageButtonCount="50" />
<EmptyDataTemplate>
There is no data available to display!
</EmptyDataTemplate>
<PagerStyle CssClass="pager" />
</asp:GridView>
and code in postback
if (!Page.IsPostBack)
{
BindData();
}
then i had a PageIndexChanging event which never fires when i click on paging.
protected void grdMissingFilterData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdMissingFilterData.PageIndex = e.NewPageIndex;
BindData();
}
can any one give me any possible reasons?

This may be probably an issue with your user control not about gridview.
Focus on that part

Related

Remove Empty row in gridview which is added automatically at the bottom of gridview

I am using grid view to show records from database. Gridview has pagination and pagesize is set as 15. And one column has link field when user click this link it proceed some functionality. Now what is my problem is
1) when number of records less than page size the last page is
shrunk. it's k for me. but on clicking view link in grid view the
empty row added automatically. how do i remove these empty row?
2) on clicking view link the page index changed to 1. example i am in
3 page of grid view and clicking view link in 3rd page the page index
number changed to 1 but the page shows 3rd row record. how do i fix
this..?
please any can help me.. thanks in advance
grid view code:-
<asp:GridView ID="gvGRNListAll" runat="server" AutoGenerateColumns="False" Style="width: 100%;"
AllowPaging="True" PageSize="15" OnPageIndexChanging="gvGRNListAll_PageIndexChanging"
CellPadding="4" ForeColor="#333333" BorderColor="#CCCCCC" ShowHeaderWhenEmpty="True"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<PagerStyle CssClass="pager" />
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="10%" />
</asp:TemplateField>
<asp:BoundField DataField="LocationName" HeaderText="Location" />
<asp:BoundField DataField="SupplierName" HeaderText="Supplier Name" />
<asp:BoundField DataField="GRNNo" HeaderText="GRN No" />
<asp:BoundField DataField="InvoiceNo" HeaderText="In.No">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="GRNDate" HeaderText="GRNDate" />
<asp:TemplateField HeaderText="Action" SortExpression="ExId" ControlStyle-ForeColor="Blue">
<ItemTemplate>
<asp:HiddenField ID="hdnStoreCode" runat="server" Value='<%# Eval("StoreCode") %>' />
<asp:HiddenField ID="hdnGRNNo" runat="server" Value='<%# Eval("GRNNo") %>' />
<asp:LinkButton ID="lbView" OnClick="lbView_Click" runat="server" Text="View"></asp:LinkButton>
</ItemTemplate>
<ControlStyle ForeColor="#FF3300" />
<HeaderStyle CssClass="GridHeaderROW" Width="10%" />
<ItemStyle CssClass="GridItemROW" Width="10%" />
</asp:TemplateField>
</Columns>
<EmptyDataRowStyle HorizontalAlign="Center" VerticalAlign="Bottom" ForeColor="#FF3300" />
<EmptyDataTemplate>
No Records Found.
</EmptyDataTemplate>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
On PageIndexChanged
protected void gvGRNListAll_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvGRNListAll.PageIndex = e.NewPageIndex;
SearchData();
gvGRNListAll.PageIndex = 0;
}
Searchdata Code :
public void SearchData()
{
DataTable dtGRN = objBAL.FilterGRNScannedList("filterGRNScannedList", suppliercode, grnno, locationcode, Fromdate, Todate, "");
gvGRNListAll.DataSource = dtGRN;
gvGRNListAll.DataBind();
}
Onclick event Code :
if (sender is LinkButton)
{
GridViewRow gvrCurrent = ((LinkButton)sender).NamingContainer as GridViewRow;
hdnGRNNo = (HiddenField)gvrCurrent.FindControl("hdnGRNNo");
hdnStore = (HiddenField)gvrCurrent.FindControl("hdnStoreCode");
gvGRNListAll.Rows[gvrCurrent.RowIndex].BackColor = System.Drawing.Color.Empty;
gvGRNListAll.Rows[gvrCurrent.RowIndex].BackColor = System.Drawing.ColorTranslator.FromHtml("#D8D8D8");
if (hdnGRNNo != null && hdnStore != null)
{
GetGRNListForNo(hdnGRNNo.Value, Convert.ToInt32(hdnStore.Value));
}
}
on RowDataBound do something like below
private void gvGRNListAll_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[6].Text == "") // here add the cell no at which the row is coming blank.
e.Row.Visible = false;
}
Hope it helps

Paging GridView Issues

I just read some articles about paging gridview but I couldn't make it...
There is my full gridview's code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="font-family: Verdana, Arial, Sans-Serif;"
CssClass="gridview" OnSorting="GridView_Sorting"
DataKeyNames="id"
AllowSorting ="True" BackColor="#CCCCCC"
BorderStyle="Inset" BorderWidth="2px" BorderColor="GrayText"
CellPadding="1"
CellSpacing="5"
HeaderStyle-HorizontalAlign="Center"
OnRowDataBound="GridView1_RowDataBound"
ForeColor = "Black" RowStyle-CssClass="gridview"
OnRowCommand="GridView1_RowCommand" AllowPaging="True"
OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5">
<AlternatingRowStyle BackColor="#CCCCCC" />
<columns>
<asp:BoundField HeaderText="ID" DataField="id" />
<asp:BoundField HeaderText="PRIORIDADE" DataField="prioridade"
ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="SITUAÇÃO" DataField="situacao" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="RESPONSAVEL" DataField="responsavel" HeaderStyle-Width="65px" ItemStyle-HorizontalAlign="Center">
<HeaderStyle Width="65px" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="DATA DE CADASTRO" DataField="dt_cadastro" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px" SortExpression="dt_cadastro"
ItemStyle-HorizontalAlign="Center" >
<HeaderStyle Width="60px" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="PREVISÃO DE TÉRMINO" DataField="previsao_termino" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px"
ItemStyle-HorizontalAlign="Center">
<HeaderStyle Width="60px" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="PROJETO" DataField="projeto" ItemStyle-HorizontalAlign="Center">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="FUNCIONALIDADE" DataField="funcionalidade"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="CLUBE" DataField="clube"
ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderStyle-Width="70px" HeaderText="VISUALIZAR" >
<ItemTemplate>
<asp:Button ID="Btn_Visualizar" runat="server" Text="VISUALIZAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana, Arial" OnClick="Btn_Visualizar_Click"
CommandName="visualizar" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
</ItemTemplate>
<HeaderStyle Width="70px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="66px" HeaderText="ALTERAR">
<ItemTemplate>
<asp:Button ID="Btn_Alterar" runat="server" Text="ALTERAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana, Arial"
CommandName="editar" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
</ItemTemplate>
<HeaderStyle Width="66px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="66px" HeaderText="FEEDBACK">
<ItemTemplate>
<asp:Button ID="Btn_Feedback" runat="server" Text="ADICIONAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana,Arial"
CommandName="feedback" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
</ItemTemplate>
<HeaderStyle Width="66px" />
</asp:TemplateField>
</columns>
<EditRowStyle ForeColor="Black" CssClass="GridViewEditRow" />
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" BorderColor="White" BorderStyle="Solid" BorderWidth="1px" />
<PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="5" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:GridView>
CodeBehind:
public partial class TodosChamados : System.Web.UI.Page
{
BDUsuarios usr = new BDUsuarios();
BDFuncionalidades func = new BDFuncionalidades();
BDChamados ch = new BDChamados();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = ch.BuscaTodosChamados();
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].Text == "ALTA")
{
e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
e.Row.ControlStyle.Font.Bold = true;
}
}
if (e.Row.Cells[0].Visible = e.Row.RowType == DataControlRowType.Pager)
{
e.Row.Cells[0].Visible = false;
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName != "Sort")
{
if (e.CommandName == "visualizar")
{
Session["id"] = GridView1.Rows[Convert.ToInt32(e.CommandArgument.ToString())].Cells[0].Text;
Session["editar"] = null;
}
else if (e.CommandName == "editar")
{
Session["id"] = GridView1.Rows[Convert.ToInt32(e.CommandArgument.ToString())].Cells[0].Text; //Grava o ID do chamado da linha correspondente.
Session["editar"] = 1; // 1 - Editar 2 - Não Editar .
}
else if (e.CommandName == "feedback")
{
Session["id"] = GridView1.Rows[Convert.ToInt32(e.CommandArgument.ToString())].Cells[0].Text; //Grava o ID do chamado da linha correspondente.
Response.Redirect("~/Adm/Feedback.aspx");
}
Response.Redirect("~/Adm/DetalhesChamado.aspx");
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = ch.BuscaTodosChamados();
GridView1.DataBind();
}
Update 2
Now i hopefully have found the reason for your pager not being shown. You are making the first colum invisible, even for the pager-row in RowDataBound, but the pager is sitting in the first cell by default:
e.Row.Cells[0].Visible = false;
You have to check the correct RowType:
e.Row.Cells[0].Visible = e.Row.RowType == DataControlRowType.Pager;
or even better on the aspx markup:
<asp:BoundField HeaderText="ID" DataField="id" Visible="False" />
A silly question, have you set AllowPaging to true?
<asp:GridView Id="GridView1" runat="server" AllowPaging="True" >
<PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="5" />
</asp:gridview>
GridView.AllowPaging Property
true if the paging feature is enabled; otherwise, false. The default is false.
You should also databind the GridView only if(!IsPostBack) when viwstate is enabled(default).
So wrap your databind code from Page_Load(i guess) into this check:
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
GridView1.DataSource = ch.BuscaTodosChamados();
GridView1.DataBind();
}
}
Otherwise events won't be triggered and changes will be overwritten when you reload data on postback.
Update According to your last edit:
I assume that your RowCommand is the reason for this issue. It is called before the PageIndexChanging event and it is triggered also when the page changes(if i remember correctly). So have a look at your Response.Redirect which happens when no if/else-if will catch it which seems to be the case.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName != "Sort")
{
if (e.CommandName == "visualizar")
{
// ..
}
else if (e.CommandName == "editar")
{
//..
}
else if (e.CommandName == "feedback")
{
//..
}
Response.Redirect("~/Adm/DetalhesChamado.aspx");
}
}
You must specify the event for OnPageIndexChanging in your .aspx. It should be:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
OnPageIndexChanging = "GridView1_PageIndexChanging"
AutoGenerateColumns="False" DataKeyNames="LastName,FirstName"
EnablePersistedSelection="True" SelectedRowStyle-BackColor="Yellow"
DataSourceID="SqlDataSource1" AllowSorting="True">
You might be having the same problem here
UPDATE
Make sure that the data that's being bound to the GridView is more that 5 since PageSize="5". If the number of rows is less than 5, then the pager will not be shown.

How to call a form from a gridview button click?

I am using a Gridview in my application which contains button field as below.
<asp:GridView ID="grvAccrualData" runat="server" AutoGenerateColumns="False" ForeColor="Black" Font-Names="Arial"
BackColor="#B10633" __designer:wfdid="w9" AllowPaging="True" Width="100%" OnRowCommand="grvAccrualData_RowCommand">
<PagerSettings FirstPageText="&lt;&lt; First" LastPageText="Last &gt;&gt;"
Mode="NextPreviousFirstLast" NextPageText="Next &gt;" PreviousPageText="Prev &lt;">
</PagerSettings>
<FooterStyle BackColor="#B10633" Font-Bold="True" ForeColor="White"></FooterStyle>
<RowStyle Font-Names="Arial" Font-Size="8pt" HorizontalAlign="Left" VerticalAlign="Middle" BackColor="Ivory" />
<Columns>
<asp:BoundField DataField="AccrlHeaderAccrualNo" HeaderText="Accrual Number">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle"></ItemStyle>
</asp:BoundField>
<asp:ButtonField ButtonType="button" CommandName="AccrualDetail" HeaderText="Debit Generation" Text="Account" />
</Columns>
<PagerStyle HorizontalAlign="Center" BackColor="#B10633" ForeColor="Black"></PagerStyle>
<HeaderStyle BackColor="#B10633" Font-Names="Arial" Font-Size="8pt" ForeColor="White">
</HeaderStyle>
<AlternatingRowStyle BackColor="#FFC0C0"></AlternatingRowStyle>
</asp:GridView>
And my server side code is :
protected void grvAccrualData_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AccrualDetail")
{
}
}
In this server code I want to call a form or a webpage on button click.
Use
Response.Redirect("~/WebPage.aspx");
Or if you want to pass parameter along with this means then use QueryString.

GridView, SortedAscendingCellStyle server side databinding

I try to use SortedAscendingCellStyle to assign the style of my gridView this is my ASP Code:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" CellPadding="4" OnSorting="GridView1_Sorting" AutoGenerateColumns="false">
<SortedAscendingCellStyle CssClass="SortedAscendingCellStyle" />
<SortedAscendingHeaderStyle CssClass="SortedAscendingHeaderStyle" />
<SortedDescendingCellStyle CssClass="SortedDescendingCellStyle" />
<SortedDescendingHeaderStyle CssClass="SortedDescendingHeaderStyle " />
<Columns>
<asp:BoundField DataField="Nom" HeaderText="Nom" SortExpression="NOM">
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Prenom" HeaderText="Prenom">
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Pseudo" HeaderText="Pseudo"></asp:BoundField>
<asp:BoundField DataField="Mail" HeaderText="Mail">
<ItemStyle Width="140px" />
</asp:BoundField>
</Columns>
</asp:GridView>
there is no problem when i use DataSourceID, but when I use server side databining the SortedAscendingCellStyle Did Not work, this my code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
EmployeDAL oem = new EmployeDAL();
this.GridView1.DataSource = oem.GetAll();
GridView1.DataBind();
}
}
From my research it appears that you cannot use these nice attributes you when manually bind your GridView in the code-behind. You can find another discussion on this topic here:
http://forums.asp.net/t/1725183.aspx/1
...and also here...
http://www.aarongoldenthal.com/post/2009/04/19/Manually-Databinding-a-GridView.aspx
Good luck! I'm trying to figure out a solution to the same problem..

gridview postback not being posted - VS2008

This is my aspx:
<asp:UpdatePanel ID="resultPanel" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="AddDocument" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="False" EnableSortingAndPagingCallbacks="True"
AllowPaging="True" DataSourceID="FilesObjectDataSource" PageSize="5" OnRowCommand="gridView_RowCommand"
DataKeyNames="FileGuid" HorizontalAlign="Left" Width="100%" BorderStyle="Solid"
BorderColor="Black">
<Columns>
<asp:BoundField DataField="RID" HeaderText="ID" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="Category" HeaderText="SubCategory" ReadOnly="True">
</asp:BoundField>
<asp:BoundField DataField="FileTypeName" HeaderText="Type" ReadOnly="True">
</asp:BoundField>
<asp:BoundField DataField="FileGUID" Visible="false" />
<asp:ButtonField Text="X" ButtonType="Button" ItemStyle-Width="20px" CommandName="DelFile">
<ItemStyle Width="20px" />
</asp:ButtonField>
</Columns>
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
</asp:GridView>
</ContentTemplate>
This is my code behind for the ButtonField marked 'X'
protected void gridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DelFile")
{
//Selected Row
int rowIndex = Convert.ToInt32(e.CommandArgument);
fileGuid = new Guid(gridView.DataKeys[rowIndex].Values["FileGuid"].ToString());
}
}
Sometimes itt results in object ref not set and at times it works. Not sure why this happens in production and not in dev and testing.
I'm not sure if it makes any difference, but but the case in ("FileGuid")
DataKeyNames="FileGuid"
and
fileGuid = new Guid(gridView.DataKeys[rowIndex].Values["FileGuid"].ToString());
doesn't match ("FileGUID")
<asp:BoundField DataField="FileGUID" Visible="false" />
You can use like this for accessing value at row command event
protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DelFile")
{
int index = Convert.ToInt32(e.CommandArgument);
int documentID = Convert.ToInt32(gridView.DataKeys[index].Value);
// Write your further code
}
}

Resources