本文介绍了如何在同一GroupName中获取所选单选按钮的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有四个无线电按钮,组名全部相同。
i想要在代码隐藏中获取所选的一个文本。
这就是我在设计页面中添加控件的方法......
i have four radio butons,Group name of all is same.
i want to get the text of selected one in codebehind.
this is how i have added the controls in design page...
<tr>
<td colspan="4">
<asp:RadioButton ID="opt1" runat="server" GroupName="a" />
<asp:RadioButton ID="opt2" runat="server" GroupName="a" />
<asp:RadioButton ID="opt3" runat="server" GroupName="a" />
<asp:RadioButton ID="opt4" runat="server" GroupName="a" />
</td>
</tr>
代码填充radiobuttons文字...
code to populate the radiobuttons text...
try
{
dsques = dbcon.ExecuteDataSet("select * from tblExamineeExam where examineeid='" + userid + "'");
if (dsques.Tables.Count > 0 && dsques.Tables[0].Rows.Count > 0)
{
int i = Convert.ToInt32(lblqno.Text) - 1;
//for (int i = no; i < dsques.Tables[0].Rows.Count; i++)
//{
lblqno.Text = i.ToString();
lblquestion.Text = dsques.Tables[0].Rows[i][1].ToString();
opt1.Text = dsques.Tables[0].Rows[i][2].ToString();
opt2.Text = dsques.Tables[0].Rows[i][3].ToString();
opt3.Text = dsques.Tables[0].Rows[i][4].ToString();
opt4.Text = dsques.Tables[0].Rows[i][5].ToString();
//}
}
}
catch (Exception ex)
{
}
现在我想要选择的radiobutton文本..
now i want text of slected radiobutton..
sttring strAnswr = ;//need code here
谢谢!
Thanks!
推荐答案
<asp:RadioButton ID="opt1" runat="server" Text="opt1 radioButton selected" GroupName="a"/>
<asp:RadioButton ID="opt2" runat="server" Text="opt2 radioButton selected" GroupName="a" />
<asp:RadioButton ID="opt3" runat="server" Text="opt3 radioButton selected" GroupName="a" />
<asp:RadioButton ID="opt4" runat="server" Text="opt4 radioButton selected" GroupName="a" />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
代码中我们可以看出我们的选中如下文字
in code being we can make out our selected text like below
protected void Button1_Click(object sender, EventArgs e)
{
string checkedval="";
if (opt1.Checked)
{ checkedval = opt1.Text; }
else if (opt2.Checked)
{ checkedval = opt2.Text; }
else if (opt3.Checked)
{ checkedval = opt3.Text; }
else if (opt4.Checked)
{ checkedval = opt4.Text; }
Response.Write(checkedval);
}
其他方式是使用radiobutton列表
Other way is to use radiobutton list
<asp:radiobuttonlist id="RadioButtonList1" runat="server" xmlns:asp="#unknown">
<asp:listitem value="1">opt1</asp:listitem>
<asp:listitem value="1">opt2</asp:listitem>
<asp:listitem value="1">opt3</asp:listitem>
<asp:listitem value="1">opt4</asp:listitem>
</asp:radiobuttonlist>
protected void Button1_Click(object sender, EventArgs e)
{
string checkedval="";
checkedval= RadioButtonList1.SelectedItem.Text;
Response.Write(checkedval);
}
string strAnswr= string.Empty;
if(opt1.Checked)
{
strAnswr = opt1.Text;
}
else if(opt2.Checked)
{
strAnswr = opt2.Text;
}
else if(opt3.Checked)
{
strAnswr = opt3.Text;
}
else
{
strAnswr = opt4.Text;
}
如果用 RadioButtonList 绑定它可以更简单。
It can be more simple if you bind it with RadioButtonList.
//suppose you have
<asp:radiobuttonlist id="RadioButtonList1" runat="server" xmlns:asp="#unknown">
</asp:radiobuttonlist>
string strAnswr= RadioButtonList1.SelectedItem.Text; // simple
希望它可以帮到你。
谢谢。
Hope it helps you.
Thanks.
这篇关于如何在同一GroupName中获取所选单选按钮的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!