I know this might be asked before. My gridview rows are changed according to search filter which fetches data from Database. But when I click on Link button to view data only Onclientclick
is fired and onclick
is not firing.
Here's my code :
<form id="form1" runat="server">
<asp:HiddenField runat="server" ID="Hf" />
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<label> Show <asp:DropDownList runat="server" CssClass="custom-select custom-select-sm form-control form-control-sm"
ID="RecordSize" AutoPostBack="true" Style="width: 51.75px; height: 30.75px;">
<asp:ListItem Value="10" Selected="True">10</asp:ListItem>
<asp:ListItem Value="25">25</asp:ListItem>
<asp:ListItem Value="50">50</asp:ListItem>
</asp:DropDownList> entries</label> <asp:Label Style="margin-left: 10px;" runat="server" ID="lblerror" ForeColor="Red"></asp:Label>
<div id="m_table_1_filter" class="dataTables_filter">
<label>Search:</label>
<asp:TextBox runat="server" ID="txtSearch" type="search" autocomplete="off"
AutoPostBack="true" MaxLength="20" CssClass="form-control form-control-sm" OnTextChanged="txtSearch_TextChanged" Style="margin-top: -30px; margin-left: 65px; width: 250px;"></asp:TextBox>
</div>
<div class="row" style="display: block;">
<asp:GridView OnPageIndexChanging="MandateGrid_PageIndexChanging" runat="server"
ID="MandateGrid" AutoGenerateColumns="false" OnRowDataBound="MandateGrid_RowDataBound"
PageSize="5" Style="text-align: center;" AllowPaging="true" AllowSorting="true"
CssClass="table table-striped- table-bordered table-hover table-checkable dataTable no-footer">
<PagerSettings FirstPageText="<<" LastPageText=">>" Mode="NumericFirstLast" />
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="LabelID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mandate Status">
<ItemTemplate>
<asp:LinkButton CommandArgument="Select" CommandName="Select" ID="btnView" runat="server" Text="View"
CssClass="btn m-btn--square" OnClientClick="changeAnchor();" OnClick="btnView_Click" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
This is my javascript function, I'm using it just to retrive current column index which I will need in my method. :
function changeAnchor() {
$(function () {
$("[id*=MandateGrid]").find("[id*=btnView]").click(function () {
var row = $(this).closest("tr");
var message = "Row Index: " + (row[0].rowIndex - 1);
$("#Hf").val(row[0].rowIndex - 1);
//alert(message);
return false;
});
});
}
Here's my onclick event :-
protected void btnView_Click(object sender, EventArgs e)
{
lblerror.Text = "Event Fired";
}
Please let me know if I have to add any other code to this question.