[英]Get SelectedValue of RadioButtonList on SelectedIndexChanged event in a datalist using C#
I've created a DataList
in which there are 2 Label
and a RadioButtonList
. 我创建了一个DataList
,其中有2个Label
和一个RadioButtonList
。 The 2 Label
contains question ID and the question in which the Question ID is hidden, and the RadioButtonList
contains the options. 2 Label
包含问题ID和隐藏了问题ID的问题,而RadioButtonList
包含选项。 All these controls are bounded. 所有这些控件都是有界的。 Here's the code: 这是代码:
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="Que_id" Visible="false" runat="server" Text='<%# Eval("question_id") %>'></asp:Label>
<asp:Label ID="Question" runat="server" Text='<%# Eval("question") %>'></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" AutoPostBack="true" RepeatColumns="2" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
</asp:RadioButtonList>
</ItemTemplate>
</asp:DataList>
What I'm trying to do is whenever user selects an option in the RadioButtonList
the selected option and its question id should get stored in the database. 我想做的是每当用户在RadioButtonList
选择一个选项时,所选选项及其问题ID都应存储在数据库中。 It's easy to store the whole DataList
at the button click event, but I want to store the response side by side as the user selects the option for a particular question. 在按钮单击事件中存储整个DataList
很容易,但是当用户选择特定问题的选项时,我想并排存储响应。
I don't know how to get the question ID on SelectedIndexChanged
event but I tried the following code on SelectedIndexChanged
to get the selected option value but this too doesn't work. 我不知道如何获得的问题ID SelectedIndexChanged
事件,但我想在下面的代码SelectedIndexChanged
获得选择的选项值,但是这也不能正常工作。
RadioButtonList rbl = (RadioButtonList)DataList1.FindControl("RadioButtonList1");
string answer=rbl.SelectedValue;
For more clarification, I brought this data from database in a DataSet
and filtered it for the question ID, question and options. 为了进一步说明,我将这些数据从数据库引入了数据DataSet
,并针对问题ID,问题和选项进行了过滤。 The values are being set during the Page_Load in the if(!IsPostBack). 这些值是在Page_Load期间在if(!IsPostBack)中设置的。
Try below, 试试下面,
modify 修改
<asp:DataList ID="DataList1" runat="server">
To 至
<asp:DataList ID="DataList1" OnItemCommand="DataList1_command" runat="server">
Assuming you have a <asp:Button
and id as button1
and OnClick as bclick
假设您有一个<asp:Button
和id为button1
而OnClick为bclick
void bclick(object sender, DataListCommandEventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
HtmlInputRadioButton radio = (item.FindControl("radioButton") as HtmlInputRadioButton);
if (radio.Checked)
{
// this is you checked radio
}
}
}
You can use a HiddenField to store the question ID in the DataList (it is more "natural" than an invisible Label): 您可以使用HiddenField将问题ID存储在DataList中(它比不可见的Label更“自然”):
<asp:HiddenField ID="hiddenQuestionID" runat="server" Value='<%# Eval("question_id") %>' />
In the event handler of the RadioButtonList, you can use the sender
parameter to retrieve the control, and then get the selected value. 在RadioButtonList的事件处理程序中,可以使用sender
参数来检索控件,然后获取选定的值。 The NamingContainer
of the RadioButtonList also contains the HiddenField, from which you can get the question ID: RadioButtonList的NamingContainer
还包含HiddenField,从中可以获取问题ID:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonList rbl = sender as RadioButtonList;
string answer = rbl.SelectedValue;
HiddenField hiddenQuestionID = rbl.NamingContainer.FindControl("hiddenQuestionID") as HiddenField;
string questionID = hiddenQuestionID.Value;
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.