首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在datalist中获取选中的单选按钮文本?

如何在datalist中获取选中的单选按钮文本?
EN

Stack Overflow用户
提问于 2012-04-07 13:40:28
回答 1查看 2.4K关注 0票数 1

在下面的数据列表中代表了一组问题和答案,当用户使用javascript点击提交按钮时,如何检查用户是否选择了正确答案单选按钮?

答案存储在数据库中。

数据列表:

代码语言:javascript
运行
复制
<asp:DataList ID="DataList1" runat="server" DataKeyField="Qno" 
        DataSourceID="SqlDataSource1">
        <ItemTemplate>
            Qno:
            <asp:Label ID="QnoLabel" runat="server" Text='<%# Eval("Qno") %>' />
            <br />
            Question:
            <asp:Label ID="QuestionLabel" runat="server" Text='<%# Eval("Question") %>' />
            <br />
            <asp:RadioButton ID="RadioButton1" runat="server" Text='<%# Eval("Ans1") %>' />
            <br />
            <asp:RadioButton ID="RadioButton2" runat="server" Text='<%# Eval("Ans2") %>' />
            <br />
            <asp:RadioButton ID="RadioButton3" runat="server" Text='<%# Eval("Ans3") %>' />
            <br />
            <asp:RadioButton ID="RadioButton4" runat="server" Text='<%# Eval("Ans4") %>' />
            <br />
            <asp:Button ID="Button2" runat="server" Text="Submit" />
            <br />
        </ItemTemplate>
    </asp:DataList>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-07 14:19:06

为DataList中的验证提供一个验证参数:CommandName=“CommandName”

添加DataList1的OnItemCommand事件:*OnItemCommand="DataList1_OnItemCommand"*

在后面的代码中,填写*DataList1_OnItemCommand*事件操作:

代码语言:javascript
运行
复制
protected void DataList1_OnItemCommand(object sender, DataListCommandEventArgs e)
{
 if (String.Equals(e.CommandName, "Validate"))
 {
  DataListItem dataItem = (DataListItem )e.Item;
  RadioButton rbtn1 = (RadioButton)dataItem.FindControl("RadioButton1");
  RadioButton rbtn2 = (RadioButton)dataItem.FindControl("RadioButton2");
  RadioButton rbtn3 = (RadioButton)dataItem.FindControl("RadioButton3");
  RadioButton rbtn4 = (RadioButton)dataItem.FindControl("RadioButton4");

  // Code to check which radio button was checked.
  if(rbtn1 != null && rbtn1.Checked)
  {

  }
  else if(rbtn2 != null && rbtn2.Checked)
  {

  } //Perform these for the remaining two check boxes
 }
}

根据选中的复选框执行所需的操作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10052108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档