flashnik
flashnik

Reputation: 1932

How to set html button as default for ASP.Net form?

Well, I'm trying to make ASP.NET urls looking user-friendly, like it was explained in this question. So I created an ASP.Net form, and placed asp:textbox and asp:button on it. Also I set onclientclick attribute to call JS function which navigates to smart URL by setting windows.location.href. In Firefox it works well but in IE and Opera the browser first navigates to smart url but then it closes connection and sends a postback using an asp.net form action.

I tried to solve it using html button instead of server ones. It works, but the problem is that it can't be set as default for the asp.net form. So' if user clicks on it, it does its work. But if the user just presses enter when form is active, the form performs its action, so the button is not pressed and JS url rewriting doesn't occur. So how can I solve this problem?

My JS looks like this:

function searchRedirect() {
    var query = $get('colSearch');
    window.location.href = 'colSearch?q=' + query.value;
    return false;
}

and in search.aspx i have

<form id="MainForm" runat="server" method="get">
        <asp:TextBox id="colSearch" runat="server" Width="615px" CssClass="searchLine"></asp:TextBox>
        <input id="Button1" type="button" value="Search!" onclick="searchRedirect();" class="search" />

I also tried with asp:button:

<form id="MainForm" runat="server" method="get" defaultbutton="submitReqBtn">
        <asp:TextBox id="colSearch" runat="server" Width="615px" CssClass="searchLine"></asp:TextBox>
        <asp:Button runat="server" Text="Search!" ID="submirReqBtn" 
            onclientclick="searchRedirect();" CausesValidation="False" 
            EnableViewState="False" UseSubmitBehavior="False"></asp:Button>
</form>

Upvotes: 0

Views: 1950

Answers (2)

lmsasu
lmsasu

Reputation: 7583

The form accepts an attribute showing which of the buttons are set as default: use it as in

<form id="form1" runat="server" defaultbutton="Button1">

where Button1 is the id of a button on the page.

Upvotes: 0

Kris Krause
Kris Krause

Reputation: 7326

Your onclientclick event needs to return false;

Upvotes: 2

Related Questions