By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

Get the index of the selected button in radiobuttonlist

P: n/a
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.

Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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/

---
Get your own Travel Blog, with itinerary maps and photos!
http://www.blogabond.com/

Nov 19 '05 #2

P: n/a
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"?

Nov 19 '05 #3

P: n/a

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
}

"sv****************@gmail.com" wrote:
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.

Nov 19 '05 #4

P: n/a
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/

---
Get your own Travel Blog, with itinerary maps and photos!
http://www.blogabond.com/

Nov 19 '05 #5

P: n/a
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 :)

Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.