views:

125

answers:

2

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>
+2  A: 

Your onclientclick event needs to return false;

Kris Krause
I tried that with `asp:button` - still, browser closes connections. I added sample code in the question - possibly I was mistaken in my code.
flashnik
Well, I added `return false;` to onclientclick in description of button so it looks like `onclientclick="searchRedirect(); return false;"`and everything began to work.Thank you!
flashnik
A: 

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.

lmsasu
It dosn't work hence a button specified as default should implement IButtonControl interface. A Html button doesn't implement it since it's just a piece of html code
flashnik