I am using C# + VS2008 + .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://example.org/foo.aspx?parame1=abc¶m2=def¶m3=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 sa开发者_如何学Pythonme 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://example.org/foo.aspx?parame1=abc¶m2=def¶m3=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://example.org/foo.aspx?parame1=abc¶m2=def¶m3=ghi, and the page displays, when user changes text input from abc to google, the new request will be http://example.org/foo.aspx?parame1=google¶m2=def¶m3=ghi
Any reference samples? My question is I do not know how to implement so many functions in one aspx page.
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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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());
}
}
}
精彩评论