Solved

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

Posted on 2006-06-25
2
1,139 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

9 Experts available now in Live!

Get 1:1 Help Now