开发者

How do I redirect?

I have an image that is a hyperlink. How do I access my "searchRedirect" function for a redirect from the server side after the image is clicked?

<input id="textSearch" runa开发者_StackOverflowt="server" name="textSearch" type="text" />
<asp:HyperLink id="searchButton" runat="server"> 
       <img alt="" src="images/SearchButton.png"/>
</asp:HyperLink>

protected void searchRedirect()
{
    Response.Redirect("/NewProject/Home/?searchString=" + textSearch.Value;
}


Rather than a HyperLink, you'll want to use a LinkButton and listen for its Click event

<input id="textSearch" runat="server" name="textSearch" type="text" />
<asp:LinkButton id="searchButton" runat="server" OnClick="searchRedirect"> 
       <img alt="" src="images/SearchButton.png"/>
</asp:LinkButton>

protected void searchRedirect(sender As Object, e As EventArgs)
{
    Response.Redirect("/NewProject/Home/?searchString=" + textSearch.Value);
}

Al Kepp is suggesting that doing it this way causes the page to post back only to realize the redirect, leading to an unnecessary page load. A javascript version such as the one below would avoid the first post back:

<input id="textSearch" runat="server" name="textSearch" type="text" />
<a href="#" onclick="window.location='/NewProject/Home/?searchString=' + getElementById('textSearch').value; return false;">
       <img alt="" src="images/SearchButton.png"/>
</a>

I didn't actually test that code, but I don't think I have any typos.


Either write it in JavaScript, because it is the only way how to perform that code on client side. That is necessary to make it work exactly as you wish.

Or use LinkButton as Nick Spiers said. In that case you won't see testSearch.Value in url address. (That is often good, but if you want to see it there, the best you can do is to write it in JavaScript.) But unlike Nick Spier's code, I would omit redirect command there and perform the required search action immediately. (Because redirect will cause sending two pages to client, which is not what user usually expects when he/she presses the search button.)


Or you could use javascript and get rid of an extra roundtrip to the server. Here's an example using jQuery

<input id="textSearch" class="textSeachClass" runat="server" name="textSearch" type="text" />
<asp:HyperLink id="searchButton" CssClass="searchButtonClass" runat="server"> 
        <img alt="" src="images/SearchButton.png"/>
</asp:HyperLink>

<script type="text/javascript">
    // This code should be put in a separate .js-file so it can 
    // be cached and the aspx page will be more SEO-friendly
    $("a.searchButtonClass").click(function (e) {
        e.preventDefault();
        window.location = "/NewProject/Home/?searchString=" + $("input.textSeachClass").val();
    });
</script>

I use css classes as selectors since ASP.Net Webforms will change the client id of the controls when they get rendered, unless you use .Net Framework 4.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜