LinkButton's not firing GridView_RowCommand - c#

I have a LinkButton inside a GridView that passes a CommandArgument and tries to fire RowCommand
GridView declaration:
<asp:GridView PageIndex="0" OnRowCommand="GridView1_RowCommand" PageSize="10" AllowPaging="true" OnPageIndexChanging="GridView1_PageIndexChanging"
ID="GridView1" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" Width="700px"
AutoGenerateColumns="False" OnPageIndexChanged="GridView1_PageIndexChanged">
LinkButton inside the GridView:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" OnClientClick="return confirm('Are you sure you want to delete this checklist?');" runat="server" CausesValidation="false" CommandName="DeleteChecklist" CommandArgument='<%# Eval("refID") %>' Text="Delete"></asp:LinkButton>
</ItemTemplate>
<ItemStyle ForeColor="Black" />
<ControlStyle BorderStyle="None" ForeColor="Black" />
</asp:TemplateField>
Code behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DeleteChecklist")
{
string refID = e.CommandArgument.ToString();
DAL.DeleteChecklist(refID);
}
}
I placed a breakpoint at RowCommand but it doesn't get fired at all, any idea why?

I assume that you're databinding the GridView on postbacks. So always embed it in a if(!IsPostBack) check:
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
GridView1.DataSource = getSource();
GridView1.DataBind();
}
}
Otherwise events won't be triggered and edited values will be overridden.
Another possible reason: Have you disabled ViewState?

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.

I had the same problem for a LinkButton in a ListView.
Turns out I should have been using OnItemCommand in the ListView, not OnRowCommand.
Changing this fixed the issue.
Also, you must have a CommandName set on the LinkButton (but no OnCommand).

Related

Image button on gridview not triggering asp.net

so i have a gridview with an imagebutton, thing is that it never triggers. I dont know why. here is the code.
<asp:GridView ID="GridView1" runat="server" Width="233px"
OnCommand="GridView1_RowCommand" onrowcommand="GridView1_RowCommand"
DataKeyNames="Nombre">
<Columns>
<asp:TemplateField HeaderText="" ItemStyle-Width="12%" ItemStyle-
HorizontalAlign="Center" >
<ItemTemplate>
<asp:ImageButton ID="lnkEditar" runat="server" CommandName="Edit"
ImageUrl="Imagenes/edit.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and the code behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
int index = Convert.ToInt32(e.CommandArgument);
Label1.Text = GridView1.Rows[index].Cells[2].ToString();
}
}
I debugged it and it never triggers the RowCommand event. Any help is thanked!
Try to change as follow OnRowCommand
OnRowCommand="GridView1_RowCommand"

How to know in which row a button is clicked in asp.net gridView

This is a simple gridView I m using in aspx web page
<asp:GridView ID="Order" runat="server" AutoGenerateColumns="False" ShowFooter="True" GridLines="none"
ItemType="MyProject.Models.TempList" SelectMethod="GetList" >
<Columns>
<asp:BoundField DataField="ItemID" HeaderText="ID" SortExpression="ItemID" />
<asp:TemplateField HeaderText="ItemName">
<ItemTemplate>
<asp:Label runat="server" ID="ItemName" Width="40" Visible="true" text="<%#: Item.Item.ItemName %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price(each)">
<ItemTemplate>
<%#: String.Format("{0:c}", Convert.ToDouble(Item.Item.UnitPrice))%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<div style="float:left">
<asp:Button ID="DeleteBtn" runat="server" Text="Delete" OnClick="DeleteBtn_Click"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have 2+ items in a list<> which is populating the gridView, how can I tell (in the codeBehind.cs) that which Row the "DeleteBtn" was clicked on?
I was using a for loop to iterate every item in gridView using Rows[i] and used a check box to know which item wants to be deleted using a Update button.
But I want to do it directly on a custom created deletebutton.
Thanks in advance, I know I'm missing something silly.
The best way to do that is using CommandName and CommandArgument in your button declaration like that
<asp:Button ID="AddButton" runat="server" CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="Add to Cart" />
you can pass any value in the case we pass the rowIndex, you can get a propertie from your object like that CommandArgument="<%# Eval("id") %>" after that you will handle the onRowCommand method
protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here to add the item to the shopping cart.
}
}
hope it helps :)
Use the button's CommandArgument property; assign Container.DataItemIndex to it. Then use the OnRowCommand event of the gridview and grab the index.
Sample aspx:
<asp:Label runat="server" ID="lblMsg" />
<asp:GridView runat="server" id="gvSample" AutoGenerateColumns="false" OnRowCommand="PerformOperation">
<Columns>
<asp:BoundField DataField="RowValue"/>
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Delete" runat="server" CommandName="MyCustomCommand" CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateGrid();
}
}
private void PopulateGrid()
{
gvSample.DataSource = Enumerable.Range(0, 10).Select(i => new { RowValue = i });
gvSample.DataBind();
}
protected void PerformOperation(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "MyCustomCommand")
{
var rowIndex = int.Parse(e.CommandArgument);
lblMsg.Text = string.Format("Button on row index: {0} was clicked!", rowIndex);
}
}
Let the button field be given a CommandName, say for example a unique string myCommandName in the Edit columns dialog box. Don't worry about the CommandArgument. Then, in the Grid view Row command event, you can check which button (i.e. column) is clicked by tracing the command name like if e.commandname = "mycommandname", at the same time the CommandArgument will also be available as String and all we have to do is to converttoint32, something like intSelectedRow = convert.ToInt32(e.CommandArgument) which will give us the selected row's index.

Function isn't called on onclick event?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="245px" onselectedindexchanged="GridView1_SelectedIndexChanged" >
<Columns>
<asp:TemplateField>
<ItemStyle BackColor="#CCCCCC" ForeColor="Black" Width="250px" HorizontalAlign="Center" BorderStyle="None" />
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("users") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I don't want to use the auto generated select link or any button, I just want the label (or the link) itself to fire the selectedIndexChanged event.
The C# code is :-
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
show_chats();
}
But the event method is not being called. Please suggest what to do
<a onclick="..."> is strictly HTML markup, and the onclick is looking for a javascript function called GridView1_SelectedIndexChanged, not a server side method. Change this to:
<asp:LinkButton runat="server" OnClick="GridView1_SelectedIndexChanged">
If you dont want to use linkbutton, alternatively, you can do as follows-
<asp:Label ID="Label5" runat="server" Text='<%# Bind("users") %>'>
And code behind
protected void Page_Load(object sender, EventArgs e)
{
yourlinkId.ServerClick += new EventHandler(GridView1_SelectedIndexChanged);
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
show_chats();
}

how to get control id from gridview in asp.net?

I need control id from grid view to use Trigger.
My code is here :
<asp:GridView ID="gvDetails" CssClass="table table-striped table-bordered datatables dataTable" DataKeyNames="folder_path" CellPadding="5" runat="server" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="attachment_name" HeaderText="Attachment" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="Download" runat="server" OnClick="DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-CssClass="hideGridColumn" ItemStyle-CssClass="hideGridColumn">
<ItemTemplate>
<asp:HiddenField ID="hdnAttach_Id" Value='<%#(Eval("attachment_id").ToString())%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#2FBDF1" Font-Bold="true" />
</asp:GridView>
I need code like...
<Triggers>
<asp:PostBackTrigger ControlID="lnkDownload" />
</Triggers>
how to get "lnkDownload" id from gridview?
Exception :
You need to register each and every LinkButton as an PostBackTrigger. After each row is bound in your GridView, you'll need to search for the LinkButton and register it through code as follows:
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton lb = e.Row.FindControl("lnkDownload") as LinkButton;
ScriptManager.GetCurrent(this).RegisterPostBackControl(lb);
}
And need to call this on RowDataBoundevent.
You can access the controls from the grid view using the findcontrol method
foreach(GridViewRow row in gvDetails.Rows)
{
if(row.RowType == DataControlRowType.DataRow)
{
LinkButton linkButton = (LinkButton )row.FindControl("lnkDownload");
//Your other code
}
}
I've recently encountered this problem as well.
You can find a control by doing e.row.findcontrol("NameOfControl").
Since I don't fully know what you intend to do, you can retrieve the ClientID by finding the control. Then you can specify the ID of the button by saying Button.ClientID. This will display the ContentHolder1_gridview_0_button_0.
To add attributes, you can do button.Attributes.Add("attribute", "#" + button.ClientID);
The following code is something I've fixed to have the following attribute added to the button. This is so that I can click a button and copy the textbox.
Example:
protected void gvListInventoryPassword_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HtmlButton buttonPass= (HtmlButton)e.Row.FindControl("buttonPass");
TextBox txtBox= (TextBox)e.Row.FindControl("txtBox");
buttonPass.Attributes.Add("data-clipboard-target", "#" + txtBox.ClientID);
}
}//End of gvListInventoryPassword_RowDataBound function

On button Click in the Grid I can't get in to the function

Using CSharp, I'm not getting into the (GridView1_RowDeleting) function on the button click.. I don't know whats the problem is but wasted alot of time on it.
Default.aspx
asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting"
AllowPaging="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="img1" runat="server" CommandName="GridView1_RowDeleting" ImageUrl="~/Images/cross.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Default.aspx.cs
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
Use OnItemCommand:
protected void Test(object sender,DataGridEventArgs e)
{
if(e.CommandName == "GridView1_RowDeleting")
{
// do something
}
}
<asp:GridView ID="GridView1" runat="server" OnItemCommand="Test" AllowPaging="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="img1"
runat="server"
CommandName="GridView1_RowDeleting"
ImageUrl="~/Images/cross.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have to use the OnCommand property of ImageButton:
Please update your code with following.
Write OnCommand="GridView1_RowDeleting"
I hope it would work.
protected void GridView1_RowDeleting(object sender, CommandEventArgs e)
{
// Do something
}

Categories