如何获取所选单选框的值 [英] how to retrieve value of a selected radio box
问题描述
对于单选按钮的网格视图,我有以下代码.单选按钮属于同一组.
I have the following code for grid view with radio buttons.The radio button belongs to the same group.
<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>
.cs代码如下-:
The .cs code is as follows-:
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
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.
推荐答案
rb1 = (RadioButton)row.FindControl("radioButton1");
是导致错误的行.您只在寻找radioButton1.您必须尝试这样的事情
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
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
使用Request [控件ID"],它将返回您选择的单选按钮值.
就您而言
Use Request["ID of the control"], and it will return you the selected Radio button value.
In your case
Request["radioButton1"]
尝试它可能会有所帮助
这篇关于如何获取所选单选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!