Get the index of the selected button in radiobuttonlist

S

svein.erik.storkas

How can i get the index of the button that is selected in a
radiobuttonlist?
I have a radiobuttonlist that contains 5 buttons, and i do a switch on
what index that is selected, like this:

Session["rblMarked"] = GetIndexString( rblMarked.SelectedIndex );

Session["rblCustomersSeriosly"] = GetIndexString(
rblCustomersSeriosly.SelectedIndex );

....

then the method GetIndexString:

private string GetIndexString( int index )
{
string temp = "";

switch(index) {
case 0:
temp = "Stor innverknad";
break;
case 1:
temp = "Ein del innverknad";
break;
case 2:
temp = "Middels innverknad";
break;
case 3:
temp = "Lite innverknad";
break;
case 4:
temp = "Ingen innverknad";
break;
}

return temp;
}

But the number 0 is coming from .SelectedIndex no matter what button i
push...why?

I really appreciate some quick help..i've got time pressure on this
project! :)

Thanks!
Svein Erik.
 
J

Jason Kester

You might want to step through your code and make sure you're not
re-filling that list before you get around to reading from it. That's
the most common reason for this happening. If you put a breakpoint at
the first line of that GetIndexString method and another at the first
line of FillThatList() (or whatever is binding that list), you might
see that FillThatList gets called first. If that happens, it will
throw away your selection and you'll get the results you're seeing.

The solution is usually to stick FillThatList() into an if(!isPostBack)
block.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
S

svein.erik.storkas

Thanks, but i have set breakpoints, and there is nothing that gets
called in between...The only thing worth mentioning is that both Value
and Text fields of the radiobuttonlist properties are blank...does this
have anything to do with the "problem"?
 
G

Guest

Are you databinding every time in page_load by any chance?

the databinding should happen like below

if(!Page.PostBack())
{
//databind the radio button list here
}
 
J

Jason Kester

And the Items collection on the list? Does that come back empty too?

If not, you can spin through the list and look for the first one marked
..Selected. A bit more effort, but it will get you there.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
S

svein.erik.storkas

I finally found a "hack", i had to set value to   . Then it worked
like it should..it seemes that sometimes you have to make a workaround
out of the standards..?

Well, thank you very much for your help :)
 
Joined
Nov 5, 2008
Messages
1
Reaction score
0
radiobuttonlist returns 0

Hi, I have the same problem. My radiobuttonlist properties are blank and i'm trying get the selectedindex. But the problem is, it returns zero always.
Can't find any help on this. can someon help please...????

Thanks, but i have set breakpoints, and there is nothing that gets
called in between...The only thing worth mentioning is that both Value
and Text fields of the radiobuttonlist properties are blank...does this
have anything to do with the "problem"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top