【问题标题】:Hyperlink or button does not fire inside repeater item template超链接或按钮不会在转发器项目模板内触发
【发布时间】:2019-03-09 06:33:15
【问题描述】:

我有一个中继器,在项目模板内部我有一个按钮和一个锚点。

<asp:Repeater runat="server" ID="rptCategoryList" OnItemDataBound="rptCategoryList_ItemDataBound">
    <ItemTemplate>
        ....
        <div style="margin-left: 81.6%;">
            <span runat="server" id="spnRegister" clientidmode="static" class="register pull-right">
                <button class="btn btn-info btn-lg" style="float: ; margin-right: 40px; margin-top: -150px; background-color: #3697EA">
                    <a href="register.aspx" style="color: white;" target="_self">Register</a>
                </button>
            </span>
        </div>
        ....
    </ItemTemplate>

单击此按钮时,它会重新加载同一页面,而不是转到 register.aspx。我使用了“查看页面源”,并且href设置正确。

我移除了锚点,并为按钮添加了一个类并使用了 jQuery:

        <button class="btn btn-info btn-lg registerSilver" style="float: ; margin-right: 40px; margin-top: -150px; background-color: #3697EA">
            Register
        </button>

$(function () {
    $('.registerSilver').click(function () {debugger
        window.location = 'register.aspx';
    });
});

仍然无法正常工作,不断重新加载同一页面。

我什至尝试将 runat="server" 添加到超链接并将其 href 设置在转发器的 itemdatabound 中,但没有运气。

我在这里错过了什么?

【问题讨论】:

  • 听起来您的按钮设置为提交,导致回发而不是重定向。尝试添加 type="button" 属性,例如&lt;button type="button" ...&gt;&lt;a href="register.aspx" ...&gt;Link Text&lt;/a&gt;&lt;/button&gt;.

标签: asp.net hyperlink .net-4.5 repeater


【解决方案1】:

&lt;button&gt; 标签的默认行为触发表单提交(即 ASPX 页面中的&lt;form runat="server"&gt;),这可能导致回发到同一页面而不是重定向到锚链接中指定的页面 URL。尝试明确设置type="button" 属性,如下例所示:

<button type="button" class="btn btn-info btn-lg" style="float: ; margin-right: 40px; margin-top: -150px; background-color: #3697EA">
    <a href="register.aspx" style="color: white;" target="_self">Register</a>
</button>

或者在 jQuery 中使用 preventDefault() 函数作为 click 事件处理程序来防止该按钮的默认提交操作:

$('.registerSilver').click(function (e) {
    e.preventDefault();
    debugger;
    window.location = 'register.aspx';
});

【讨论】:

    【解决方案2】:

    如果您只是想重定向,那么为什么使用按钮而不是那个,您可以将按钮 css 类传递给锚标记并使其看起来像按钮

    <asp:Repeater runat="server" ID="rptCategoryList" OnItemDataBound="rptCategoryList_ItemDataBound">
        <ItemTemplate>
            ....
            <div style="margin-left: 81.6%;">
                <span runat="server" id="spnRegister" clientidmode="static" class="register pull-right">                
                        <a href="register.aspx" class="btn btn-info btn-lg" style="color: white; float: ; margin-right: 40px; margin-top: -150px; background-color: #3697EA" target="_self">Register</a>
                </span>
            </div>
            ....
        </ItemTemplate>
    

    这会起作用

    【讨论】:

      【解决方案3】:

      您也可以将 html 按钮替换为 asp 按钮:

      <asp:Button runat="server" CssClass="btn btn-info btn-lg registerSilver" PostBackUrl="register.aspx" Text="Register"/>
      

      这样就可以设置 PostBackUrl 属性了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-25
        • 2017-05-30
        • 1970-01-01
        • 2018-10-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多