By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Extracting the query parameters

P: n/a
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.

Jan 22 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
"sunil" <sa**********@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
>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.

Jan 22 '07 #2

P: n/a

Eliyahu Goldin wrote:
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?

Jan 22 '07 #3

P: n/a
"sunil" <sa**********@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
Am I doing correct?
Try URLEncode / URLDecode instead of HTMLEncode / HTMLDecode
Jan 22 '07 #4

P: n/a

Mark Rae wrote:
"sunil" <sa**********@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
Am I doing correct?

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?

Jan 22 '07 #5

P: n/a
Use:

UrlPathEncode

UrlEncode has some problem while dealing with spaces, '+';

-Akki
sunil wrote:
Mark Rae wrote:
"sunil" <sa**********@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
Am I doing correct?
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?
Jan 23 '07 #6

P: n/a
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

Jan 23 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.