?
Solved

Retrieving selected value from RadioBottonList

Posted on 2007-07-24
10
Medium Priority
?
522 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:DerekWatling
  • 5
  • 5
10 Comments
 
LVL 11

Accepted Solution

by:
G0ggy earned 250 total points
ID: 19554705
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
 

Author Comment

by:DerekWatling
ID: 19554793
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
 
LVL 11

Expert Comment

by:G0ggy
ID: 19554824
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

Author Comment

by:DerekWatling
ID: 19554936
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
 
LVL 11

Expert Comment

by:G0ggy
ID: 19554962
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
 

Author Comment

by:DerekWatling
ID: 19555883
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
 
LVL 11

Expert Comment

by:G0ggy
ID: 19555950
"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
 

Author Comment

by:DerekWatling
ID: 19556025
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
 
LVL 11

Expert Comment

by:G0ggy
ID: 19556122
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
 

Author Comment

by:DerekWatling
ID: 19637344
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question