Solved

Is there a readonly equiv. for a listbox in ASP.NET 2005 using VB.NET?

Posted on 2006-06-25
2
1,141 Views
Last Modified: 2008-02-20
Is there an equiv. for the readonly property of a textbox control in a listbox control in ASP.NET?  If I set the enabled property to false on a listbox control, the user can't change the selections (which is good), but also can't scroll up and down (which is the problem).  I need the user to be able to scroll up and down but not be able to change the selections.  Any ideas?
0
Comment
Question by:stev0931
2 Comments
 

Accepted Solution

by:
kskim43 earned 200 total points
ID: 16981324
I don't think there is a property you can just set on the listbox object to do this.
I had a similar situation and had to use javascript on the client to reselect the original value in the listbox when a user changed the selected item.
I could have autoposted back and maybe just rebound the listbox with the onSelectedIndexChanged event, but seemed like a wasted trip back to the server.

I only had one selected item, but I think you can set the selected property of each list item when you generate the javascript code in the load event.

Also, you may want to check out this article from Code Project. Didn't have time to play with it yet, but looks promising..
http://www.codeproject.com/combobox/readonlycombo.asp


Here's the code..

<HTML>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:ListBox ID=lst Runat=server Width=100 onChange="StopChange(this);"  />
            </form>
      </body>
</HTML>


Code behind:


    Protected WithEvents lst As System.Web.UI.WebControls.ListBox

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            LoadPage()
        End If
    End Sub

    Private Sub LoadPage()
        Dim selectedIndx As Integer

        selectedIndx = 3

        lst.Items.Add("A1")
        lst.Items.Add("b1")
        lst.Items.Add("c1")
        lst.Items.Add("d1")
        lst.Items.Add("e1")
        lst.Items.Add("f1")
        lst.Items.Add("g1")
       
        lst.Items(selectedIndx).Selected = True

        Dim script As String

        script = "<script language=javascript>"
        script &= "      function StopChange(obj) {"
        script &= "            obj.selectedIndex = " & selectedIndx & ";"
        script &= "      }"
        script &= "</script>"

        RegisterClientScriptBlock("stopChange", script)
    End Sub
0
 

Author Comment

by:stev0931
ID: 16981522
Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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