VS 2010 Radiobuttonlist reverts from selected item-VBForums
Results 1 to 12 of 12

Thread: Radiobuttonlist reverts from selected item

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Stockholm, Sweden
    Posts
    308

    Radiobuttonlist reverts from selected item

    Hi,

    Im guessing this has been asked before. In fact I asked it myself some time ago about a dropdownlist.

    However the same solution doesn't seen to work, and I can't find the solution in the forum.

    Im populating a radiobuttonlist in code on page load, allthough not when postback is true (Ive checked this by the way).

    Autopostback is set to true, viewstate is enabled (everywhere I can think of, allthough only in the content page, if that matters)

    When I select an item, the postback happens, the populating does not happen, and the selection reverts back to the first item, which was never selected. It really looks stupid.

    Nowadays I have the knowledge to do this in javascript, but I would prefer not to. It cant be that it should work like this, can it? The control would be useless. I want the index that is selected.

    Thanks.

    Fuga.
    Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,231

    Re: Radiobuttonlist reverts from selected item

    The code.
    Slow as hell.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Stockholm, Sweden
    Posts
    308

    Re: Radiobuttonlist reverts from selected item

    Here you go, sapator. Thanks!

    In a sub, called from page load (including the msgbox check, which fires):
    Code:
            If Page.IsPostBack = True Then
                MsgBox("Postback")
            End If
            If Page.IsPostBack = False Then
                Me.RadioButtonList1.Items.Clear()
                Me.RadioButtonList1.Items.Add("Placera hr")
            End If
    and then further down, inside a loop:
    Code:
                If Me.IsPostBack = False Then
                    Dim rbitem As New ListItem
                    rbitem.Enabled = False
                    rbitem.Text = Left(stRw.Item(3), 45) & "..."
                    Me.RadioButtonList1.Items.Add(rbitem)
                    Me.RadioButtonList1.Items.Add("Placera hr")
                End If
    and the aspx:
    Code:
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server" ForeColor="Black" Font-Size="0.9em" AutoPostBack="True" ViewStateMode="Enabled">
                    </asp:RadioButtonList>
    so the postback is true, and the populating doesnt happen, but still the first item gets selected.

    Fuga.
    Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Stockholm, Sweden
    Posts
    308

    Re: Radiobuttonlist reverts from selected item

    And before you ask

    Im using the list to allow the user to chose where to put an item he creates. When doing so, he is only allowed to place the item between already existing item, so as to create "in-between" levels. Hence the disabled items comes from existing data in db, and the enabled items are named "Placera hr", which means "place here".

    Fuga.
    Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,231

    Re: Radiobuttonlist reverts from selected item

    I don't really understand what you want.
    This will work:
    Code:
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Page.IsPostBack = True Then
    'never put a messagebox in asp.net!
                MsgBox("Postback")
            End If
            If Page.IsPostBack = False Then
                Me.RadioButtonList1.Items.Clear()
                Me.RadioButtonList1.Items.Add("Placera hr")
            End If
    
            If Me.IsPostBack = False Then
                Dim rbitem As New ListItem
                rbitem.Enabled = False
                rbitem.Text = "something"
                Me.RadioButtonList1.Items.Add(rbitem)
                Me.RadioButtonList1.Items.Add("Placera hr")
            End If
        End Sub
    What exactly is the problem?
    Slow as hell.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Stockholm, Sweden
    Posts
    308

    Re: Radiobuttonlist reverts from selected item

    What exactly is the problem?
    The problem is that the wrong item is selected after postback, which means that it looks bad, and also that I cant use the value or index in code later on, because its always 0.

    'never put a messagebox in asp.net!
    Why Is there something I dont know about this? In fact Im beginning to think you should never put anything in asp.net

    Fuga.
    Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess

  7. #7
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Radiobuttonlist reverts from selected item

    Quote Originally Posted by Fuga View Post
    Why Is there something I dont know about this? In fact Im beginning to think you should never put anything in asp.net

    Fuga.
    You shouldn't use a MsgBox in asp.net as it is a winforms control and will not behave the way you expect it to. You may be seeing it on your development machine as you are the host but once your app is deployed to a webserver somewhere the users of you site will not see the MsgBox as it will just be displayed on the server all alone in a datacenter.

    Unless of course MsgBox is a function that writes client side Javascript alert?

    Don't give up on asp.net just yet!

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Stockholm, Sweden
    Posts
    308

    Re: Radiobuttonlist reverts from selected item

    it will just be displayed on the server all alone in a datacenter.
    I guess that would put a smile on someones face

    Unless of course MsgBox is a function that writes client side Javascript alert?
    Hm.

    Actually I just used it to check whether there was a postback or not, but I confess I hadnt thought about this so there might be a msgbox somewhere on the site. I better check that out!

    Thanks.
    Fuga.
    Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess

  9. #9
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,231

    Re: Radiobuttonlist reverts from selected item

    Ye check the messageboxes they are useless in deployment.
    Now.If you do a postback on the page then you run this:
    Me.RadioButtonList1.Items.Clear()
    So you clear everything.How do you expect something to stay checked?
    Remember doing a postback the second time does not mean that the page_load will not run.
    Now if that is what you want from code then you can do something simple.Using session or viewstate you can save the checked value and after the postback you can pass it to your radiobutton.
    Slow as hell.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Stockholm, Sweden
    Posts
    308

    Re: Radiobuttonlist reverts from selected item

    Now.If you do a postback on the page then you run this:
    Me.RadioButtonList1.Items.Clear()
    So you clear everything.How do you expect something to stay checked?
    But this is what I don't understand. I thought that if I put it inside the ispostback=false then it wouldn't repopulate?

    I've tried using session, but the value doesn't seem to be set until after postback, which means that I have to chose between having the correct value stored in session, OR having the correct visual selection. But none of it makes sense to me because even if the repopulating doesn't happen, the index still turns back to the first item. So the way things are, the session variable would be 0 and the selected index would be 0. No matter what I actually select.

    Thanks
    Fuga.
    Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess

  11. #11
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    4,231

    Re: Radiobuttonlist reverts from selected item

    Use the radiobutton index_change or whatever it has available(i can't remember now) to set you session value.
    I would have to say that i really cannot understand what you are looking for.Maybe my English is bad of maybe you are confused.
    If you put your code inside "If Not Page.IsPostBack" then you will have to refresh with F5 or with the browser refresh to get inside the second time so when you select your radio then if your code is inside the If Not Page.IsPostBack it will not get accessed.I would suggest that you do your coding on radio_index changed or selection_changed of whatever is the correct function instead of Page_Load.There you will have better access on what is it that you want to do.
    Slow as hell.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Stockholm, Sweden
    Posts
    308

    Re: Radiobuttonlist reverts from selected item

    Use the radiobutton index_change or whatever it has available(i can't remember now) to set you session value.
    I would have to say that i really cannot understand what you are looking for.Maybe my English is bad of maybe you are confused.
    Thanks for your help. Im quite certain its me who is confused

    From the beginning I tried both index change and text (I think) change, but the problem was the changing didnt happen unless there was a (auto-)postback, which - then - triggered the clearing of the list (happening on page_load).

    When I tried to put the clearing and populating inside the ispostback = false section, it still didnt work.

    But to be honest, Ive been fiddling around with this so long now so Im getting a bit confused as to when I did what. Ive mad a page that uses javascript now, and also one that has a different design so the buttonlist isnt needed, but Ill try it again and make sure Im doing it the way youre saying, because the design with the buttonlist is better than the others.

    thanks again.
    Fuga.
    Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.