EDIT2: Ok it got better. You can make it work with AllowCustomPaging="true"
but the property VirtualItemCount
must have to be greater than the PageSize
value of your grid view. Look at this:
<asp:GridView runat="server" ID="gvPresupuestos" AutoGenerateColumns="False"
OnRowCommand="gvPresupuestos_RowCommand"
OnPageIndexChanging="gvPresupuestos_PageIndexChanging"
OnPreRender="gvPresupuestos_PreRender" ItemType="Core.NominaEntities.TipoPresupuesto"
AllowPaging="true" PageSize="1" AllowCustomPaging="true"
UseAccessibleHeader="true" GridLines="None" CssClass="table table-hover"
ShowHeaderWhenEmpty="True">
<Columns>
<asp:BoundField HeaderText="Nombre del presupuesto" DataField="Nombre" />
<asp:TemplateField HeaderText="Opciones">
<ItemTemplate>
<a href='<%# "Detalle?p=" + Item.IdTipoPresupuesto.ToString() %>'
target="_self"><%# Item.Editable ? "Editar" : "Ver" %></a>
<asp:LinkButton runat="server"
CommandArgument='<%# Item.IdTipoPresupuesto.ToString() %>'
CommandName="Deshabilitar"
Text="Deshabilitar" Visible='<%# !Item.Editable ? true : false %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
FillView() method:
private void FillView(int desiredPage)
{
var cliente = ClientFactory.CreateAuthServiceClient();
using (cliente as IDisposable)
{
// this list only has 1 item.
List<TipoPresupuesto> resultado = cliente.ObtenerTiposDePresupuesto();
gvPresupuestos.DataSource = resultado;
// For testing pursposes, force the virtual item count to 1000.
gvPresupuestos.VirtualItemCount = 1000;
gvPresupuestos.DataBind();
}
}
So, the LinkButton does not fire the gvPresupuestos_RowCommand
event unless the virtual item count is greater than the VirtualItemCount
property.
I hope this helps.
EDIT1: I found it. You can make it work by changing the value of "AllowCustomPaging"
to false. I don't know why, but hope this can help.
ORIGINAL 'ANSWER':
I have the same problem. You can see the my code here:
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<div class="jumbotron jumbotron-small">
<h2>Presupuestos</h2>
</div>
<asp:GridView runat="server" ID="gvPresupuestos"
AutoGenerateColumns="False" AllowPaging="true" AllowCustomPaging="true" PageSize="20"
OnRowCommand="gvPresupuestos_RowCommand"
OnPageIndexChanging="gvPresupuestos_PageIndexChanging"
DataKeyNames="IdTipoPresupuesto" OnPreRender="gvPresupuestos_PreRender"
ItemType="Core.NominaEntities.TipoPresupuesto" ShowHeaderWhenEmpty="True"
UseAccessibleHeader="true" GridLines="None" CssClass="table table-hover">
<Columns>
<asp:BoundField HeaderText="Nombre del presupuesto" DataField="Nombre"/>
<asp:TemplateField HeaderText="Opciones">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Deshabilitar"
Font-Underline="true" CommandName="Deshabilitar"
Visible='<%# Item.Editable ? true : false %>'
CommandArgument='<%# Item.IdTipoPresupuesto %>' />
<a href='<%# "Detalle?p=" + Item.IdTipoPresupuesto.ToString() %>'
target="_self"><%# Item.Editable ? "Editar" : "Ver" %></a>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Link" CommandName="Deshabilitar"
Text="Deshabilitar (this fires event)" />
</Columns>
</asp:GridView>
<table class="large-table">
<tr>
<td class="text-right">
<a runat="server" class="btn btn-primary" href="Detalle">Nuevo presupuesto</a>
</td>
</tr>
</table>
</asp:Content>
Code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitializeControls();
SetPermissions();
FillView(1);
}
}
protected void gvPresupuestos_PreRender(object sender, EventArgs e)
{
// Habilita bootstrap
gvPresupuestos.HeaderRow.TableSection = TableRowSection.TableHeader;
}
protected void gvPresupuestos_RowCommand(object sender, GridViewCommandEventArgs e)
{
// This is never fired by the LinkButton in the ItemTemplate
// But the ButtonField actually fires it.
if (e.CommandName.Equals("Deshabilitar"))
{
// This is how I've been doing it in the whole project but thanks to this
// shit I can't use the CommandArgument of the LinkButton in the ItemTemplate
// Guid idPresupuesto = new Guid(e.CommandArgument.ToString());
// I don't know what I'm supposed to do now.
// ¿Why this only happens in this page?, ¿WTF?
Guid idPresupuesto = gvPresupuestos.GetTheIdFromDataKeysWithFuckingMagic();
var cliente = ClientFactory.CreateServiceClient();
using (cliente as IDisposable)
{
cliente.DeshabilitarPresupuesto(idPresupuesto);
}
}
FillView(1);
}
protected void gvPresupuestos_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvPresupuestos.PageIndex = e.NewPageIndex;
FillView(e.NewPageIndex + 1);
}
#region helper methods
private void InitializeControls()
{
// Not required yet
}
private void FillView(int desiredPage)
{
var cliente = ClientFactory.CreateAuthServiceClient();
using (cliente as IDisposable)
{
var resultado = cliente.ObtenerTiposDePresupuesto();
gvPresupuestos.DataSource = resultado;
gvPresupuestos.DataBind();
}
}
private void SetPermissions()
{
// not required yet
}
#endregion
I've tried enabling and disabling viewstate in the gridview but didn't get different results.
Here is what happens with breakpoints:
- You type the url and load the page for the first time.
- Then you access the code in the block if(!IsPostBack).
- Now you can click both the LinkButton in the ItemTemplate and the ButtonField(Look screenshot)
- If you click the LinkButton in the ItemTemplate, it doesn't fire gvPresupuestos_RowCommand. It does a postback, of course. But gvPresupuestos_RowCommand simply doesn't get fired. My code doens't handle that weird postback and the page does the things it would do with a regular postback. After it, you end up with an empty gridview because in the flow of this postback, FillView() is never called.(Look screenshot)
- If you click the ButtonField, the event gvPresupuestos_RowCommand is fired, and everything is normal. But I require the IdPresupuesto of the clicked row and I don't know how to get it now.
This is weird. I've implemented this model in all the info pages of the system (there are like 15 modules like this) and never had problems until now.