Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have the following code for grid view with radio buttons.The radio button belongs to the same group.


XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" Width="312px">
                <Columns>
                <asp:BoundField DataField ="Column1" HeaderText ="ProductName"  />
                <asp:BoundField DataField ="Column2" HeaderText ="ProductID" />
                <asp:TemplateField  HeaderText ="Select one">
                <ItemTemplate>

<asp:RadioButton ID ="radioButton1" runat="server" GroupName ="productDB" AutoPostBack ="true" OnCheckedChanged ="Rb_CheckChanged" />
                </ItemTemplate></asp:TemplateField>
                </Columns>

                </asp:GridView>



The .cs code is as follows-:

C++
String drugName;
        DataTable drugsTable = new DataTable();
        DataSet drugs =new DataSet();

        drugName = TextBox2.Text;
        GridView1.Visible = true;
        drugs = proxyService.fdbService(drugName);
        if (drugs.Tables[0].Rows.Count!= 0)
        {
            GridView1.DataSource = drugs.Tables[0].DefaultView;
            GridView1.DataBind();
            Label2.Enabled = false;
            Label2.Text = "";
        }
        else
        {
            Label2.Enabled = true;
            Label2.Text = "Search resulted in no Enteries";
            GridView1.Visible = false;
        }




At run time each row will have one radio button...

I want a solution which helps me to retrieve the value for which the radio button is selected.

I am currently using the following code


C#
foreach (GridViewRow row in GridView1.Rows)
 {
     rb1 = (RadioButton)row.FindControl("radioButton1");
     if (rb1.Checked = true)
     {
         drugId = (String)row.Cells[1].Text;
     }
     i += 1;
 }



even if i select any radio button it retrieves only the value of the 1st radio button.

Let me know wat has to be done to correct the mistake?

Thanks.
Posted
Comments
CodeHawkz 26-Apr-11 5:17am    
Run your application and go to this particular webpage. I hope you know to use a tool like FireBug (http://getfirebug.com/) to check the HTML of the page. You will need Firefox to use this tool though. Although, you can use developer tools in IE 8 :)

Use such a tool to check the names of your gridview. Check whether the radio buttons are rendered with the same name "radiobutton1" or in an incremental way like Wayne suggested. This will help you capture your problem.
bsb25 26-Apr-11 6:45am    
are u talkin about value of radio button or name of radio button.Bcoz value for all radio button is "Radio Button1"

rb1 = (RadioButton)row.FindControl("radioButton1");

is the line that is causing the error. You are only looking for radioButton1. You would have to try something like this

C#
foreach (GridViewRow row in GridView1.Rows)
            {
                string rbName = "radioButton" + i.ToString();
                rb1 = (RadioButton)row.FindControl(rbName);
                if (rb1.Checked = true)
                {
                    drugId = (String)row.Cells[1].Text;
                }
                i += 1;
            }


Hope this helps
 
Share this answer
 
Use Request["ID of the control"], and it will return you the selected Radio button value.

In your case
Request["radioButton1"]
Try that it might help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900