简体   繁体   English

使用C#在数据列表中的SelectedIndexChanged事件上获取RadioButtonList的SelectedValue

[英]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.

相关问题 在 asp.net C# 中第一次执行该事件后,如何让 RadioButtonList_SelectedIndexChanged 事件处理程序执行? - How do I get the RadioButtonList_SelectedIndexChanged event handler to execute after the first execution of that event in asp.net C#? RadioButtonList SelectedIndexChanged事件未触发 - RadioButtonList SelectedIndexChanged event not firing C#组合框SelectedIndexChanged事件 - C# Combobox SelectedIndexChanged event 在C#中使用Databind,DropDownList不会在SelectedIndexChanged上触发事件 - DropDownList doesn't Fire Event on SelectedIndexChanged Using Databind in C# ASP.Net RadioButtonList SelectedIndexChanged事件未触发 - ASP.Net RadioButtonList SelectedIndexChanged event not firing dropDownList的SelectedIndexChanged事件没有触发c# - SelectedIndexChanged event of dropDownList not firing c# 当RadioButtonList选择asp.net C#时,SelectedIndexChanged无法正常工作? autopostback还设置为true吗? - SelectedIndexChanged not working while RadioButtonList selected asp.net c#? autopostback also set to true? Itemcommand 不会在使用 c# 的数据列表中触发按钮单击事件 - Itemcommand not firing on button click event in a datalist using c# 如何从动态RadioButtonList数组中获取SelectedValue? - How to get SelectedValue from array of dynamic RadioButtonList? 在Dropdown SelectedIndexChanged上触发数据列表OnItemDataBound事件 - Fire Datalist OnItemDataBound event on Dropdown SelectedIndexChanged
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM