Hyperlink or button does not fire inside repeater item template

I have a repeater and inside item template I have a button and an anchor.

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

when this button is clicked it reloads the same page rather going to register.aspx. I used "view page source" and href is set correctly.

I removed the anchore and added a class to button and used 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';
    });
});

still not working, keeps reloading the same page.

I even tried to add runat="server" to the hyperlink and set its href in repeater's itemdatabound, no luck.

What am I missing here?

The default behavior of <button> tag triggers form submit (i.e. <form runat="server"> in ASPX page), which probably causing postback to same page instead of redirecting to page URL specified in anchor link. Try setting type="button" attribute explicitly like example below:

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

Or use preventDefault() function for click event handler in jQuery to prevent default submit action of that button:

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

I have a repeater and inside item template I have a button and an anchor. <asp:​Repeater runat="server" ID="rptCategoryList"  When the button is inside a Repeater template, you don't want to use an OnClick event, but the ItemCommand event of the Repeater control. Your .aspx code will look something like this:

If you just want to redirect then why you use button instead of that you can just pass button css class to anchor tag and make it look like button like

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

This will work

I think you are not getting my question. I want to fire item command or onclick event to display the dynamic content in the href. Does this solution  Ofcourse, this would fail when you clicked on say the link of the second item, and when you postback there is only 1 item left (because someone else removed an item). It would fail because the Page will be unable to find the clicked control, because it is no longer re-created by the Repeater.

You could also replace the html button to an asp one:

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

This way you can set the PostBackUrl attribute.

I have seen this link but the problem is that a Hyperlink does not fire the protected void RepeaterItemDataBound(object sender, <ItemTemplate> <li id="​l1" runat="server"><asp:HyperLink I could have used a Link Button instead of a Hyperlink, but a link button neither has a url nor an image url to set. At work today I tried to do something I though would be simple, I needed to add a check box control to a repeater’s item template and have it fire the ItemCommand event on the repeater. The button and link button controls do this without a problem but for some reason Microsoft decided

I created the repeater control, in that itemcommand event is not fired on clicking link button inside ItemTemplate Tag. Below is my code. Ascx:. The following example demonstrates how to specify and code a handler for the ItemCommand event of the Repeater control. Information about the button is displayed when a Button control within the Repeater control is clicked.

In this article I will explain with an example, how implement Button can download the database table SQL by clicking the download link below. The Repeater's ItemTemplate consists of two Label controls and a If Not Me. C# - Get The Value Of A HTML Select Element Inside A Repeater Control On Button Click? May 24, 2010. I have a repeater with select html inside the item template. I could not use dropdown list as it does not support so i had to build select with inside a repeater. On button click i want get the value of the selected item.

There are various templates in it like Header Template, Item Template, AlternatingItemTemplate, FooterTemplate etc. My WebApplication is 

Comments
  • Sounds like your button is set to submit which causing postback instead of redirection. Try adding type="button" attribute e.g. <button type="button" ...><a href="register.aspx" ...>Link Text</a></button>.