ASP.NET Web Form parameters in URL

后端 未结 4 670
眼角桃花
眼角桃花 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:

    
    
    
    

    .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);
    }
    

提交回复
热议问题