Programmatically Selecting Item in Radio Button List

  • Thread starter Thread starter John Criswell
  • Start date Start date
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
 
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
 
Back
Top