Radiobuttonlist reverts from selected item
Hi,
I´m 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.
I´m populating a radiobuttonlist in code on page load, allthough not when postback is true (I´ve 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 can´t be that it should work like this, can it? The control would be useless. I want the index that is selected.
Thanks.
Fuga.
Re: Radiobuttonlist reverts from selected item
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 här")
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 här")
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 doesn´t happen, but still the first item gets selected.
Fuga.
Re: Radiobuttonlist reverts from selected item
And before you ask :)
I´m 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 här", which means "place here".
Fuga.
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 här")
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 här")
End If
End Sub
What exactly is the problem?
Re: Radiobuttonlist reverts from selected item
Quote:
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 can´t use the value or index in code later on, because it´s always 0.
Quote:
'never put a messagebox in asp.net!
Why:D Is there something I don´t know about this? In fact I´m beginning to think you should never put anything in asp.net :(
Fuga.
Re: Radiobuttonlist reverts from selected item
Quote:
Originally Posted by
Fuga
Why:D Is there something I don´t know about this? In fact I´m 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!
Re: Radiobuttonlist reverts from selected item
Quote:
it will just be displayed on the server all alone in a datacenter.
I guess that would put a smile on someones face :)
Quote:
Unless of course MsgBox is a function that writes client side Javascript alert?
Hm. :blush:
Actually I just used it to check whether there was a postback or not, but I confess I hadn´t thought about this so there might be a msgbox somewhere on the site. I better check that out!
Thanks.
Fuga.
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.
Re: Radiobuttonlist reverts from selected item
Quote:
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.
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.
Re: Radiobuttonlist reverts from selected item
Quote:
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. I´m quite certain it´s me who is confused:(
From the beginning I tried both index change and text (I think) change, but the problem was the changing didn´t 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 didn´t work.
But to be honest, I´ve been fiddling around with this so long now so I´m getting a bit confused as to when I did what. I´ve mad a page that uses javascript now, and also one that has a different design so the buttonlist isn´t needed, but I´ll try it again and make sure I´m doing it the way you´re saying, because the design with the buttonlist is better than the others.
thanks again.
Fuga.