Solved

How to maintain Listbox position after Postback

Posted on 2007-12-03
5
797 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net See list of pdf documents in folder and read 2 28
ASP.net pdf file opening in debug mode but not on web 13 32
dynamic menu in asp.net c# 11 57
Re-position the objects 7 96
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

23 Experts available now in Live!

Get 1:1 Help Now