链接按钮不会触发单击事件但会触发模态

时间:2016-06-09 23:40:40

标签: c# asp.net webforms

这是我向转发器添加数据的方式:

<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>

因为你可以看到链接按钮是动态添加的,我在lbtnEye_Click事件中有代码,但它没有点击click事件(我在它上面放置了断点)但它触发了模态。我的代码出了什么问题?

2 个答案:

答案 0 :(得分:1)

我认为您需要添加OnItemCommand,如

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

在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>

背后的代码

protected void rptNotification_ItemCommand(object source, RepeaterCommandEventArgs e)
{

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

      //Your code
   }

}

答案 1 :(得分:1)

C#click事件未被触发,因为它被javascript抑制。为了确保显示模式和服务器端点击事件,你需要改变显示方式弹出窗口 - 您应该从javascript动态调用弹出窗口。像这样:

  1. 将链接按钮更改为:

    <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. 将此内容添加到页面顶部:

  3. 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>