简体   繁体   English

如何从Gridview中的LinkBut​​ton触发Onclick事件驻留在updatePanel中?

[英]How to fire Onclick event from LinkButton in Gridview resides in updatePanel?

I know this might be asked before. 我知道这可能是以前问过的。 My gridview rows are changed according to search filter which fetches data from Database. 根据从数据库中获取数据的搜索过滤器,更改了我的gridview行。 But when I click on Link button to view data only Onclientclick is fired and onclick is not firing. 但是,当我单击“链接”按钮以查看数据时,仅Onclientclick ,而没有触发onclick

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. 这是我的javascript函数,我只是用它来检索在方法中需要的当前列索引。 :

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 :- 这是我的onclick事件:-

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. 如果需要在此问题上添加其他代码,请告诉我。

your setup is such that the clientClick handler registers a clickhandler, that clickhandler seems unnecessary for what you appear to be trying to do. 您的设置是使clientClick处理程序注册一个clickhandler,对于您似乎要尝试执行的操作,似乎不需要clickhandler。 (set a hidden field so you can figure out which row was targeted on postback). (设置一个隐藏字段,以便您可以确定回发针对的行)。

Since events are handled after restoring the pages' state, you could just use sender in btnView_Click to determine the row on the serverside without resorting to javascript here. 由于事件是在恢复页面状态后处理的,因此您可以仅使用btnView_Click中的btnView_Click来确定服务器端的行,而无需在此处使用javascript。

try casting sender to Button and see the wealth of available information in the debugger after removing the OnClientClick handler altogether. 尝试完全删除OnClientClick处理程序后,将发送方转换为Button,并在调试器中查看大量可用信息。

More directly, your changeAnchor function does not return a function, it registers a function to be ran whenever jquery is ready. 更直接地,您的changeAnchor函数不会返回函数,它会在jQuery准备就绪时注册要运行的函数。 that in turn registers a clickHandler on then present html elements that happen to match the button. 依次注册一个clickHandler,然后显示恰好与按钮匹配的html元素。 you shouldn't be doing this, you end up probably registering the same handler for all buttons, every time a button is rendered. 您不应该这样做,最终可能会在每次呈现按钮时为所有按钮注册相同的处理程序。 The eventual click handler also returns false, which cancels the postback. 最终的点击处理程序还会返回false,从而取消回发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM