Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • sun 403 posts 395 karma points
    Nov 20, 2010 @ 12:14
    sun
    0

    I want to make a submit form in HomePage, but it can't goto search.aspx page.

    code like this:

    <form method="post" action="search.aspx" id="RunwayMasterForm">

        <input name="search" type="text" class="input" value="" style="width: 65%;" />

        <input class="art-button" type="submit" name="search" value="" onclick="window.location.href='search.aspx'" />

    </form>

     

    hope some one can give me some help.

  • Sebastiaan Janssen 4847 posts 14373 karma points MVP admin hq
    Nov 20, 2010 @ 13:16
    Sebastiaan Janssen
    0

    You probably have this form nested inside another form. So earlier in your page you probably have a <form runat="server"> in there. That doesn't work, ad you can't have nested forms.

  • sun 403 posts 395 karma points
    Nov 20, 2010 @ 13:20
    sun
    0

    yes, I put this form in a template that support canvas mode. Is there another way to do this?

  • Jeroen Breuer 4860 posts 12126 karma points MVP 3x admin c-trib
    Nov 20, 2010 @ 13:46
    Jeroen Breuer
    0

    I know a way how to solve this, but I'm not sure if it's the best solution. I place my fields inside the main form (with runat=server) and when the search button is pressed I submit the main form to another page. Here is a macro sample I'm using:

    .ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebsiteTop.ascx.cs" Inherits="Reeleezee.WebApplication.UserControls.WebsiteTop" %>
    
    <asp:Panel ID="PnlTop" runat="server" CssClass="topbar" DefaultButton="BtnSearch">
        <ul>
            <umbraco:Item field="websiteHeaderBlock" runat="server" recursive="true"></umbraco:Item>
            <li>
                <table cellspacing="0" cellpadding="0">
                    <tr>
                        <td>
                            <asp:TextBox ID="TxtSearch" CssClass="text" Text="Zoeken" onfocus="if(this.value=='Zoeken')this.value=''" onblur="if(this.value=='')this.value='Zoeken'" runat="server" />
                            <asp:RequiredFieldValidator ID="ReqSearch" runat="server" ControlToValidate="TxtSearch" InitialValue="Zoeken"></asp:RequiredFieldValidator>
                        </td>
                        <td>
                            <asp:Button ID="BtnSearch" CssClass="submit" runat="server"/>
                        </td>
                    </tr>
                </table>
            </li>
        </ul>
    </asp:Panel>

    .cs:

    namespace Reeleezee.WebApplication.UserControls
    {
        public partial class WebsiteTop : BaseUserControl
        {
            #region Properties
    
            public int SearchLink
            {
                get;
                set;
            }
    
            #endregion
    
            protected void Page_Load(object sender, EventArgs e)
            {
                BtnSearch.OnClientClick = string.Format("document.getElementById('aspnetForm').action='{0}';", library.NiceUrl(SearchLink));
            }
        }
    }

    As you can see I use javascript to post the form to the new link. You have to make sure you give your main form the correct id:

    <form runat="server" id="aspnetForm">

    On the other page you can get your search results from the Request.Form:

    //Dont use !IsPostBack because when we redirect to this page we don't know if it's a postback or not.
    
    //Get the textbox from the WebsiteTop usercontrol.
    TextBox txtSearch = this.Page.FindControlRecursive<TextBox>("TxtSearch");
    
    //Get the search value from the form.
    Search = Request.Form[txtSearch.UniqueID];
    
    //Set the posted value inside the textbox again.
    txtSearch.Text = Search;

     

    Btw sun do you work for Tribal China? If so I work for Digibiz and you can just get this code from svn :).

    Jeroen

  • James Telfer 65 posts 165 karma points
    Nov 24, 2010 @ 12:20
    James Telfer
    1

    Hi,

    Jeroen came across some stuff I did recently and asked that I post as he thought it might be helpful.

    As Sebastiaan pointed out, you can't have nested forms, and thanks to the ASP.NET, you can only have one form for the page. This means that you can't have a separate search form that posts to a different page. But for a search, you don't really want to do a post; more likely a GET is better (bookmarking and analytics). 

    This makes the above code from Jeroen change (in the code behind):

    protected void Page_Load(object sender, EventArgs e)
    {
        BtnSearch.OnClientClick = string.Format(
            "document.location.href ='{0}?q=' + encodeURIComponent($('#{1}').val()); return false;",
            library.NiceUrl(SearchLink), // SearchLink is the nodeId of the page that will do the search
            TxtSearch.ClientID); // This is the search terms text box control
    }
    

    (Note JQuery is required for this solution to work; use document.getElementById if you're not using it.)

    Then in the search page, you only need to get the search terms from the query string: Request.QueryString["q"]. Be sure to check for null!

    Now I'll freely admit the the above isn't a very clean solution. If you look at the generated code, you'll see that the "return false" in the ClientClick code causes the ASP.NET Post code to be skipped (intentionally).

    I could have changed the asp:Button to a link, and added a JQuery click handler to that link instead, and I could also have used a straight input instead of an asp:TextBox control.

    The link solution would look something like:

        <input id="searchTerms" name="searchTerms" type="text" />
        <a id="searchLink" href="#">Search</a>
    
        <script type="text/javascript">
            $(document).ready(function() {
                $('#searchLink').click(function(event) {
                    event.preventDefault();
                    document.location.href ='/search.aspx?q=' + encodeURIComponent($('#searchTerms').val());
                });
            });
        </script>
    

    Sometimes the single form limitation is quite frustrating, but if you don't mind using javascript you can always work around it.

    Hope this helps,

    JT

  • sun 403 posts 395 karma points
    Nov 24, 2010 @ 15:01
    sun
    0

    Thanks for you all. I do it only with javascript, no jquery.

                                <input type="text" value="" id="searchTerms"  />

                                  <a href="#" id="searchLink" onclick="document.location.href='/search.aspx?search='+encodeURIComponent(getElementById('searchTerms').value)" >search</a>

    thanks again.

  • Brian Juul Andersen 43 posts 97 karma points c-trib
    Aug 03, 2012 @ 14:23
    Brian Juul Andersen
    0

    Nice ! This is exactly what I´ve been looking for ! Thank you guys !

Please Sign in or register to post replies

Write your reply to:

Draft