Results 1 to 15 of 15

Thread: Selection in listbox selects item in another listbox{Resolved}

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Posts
    434

    Selection in listbox selects item in another listbox{Resolved}

    Hello

    I have 2 webforms designed in ASP.net. Each has a listbox that displays different parts of the output from a DB. I want to have it so that when the user clicks on an item in a listbox on one form that the same index is selected in the list box on the other form and vice versa.

    In VB.net I did it like this:
    Code:
            frmSortedList.DefInstance.lstUserOutPut.SetSelected(lstSortedLines.SelectedIndex, True)
    But in ASP.net I can't see form2 from form1. The error says that it's because the control is protected. I tried changing the initializing declaration to protected friend but that gave me another error:
    "Reference to a non-shared member requires an object reference"
    That went completely over my head so I changed it back.

    I posted this in the wrong forum first and got a suggestion about using the autopostback property of the listbox but I couldn't make it work and on further researching it doesn't really look like it's what I want.

    Is this doable? If so then what am I doing wrong?

    Thanks

    David
    Last edited by David RH; Apr 24th, 2006 at 07:59 PM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Posts
    434

    Re: Selection in listbox selects item in another listbox

    Well then, What are the ramifications of removing the protected status or adding a friend declaration to it?

    Are either of these a wise move or a recipe for disaster?

    If I do either of these how do I deal with this error that then gets raized?

    "Reference to a non-shared member requires an object reference"

    Thanks

  3. #3
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Selection in listbox selects item in another listbox

    Make an accessor function in the page:

    Public Function ChangeBox(Value as String)
    ddlMyBox.SelectedValue = Value
    End Function

    Then, don't try to change the box directly, use the accessor function to change it.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Posts
    434

    Re: Selection in listbox selects item in another listbox

    I'm sorry but I forgot to mention that I'm using VS 2002 to develope this webapp.

    I can't see a property "selectedvalue" for the list box. I'm assuning that this is something that was added to 2003 or 2005.

    Should I get the VS 2005 express web deveopement app and start using that?

    How is this accessor function different form the selecteditem_changed event handler that I'm attempting to use now?

    Is there a good reason for not removing the listbox controls protected status and declaring it shared, there by allowing me to just access it directly?

  5. #5
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Selection in listbox selects item in another listbox

    Quote Originally Posted by David RH
    I'm sorry but I forgot to mention that I'm using VS 2002 to develope this webapp.

    I can't see a property "selectedvalue" for the list box. I'm assuning that this is something that was added to 2003 or 2005.
    Two reasons: 1) I wrote that from the top of my head so it's not exact, but should be close (there should be something in the valid range of values that means what I meant) and 2) I am using C# 2002 so we are on the same version, but Im used to the other language.

    Should I get the VS 2005 express web deveopement app and start using that?
    Completely not necessary.

    How is this accessor function different form the selecteditem_changed event handler that I'm attempting to use now?
    Scope. It's able to be made public.

    Is there a good reason for not removing the listbox controls protected status and declaring it shared, there by allowing me to just access it directly?
    It will change it back every time to you go to design time, it may break object referencial integrity and in general has been declared a bad thing to do. Plus you don't want the code nazis showing up, do you huh? DO YOU? HUH?

    =)
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Posts
    434

    Re: Selection in listbox selects item in another listbox

    Ok I set up the accessor function as you describe but I can't see it either???
    Code:
        Public Function ChangeSelection(ByVal Value As String)
            lstSortedLines.SelectedIndex = Value
        End Function
    Then I use this line in the listboxes SelectedIndexChanged event handler to access it:
    Code:
    ChangeSelection(lstSortedLines.SelectedIndex)
    But this line gets tagged with the following error:

    ChangeSelection is not declared.
    Last edited by David RH; Apr 14th, 2006 at 07:46 PM.

  7. #7
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Selection in listbox selects item in another listbox

    try:

    this.ChangeSelection

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Posts
    434

    Re: Selection in listbox selects item in another listbox

    Quote Originally Posted by Techno
    try:

    this.ChangeSelection
    "this" is not a recognized anything in VB asp.net that I can find.

    This is too wierd. Why can't I access a function that I have decared public?

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Posts
    434

    Re: Selection in listbox selects item in another listbox

    On further TS I've come to this point:

    I couldn't figure out how to make the call into an object reference so I declared the function as Public Shared. Now I get the following error from the listbox property change call in the function:
    "Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."
    Code:
        Public Shared Function ChangeSelection1(ByVal Value As Short)
            lstUserOutPut.SelectedIndex = Value
        End Function
    lstUserOutPut is the underlinded error item.

    I tried fully qualifying it but it's still a no go.

    Any ideas?

  10. #10
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Selection in listbox selects item in another listbox

    David:

    What is the object heirarchy?

    Which page derives from which other and what are the class names?

    I can then tell you how it would be set up.

    Shared means that if you had more than one of the same class, all those versions would have the ONE function in common rather than a function for each class. Not relevant to what you are doing, so it's not appropriate here.

    Here is an example, lets say the drop down is on control myLoginForm which is created as part of the request to myForm, the main page.

    To get to the function, you would from myForm use myLoginForm.AccessorFunction

    I probably just muddled everything up for you =P

    If you are having a hard time following, post the two code pages for your two objects and Ill look at them.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Posts
    434

    Re: Selection in listbox selects item in another listbox

    I had already done a good job of muddling myself up.

    I have no clue what object heirarchy is or where to find that info. I created 2 webforms and placed a listbox on each. They were created by right clicking on the project in the solution explorer and choosing add webform. These 2 forms are not the first forms that were created for this project and neither is the main form.

    I started out trying to get the selection of an item in the listbox on one form to select the same index of the listbox on the second form with this:
    Code:
    Private Sub lstUserOutPut_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstUserOutPut.SelectedIndexChanged
        lstSortedLines.SelectedIndex = lstUserOutPut.SelectedIndex
    End Sub
    But I can't see the other form because it's protected. So I tried to create an accessing function like this:
    Form1
    Code:
    Public Function ChangeSelection1(ByVal Value As Short)
        lstUserOutPut.SelectedIndex = Value
    End Function
    
    Private Sub lstUserOutPut_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstUserOutPut.SelectedIndexChanged
        ChangeSelection2(lstUserOutPut.SelectedIndex)
    End Sub
    Form2
    Code:
        Public Function ChangeSelection2(ByVal Value As Short)
        lstSortedLines.SelectedIndex = Value
    End Function
    
    Private Sub lstSortedLines_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstSortedLines.SelectedIndexChanged
        ChangeSelection1(lstSortedLines.SelectedIndex)
    End Sub
    But both ChangeSelection1 and ChangeSelection2 get highlited as not being declared (can't see them).

    I'd like to be able to use the first method but I just don't seem to be able the grasp the scope issues of webforms in ASP.net. This is my first web app with ASP.net so I'm pretty clueless and VS's docs are pretty thin on the subject.

    Thanks

    David
    Last edited by David RH; Apr 17th, 2006 at 11:29 AM.

  12. #12
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Selection in listbox selects item in another listbox

    The accessor functions are perfect.

    The lines where you attempt to use them need some scopy identity however. What is the name of the classes for those controls? Let's assume the first is called MyControl1 and the second is MyControl2.

    You should be able to change:
    ChangeSelection2(lstUserOutPut.SelectedIndex)

    to

    MyControl2.ChangeSelection2(lstUserOutPut.SelectedIndex)

    and

    ChangeSelection1(lstSortedLines.SelectedIndex)

    to

    MyControl1.ChangeSelection1(lstSortedLines.SelectedIndex)

    Assuming that MyControl2 is the physical name of the control as added to your form... like so:

    <uc1:MyControlClass id="MyControl1" runat="server"></uc1:MyControlClass>

    Finally, if both sets of code are in two different user controls (which is what I beleive you are describing) you may have to manually define the control:

    The following line is C-Sharp because I do not know the VB Equivelant, but it will probably be sufficient to get you to your answer:

    MyNameSpace.MyControlClass MyControl1 = ((MyNameSpace.MyControlClass) Page.FindControl("MyControl1"));

    What that line does is create an instance of "MyControlClass" with the identifier "MyControl1" by finding the control called "MyControlOne" on the parent form (the Page). That way, you can then access MyControl1.AnyPublicFunction

    Hope that helps.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Posts
    434

    Re: Selection in listbox selects item in another listbox

    Thanks LR

    But that C# code looks like @#$&*()$%@&*()_&_@#$% to me.

    I'm strictly a VB (Classic until a couple of months ago) kinda guy. I'm really dissappointed that in .net that they have gone out of their way to try and make everything as complex and convoluted as possible. I've ordered some books on ASP.net but I'm not real hopeful.

    With this project I'm converting a stand alone VB.net app to web based and I think that there is just going to be a bunch of things that it can't do as a web app.

    The reason that I have these 2 outputs on different pages is that the user needs to be able to see the data in the first listbox in its entirety without scrolling. For it to be readable and see the whole page it takes up the whole screen. Hence the second output listbox is on it's own page. The second window is used to maniplate the data in the first window with buttons that programatically move the items in both windows.

    The main page has over 80 controls that take input from the user. Most of these have limits and restrictions on what can be input. In the stand alone I coded the restrictions into events of the controls. In the web app alot of the events and methods don't exist.

    I'm doing some reevaluating and I may have to drop several of the features from the web based version.
    Last edited by David RH; Apr 18th, 2006 at 02:38 PM.

  14. #14
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Selection in listbox selects item in another listbox

    Quote Originally Posted by Lord_Rat
    MyNameSpace.MyControlClass MyControl1 = ((MyNameSpace.MyControlClass) Page.FindControl("MyControl1"));
    Probably:

    VB Code:
    1. Dim MyControl1 as MyNameSpace.MyControlClass = DirectCast(Page.FindControl("MyControl1"), MyNameSpace.MyControlClass)

    If you even have to cast it....you can prolly get away with just assigning it:

    VB Code:
    1. Dim MyControl1 as MyNameSpace.MyControlClass = Page.FindControl("MyControl1")
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    May 2002
    Posts
    434

    Re: Selection in listbox selects item in another listbox

    I got distracted working on another project. I'm finally got back to it.

    That did the trick.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width