Solved

How to maintain Listbox position after Postback

Posted on 2007-12-03
5
793 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
Short and sweet  -  Thank You very much!!!
0
 

Author Closing Comment

by:allanmark
Comment Utility
Most educational -  thank you!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now