Retrieving selected value from RadioBottonList

I have an ASP.NET 2.0 RadioBottonList that is being populated with 1 blank entry and the directory listing of an image folder.

.aspx
<asp:RadioButtonList ID="rblLogo" runat="server" />

.aspx.vb
Const IMAGE_DIRECTORY As String = "../Images/Subscribers/"
Dim s As String, html As String
Dim value As String
rblLogo.Items.Add(New ListItem("None", ""))
For Each s In Directory.GetFiles(Server.MapPath(IMAGE_DIRECTORY), "*.gif")
    html = "<img src=""" & IMAGE_DIRECTORY & Path.GetFileName(s) & """>"
    value = Path.GetFileName(s)
    rblLogo.Items.Add(New ListItem(html, value))
Next

In a MySQL database I have a `Logo` column that stores the name of of file: "name.gif". When reading from the database I can select the correct image in the RadioButtonList with
    rblLogo.SelectedValue = CMAReader("Logo")

but when trying to set a parameter to write back to the database I get an empty string
    CMACommand.Parameters.AddWithValue("?_Logo", rblLogo.SelectedValue)

All my other parameters and the stored procedure that is being called works fine. The problem I'm having is how to retrieve the value of the selected RadioButton into the parameter.
DerekWatlingAsked:
Who is Participating?
 
G0ggyCommented:
Stick the radio button list to autopostback=true and stick a MsgBox on the page load event, outside of the Not PostBack:

MsgBox("Value = " & rblLogo.SelectedValue)

Post back with expected/actual results please.
0
 
DerekWatlingAuthor Commented:
MsgBox doesn't work on a WebForm, but by using a Response.Write it is giving me the correct value when selecting a new image.
0
 
G0ggyCommented:
MsgBox works just fine, so I don't know where you get that information.

If the Response.Write is giving you the correct value then the next step is to check your parameter in MySQL, can you trace log MySQL?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
DerekWatlingAuthor Commented:
When trying a MsgBox I get the error:
    System.InvalidOperationException: It is invalid to show a modal dialog or form when the application is not running in UserInteractive mode.
and the FAQ I found says it doesn't work on WebForms.

I've been writing out various values after adding all the parameters but before executing the command and all the values are there except the one from the RadioButtonList. After playing around a bit I have found that the "blank" item
    rblLogo.Items.Add(New ListItem("None", ""))
is causing the problem. Putting in a value, even a space, rather than an empty string makes it work. But this introduces a new problem in that when it reads an empty value from the database it can't find a matching value to set. I can work around this by retuning a space if it is blank, but not a very elegant solution.
0
 
G0ggyCommented:
Yeah it doesn't seem right to jump through these steps, can you not test for Is DbNull and then circumvent that piece of code?
0
 
DerekWatlingAuthor Commented:
Found the problem: the Page_Load didn't have a "If Not Is Postback ..." block so all the fields were being reinitialised from the database before the update Sub did it's thing.
0
 
G0ggyCommented:
"Stick the radio button list to autopostback=true and stick a MsgBox on the page load event, outside of the Not PostBack:"

Sorry if it wasn't so clear.
0
 
DerekWatlingAuthor Commented:
Still can't get a MsgBox to work in a ASP.NET WebForm and the other resources I've found say it DOES NOT.
0
 
G0ggyCommented:
I believe it is legacy, not a problem, some people use MsgBox's, some people use Console.WriteLine, down to preference I guess, I would not really use it on a website, only for debugging.
0
 
DerekWatlingAuthor Commented:
G0ggy, Ive assigned you the points for pointing me in the right direction.

Just to clarify MsgBox does not work in ASP (never has in any version) as it is a server side function and any popup's on the web need to be server side. It could be used if you are using VB as the client script language, but is then limited to IE. However it would still be a client script and can't be called the way you suggest. MsgBox and Console.WriteLine are VB calls. In ASP you are limited to JavaScript:alert() or Response.Write.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.