简体   繁体   English

链接按钮不会触发点击事件,但会触发模式

[英]Link button doesn't fire click event but triggers modal

This is how I add data to my repeater: 这是我向中继器添加数据的方式:

<asp:Repeater ID="rptNotification" runat="server">
     <HeaderTemplate>
        <table class="table table-hover">
           <tr>
             <th>Code</th>
             <th>Description</th>
             <th>Name</th>
             <th>Action</th>
            </tr>

     </HeaderTemplate>
     <ItemTemplate>
      <tr>
           <td><asp:Label ID="lblCode" runat="server" Text='<%#Eval("[Group Code]") %>'></asp:Label></td>
            <td><asp:Label ID="lblDescription" runat="server" Text='<%#Eval("[Description]") %>'></asp:Label></td>
            <td><asp:Label ID="lblName" runat="server" Text='<%#Eval("[Professor]") %>'></asp:Label></td>
             <td><asp:LinkButton ID="lbtnEye" CommandArgument='<%#Eval("[ID]") %>' runat="server" CssClass="btn btn-primary btn-xs" OnClick="lbtnEye_Click" data-toggle="modal" data-target="#myModal"><i class="fa fa-eye"></i></asp:LinkButton>
                                                <!-- <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder> -->
        </td>
       </tr>
      </ItemTemplate>
       <FooterTemplate>
       </table>
       </FooterTemplate>
        </asp:Repeater>

So as you can see the linkbutton is added dynamically, I have code inside the lbtnEye_Click event but it doesn't hit the click event (I placed breakpoint on it) but it fires the modal. 因此,如您所见,linkbutton是动态添加的,我在lbtnEye_Click事件中包含代码,但是它没有命中click事件(我在该事件上放置了断点),但是触发了模式。 What is wrong with my code? 我的代码有什么问题?

I think you need to add, OnItemCommand like 我认为您需要添加OnItemCommand之类的

<asp:Repeater ID="rptNotification" runat="server"  OnItemCommand="rptNotification_ItemCommand">

Add CommandName in linkButton 在linkBut​​ton中添加CommandName

<asp:LinkButton ID="lbtnEye" CommandName="EyeClicked" CommandArgument='<%#Eval("[ID]") %>' runat="server" CssClass="btn btn-primary btn-xs" OnClick="lbtnEye_Click" data-toggle="modal" data-target="#myModal"><i class="fa fa-eye"></i></asp:LinkButton>

Code behind 后面的代码

protected void rptNotification_ItemCommand(object source, RepeaterCommandEventArgs e)
{

    if (e.CommandName == "EyeClicked") // check command
    {

      //Your code
   }

}

The C# click event is not triggered because it's being suppressed by javascript.To make sure both the modal is displayed and the server side click event get's raised you need to change the way you display the popup - you should dynamically call the popup from javascript. C#click事件未触发,因为它已被javascript抑制。要确保同时显示模式和引发服务器端click事件,您需要更改显示弹出窗口的方式-您应该从javascript动态调用弹出窗口。 Like this: 像这样:

  1. Change your link button to be like this: 更改您的链接按钮,如下所示:

     <asp:LinkButton ID="lbtnEye" CommandArgument='<%#Eval("ID") %>' runat="server" CssClass="btn btn-primary btn-xs" OnClientClick="showPopup()" OnClick="lbtnEye_Click"> <i class="fa fa-eye"> Click me... </i> </asp:LinkButton> 
  2. Add this to the top of the page: 将其添加到页面顶部:

Javascript and library references: Javascript和库参考:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  

<script type="text/javascript">
    $(function () {
        showPopup = function () {
            debugger;
            $("#myModal").modal('show');
            return true;
        }
    });
</script>

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

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