ASP.NET Web Form parameters in URL

后端 未结 4 581
眼角桃花
眼角桃花 2021-01-19 16:22

I have an ASP.NET page that contain a form that search the page.

Is there any solution so that I can have the search text in the URL?

I want to give the posi

相关标签:
4条回答
  • 2021-01-19 16:45

    There might be other better/cleaner/proper ways of doing it, like changing form's action, or changing button's PostBackUrl, but this is what I would do.

    1. Redirect to self with search term appended to query string.
    2. On page load, if query string is not empty, do search.

    .aspx:

    <asp:Label ID="Label1" runat="server" Text="Search Term:&nbsp;"></asp:Label>
    <asp:TextBox ID="txtSearchTerm" runat="server"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" Text="Search" 
        onclick="btnSearch_Click" />
    

    .cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
            return;
    
        if (!string.IsNullOrEmpty(Request.QueryString["SearchTerm"]))
        {
            string searchTerm = Request.QueryString["SearchTerm"];
            txtSearchTerm.Text = searchTerm;
            DoSearch(searchTerm);
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtSearchTerm.Text.Trim()))
        {
            Response.Redirect("~/Search.aspx?SearchTerm=" + txtSearchTerm.Text.Trim());
        }
    }
    
    private void DoSearch(string searchTerm)
    {
        //search logic here
        Response.Write("Search result: " + searchTerm);
    }
    
    0 讨论(0)
  • 2021-01-19 16:45

    If you want the ability to copy/paste the URL, you will need to get the search parameters from the form and redirect back to the form with the search parameters on the URL, then read the QueryString values to perform the search.

    0 讨论(0)
  • 2021-01-19 16:50

    You can also use jQuery to do the trick, like this:

    $(function(){
    
    $('input[type="submit"]').click(function(e){
           e.preventDefault();
           var searchResult = "Search.aspx?q=" + $('input#textbox').val();
           $('form#myForm').attr('action',searchResult);
        });
    });
    
    0 讨论(0)
  • 2021-01-19 17:00

    After more research abut this topic I think that the javascript solution is the best:

    You can access the ACTION attribute of the form using JavaScript.

    <form id="myForm" action="Search.aspx" onsubmit="return setAction();">
        <input id="textbox" />
        <input type="submit" value="Submit" />
    </form>
    
    <script type="text/javascript">
    
    function setAction()
    {
        var myForm = document.getElementById( "myForm" );
        var myText = document.getElementById( "textbox" );
    
        if (myForm && myForm.action && myText && myText.value != null )
        {
           myForm.action = "Search.aspx?q=" + myText.value;
        }
        return true;
    }
    
    </script>
    

    Personally I am not a big fan of JavaScript ... but this does not add an extra request to the server. If you think that this has any drawbacks please let me know.

    0 讨论(0)
提交回复
热议问题