Solved

How to maintain Listbox position after Postback

Posted on 2007-12-03
5
803 Views
Last Modified: 2012-06-21
Greetings

I have a listbox that contains names. AT this point all I want to do si display the current item in a text box.

As I understand things (which stands a fair chance of being wrong!!), in order to see the change effected to the textbox, the AutoPostBack property must be set to TRUE, to foirce a PostBack after selection change. This I have done, but am getting the following:

A potentially dangerous Request.Form value was detected from the client (TextBox1="...>>Mickey<<<PL").

WHAT SHOULD I BE DOING??
(IT ALSO LOOKS LIKE THE POSITION IN THE LISTBOX IS BEING LOST ON POSTBACK!!)

My code:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ListBox1.Items.Add("Mickey");
            ListBox1.Items.Add("Donald");
            ListBox1.Items.Add("Goofy");
            ListBox1.Items.Add("Wacko");
            ListBox1.SelectedIndex = 0;
        }
       
        TextBox1.Text = ">>>" + ListBox1.SelectedValue + "<<<PL";
    }
   
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = ">>>" + ListBox1.SelectedValue + "<<<SI";
    }
    protected void ListBox1_TextChanged(object sender, EventArgs e)
    {
        Label1.Text = ">>>" + ListBox1.SelectedValue + "<<<ST";
    }

In advance, thanks!!
0
Comment
Question by:allanmark
  • 2
  • 2
5 Comments
 
LVL 37

Accepted Solution

by:
samtran0331 earned 300 total points
ID: 20395715
>>A potentially dangerous Request.Form value was detected from the client

this is happening because .Net detects that the textbox contains the "<" and the ">" characters....which can be used for sql/script injection attacks.

while you can turn the warning off, you lose a level of hack protection.
0
 
LVL 14

Assisted Solution

by:Dustin Hopkins
Dustin Hopkins earned 75 total points
ID: 20395757
You could use the html encoded values for >(&gt;) and <(&lt;) in your code.

Label1.Text = "&gt;&gt;&gt;" + ListBox1.SelectedValue  + "&lt;&lt;&lt;PL";

Open in new window

0
 
LVL 37

Assisted Solution

by:samtran0331
samtran0331 earned 300 total points
ID: 20395802
that would work for a label, but .Net is catching the tags when the code does this:

TextBox1.Text = ">>>" + ListBox1.SelectedValue + "<<<PL";

and setting a textbox text to "&gt;" will still render "&gt;"
0
 

Author Comment

by:allanmark
ID: 20396170
Short and sweet  -  Thank You very much!!!
0
 

Author Closing Comment

by:allanmark
ID: 31412334
Most educational -  thank you!!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question