views:

57

answers:

1

Hello everyone,

I am using C# + VSTS 2008 + .Net 3.5 + ASP.Net + IIS 7.0 + ADO.Net + SQL Server 2008. I want to develop an ASP.Net aspx page which has the following function,

1 It could accept 3 Url parameters param1, param2 and param3, and the request looks like this,

http://mysite.com/foo.aspx?parame1=abc&param2=def&param3=ghi

2 When the page is responsed to client browser, I want to display a text input and a submit button nearby in the result html page, and value of text input is the same as param1, in this sample, abc will be displayed in text box, in the browser address bar, I want to keep the original long url http://mysite.com/foo.aspx?parame1=abc&param2=def&param3=ghi;

3 When the user change the value in text input, and click submit button, I want to send this request again to foo.aspx, and changing the param1 value to the value which user entered in text input, and at the same time, keep values of parame2 and param3 the same as last request's response. For example, when user requests http://mysite.com/foo.aspx?parame1=abc&param2=def&param3=ghi, and the page displays, when user changes text input from abc to google, the new request will be http://mysite.com/foo.aspx?parame1=google&param2=def&param3=ghi

Any reference samples? My question is I do not know how to implement so many functions in one aspx page.

thanks in advance, George

+2  A: 

If you want the browser address bar to show the updated URL you can allow the button click to postback to the server and then you handle the TextChanged event of the textbox.

In the TextChanged event handler you build the new URL with the changed querystring arguments and use Response.Redirect to redirect the browser to the new URL.

Here is a quick an dirty example.

Given a ASPX page with a textbox and a button, somthing like this

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
  <title></title>
</head>
<body>

  <script type="text/javascript">
    function requery() {
      var query = location.search.substring(1);
      var pairs = query.split("&");
      var param1Value = document.getElementById("txtParam1").value;

      url = "/Default.aspx?param1=" + param1Value;
      for (var i = 0; i < pairs.length; ++i) {
        var pair = pairs[i];
        if ((pair.indexOf("param1") != 0) && (pair.length > 0)) {
          url += "&" + pair;
        }
      }
      location.href = url;
    }
  </script>

  <form id="form1" runat="server">
  <div>
    <asp:TextBox ID="txtParam1" runat="server" OnTextChanged="txtParam1_TextChanged"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Submit" />
    <input type="button" value="Client Action" onclick="requery()" />
  </div>
  </form>
</body>
</html>

Your code behind to handle the TextChanged event of the textbox could do the following.

using System;
using System.Text;

namespace WebApplication1
{
  public partial class _Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        txtParam1.Text = Request.QueryString["param1"];
      }
    }

    protected void txtParam1_TextChanged(object sender, EventArgs e)
    {
      // Build the new URL with the changed value of TextBox1      
      StringBuilder url = new StringBuilder();
      url.AppendFormat("{0}?param1={1}",
        Request.Path, txtParam1.Text);

      // Append all the querystring values that where not param1 to the
      // new URL
      foreach (string key in Request.QueryString.AllKeys)
      {
        if (string.Compare(key, "param1", true) != 0)
        {
          url.AppendFormat("&{0}={1}", key, Request.QueryString[key]);
        }
      }

      // Redirect the browser to request the new URL
      Response.Redirect(url.ToString());
    }
  }
}
Chris Taylor
Another issue I find is that, when enter string into text input, and submit again, Page_Load will be called twice, once with IsPostBack true and the other time with IsPostBack false, is that expected behavior (client send two request to server?)? Thank you.
George2
@George2, what do you mean by keep the original two parameters. The sample I provided should keep param2 and param3 on the URL. As for the two Page_Load events, that is expected. The first is in response to the PostBack event of the button and the second is in response to the browser redirecting to the new URL (this is a GET so IsPostBack == false), which is the same target page with the new parameters.
Chris Taylor
Yes, you are correct. I am wrong. :-(Could you answer this question please -- "Another issue I find is that, when enter string into text input, and submit again, Page_Load will be called twice, once with IsPostBack true and the other time with IsPostBack false, is that expected behavior (client send two request to server?)? Thank you."
George2
@George2, I believe I already answered that in my first comment. I quote "As for the two Page_Load events, that is expected. The first is in response to the PostBack event of the button and the second is in response to the browser redirecting to the new URL (this is a GET so IsPostBack == false), which is the same target page with the new parameters"
Chris Taylor
Thanks Chris, so in your sample code the default.aspx page is requested twice (from user click submit new entered text, to the page response with new entered text value in both url parameter/html text input)? Any better solutions which requests only once to server and it could achieve the same effect?
George2
@George2, well an alternative would be to use javascript on the client side to build the new URL on the button click (this would be handled on a client side event rather than a server side event) and then change the Window.Location to the new URL. Take a look here for more info on Window.Location: http://www.w3schools.com/jsref/met_loc_replace.asp
Chris Taylor
Thanks Chris, I read through the document as you suggested. But how to use window.location in your sample? Appreciate if you could provide some sample code or solutions. (I understand what is the function of window.location, but do not know how to utilize it in the problem I am facing). I am also confused that if I need to use javascript, should I user server side submit button (which uses "runat server") or html submit button?
George2
@George2, I will try get sometime to put together a sample. But you would not use a serverside control for the button, since you want to handle this client side.
Chris Taylor
@George2, I updated the ASPX code with a quick and dirty javascript function and a client button that calls that function. So you now have a simple example of how to do this on both the client and the server side. The JS can probably be improved, this is not exactly something I work with much so I just used the basic functions, but it should work.
Chris Taylor
Thank you so much Chris! When I come to office, I will definitely try your solution and let you know the result here. Eager to try and learn from you guru!
George2
Thanks, your solution works!
George2