Extracting the query parameters

S

sunil

I have a button named Button1, and I wrote an event handler for the
OnClick event.

protected void Button1_Click(object sender, System.EventArgs e)
{
this.Response.Redirect("Default.aspx?q=" + this.TextBox1.Text+ " ");
}

The TextBox1 is a text box that takes user input.
I get the text box content by extracting the query parameters as
follows:

protected string Q
{
get
{
string query = this.Request.Params["q"];
if (query == String.Empty)
return null;
return query;
}
}

The Page_Load event handler is as follows:

protected void Page_Load(object sender, System.EventArgs e)
{
// Press Button1 on enter
Page.RegisterHiddenField("__EVENTTARGET", "Button1");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
if (!IsPostBack)
{
if (this.Q != null)
{
// I call my own method
findResults();
}
DataBind();
}
}

The problem is if I enter the term "c# language", the value of the
TextBox1.Text is "c# language" which is as I expect. But as soon as the
response is redirected and the control goes to the Page_Load(), then
"this.Q" value is being set to "c".
How do I send the correct value?
Thanks in advance.
 
S

sunil

Eliyahu said:
HtmlEncode it as Server.HtmlEncode(this.TextBox1.Text) and HtmlDecode on the
other end.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net

Hi Eliyahu,
I have tried out as you have suggested. Now the line looks like
protected void Button1_Click(object sender, System.EventArgs e)
{
this.Response.Redirect("Default.aspx?q=" +
Server.HtmlEncode(this.TextBoxQuery.Text));
}

The new Q property looks like:

protected string Q
{
get
{
string query = Server.HtmlDecode(this.Request.Params["q"]);
if (query == String.Empty)
return null;
return query;
}
}

But this didnt work out for me. Still, the value of query is "c"
Am I doing correct?
 
S

sunil

Mark said:
Try URLEncode / URLDecode instead of HTMLEncode / HTMLDecode

Hi Mark,
Thanks for the reply. Using UrlEncode/UrlDecode almost worked out. It
is working for the first example that I had given("c# language")
But if I give:"c++ language" in the TextBox, then after decoding it is
changed to "c language".
How can I avoid this?
 
S

sunil

Hi
I have used UrlEncode only to encode the string? On the receiver side,
I am not decoding.
The problem for the previous example of "c++ language being converted
to "c language" is that I was decoding the received query string using
UrlDecode. So the decoding happens twice.
Now I am not decoding on the receiver side, it works perfectly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top