Programmatically Selecting Item in Radio Button List

J

John Criswell

I have created a radio button list programmatically.

C#
SqlConnection conn = new SqlConnection("data source=localhost;
integrated security=true; initial catalog=pubs");
SqlCommand cmdAuthors = new SqlCommand("select * from Authors", conn);
SqlDataReader dr;
dr = cmdAuthors.ExecuteReader();
RadioButtonList rblAuthors = new RadioButtonList();
rblAuthors.DataSource = dr;
rblAuthors.DataTextField = "au_fname" + " " + "au_lname";
rblAuthors.DataBind();
dr.Close();
conn.Close();

The first item in the radio button list should be "Johnson White" as
he is the first author in the authors table of the pubs database. He
is the first author because the index on the table is on SSN and he
has the lowest SSN.

Now I want to programmatically "select" the radio button associated
with the listitem that corresonds to "Johnson White." I don't want
the user to make that selection; I want the application to do it on a
page load for example. How do I write the code to "select" or "check"
a radio button item in a radio button itemlist programmatically?

Any help or suggestions will be greatly appreciated.

Thanks,

John Criswell
 
J

John Criswell

Ray,

Your code worked perfectly and did exactly what I wanted it to do. You
said that you had some difficulties with my code. I apologize for that.
I didn't test it to see if it would work. I just put down some sample
code using the famous "pubs" database to communicate my concept. I'm
sorry if my code caused you some difficulties.

On the bright side, I plugged your method into my project, and it worked
the first time. You are an outstanding C# ASP.NET programmer.

Many thanks,

John Criswell
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top