【问题标题】:How to multiple render radio buttons inside table如何在表格内多个渲染单选按钮
【发布时间】:2019-08-07 12:58:03
【问题描述】:

我有一个网络表单应用程序,一个页面有这个表单,其中包含 5 个问题,其中多个选项作为单选按钮,如图所示。我必须从数据库、标题值和绑定单选按钮值与数据库字段中获取问题。 DB部分实际上已经解决了,现在我尝试将其渲染为DataTable,但在我看来DT只渲染文本,我不知道如何在其中渲染单选按钮。

欢迎提出任何建议。此外,我必须从代​​码隐藏中动态添加这些单选按钮。

基本上,我需要显示表中的值(从数据库中获取),并且稍后还要读取该值,以存储响应。到目前为止,似乎最好的选择是使用 GridView 而不是 DataTable,但我仍然需要有关如何在单个单元格内呈现 aspx 控件的帮助。

【问题讨论】:

  • 我实际上会使用中继器,因为它为您提供了更多的样式灵活性。
  • 你用什么语言写的? C# 还是 VB?
  • 您可以使用 GridView 的OnRowDataBound 事件动态构建每一行,包括添加按钮。
  • 我正在使用 C#。我不知道如何使用 RadioButton 构造函数创建一个单选按钮,然后在后面的代码中将其渲染到某个特定位置,例如表格单元格。 MSDN 文档中的示例没有任何由转发器呈现的控件,而是我设法创建的纯文本,但 radioButtons 仍然是一个问题。
  • @JohnPete22 好的,到目前为止,我有两个中继器在做他们应该做的,但是当我嵌套它们时,另一个没有显示

标签: asp.net datatable webforms


【解决方案1】:

好的,这是给你的一些代码。这应该会给你一个关于如何继续的不错的蓝图。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="_testPWforSO.aspx.cs" Inherits="_testPWforSO" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>


</head>
<body>
    <form id="form1" runat="server">
        <div>

            <asp:Repeater ID="rptTest" runat="server" OnItemDataBound="rptTest_DataBinding" EnableViewState="true">
                <HeaderTemplate>
                    <table style="border-color: #CCCCCC; width: 100%; border-collapse: collapse;">
                        <tr>
                            <th></th>
                            <th>Not Good</th>
                            <th>Somewhat Good</th>
                            <th>Good</th>
                            <th>Very Good</th>
                            <th>Excellent</th>
                        </tr>
                </HeaderTemplate>

                <ItemTemplate>
                    <tr>
                        <td><asp:Label ID="lblQuestion" runat="server" /></td>
                        <td><input type="radio" id="notGood" runat="server" name="option" /></td>
                        <td><input type="radio" id="somewhatGood" runat="server" name="option" /></td>
                        <td><input type="radio" id="good" runat="server" name="option" /></td>
                        <td><input type="radio" id="veryGood" runat="server" name="option" /></td>
                        <td><input type="radio" id="excellent" runat="server" name="option" /></td>
                    </tr>
                </ItemTemplate>

                <FooterTemplate>
                    <hr />
                    </table>
                </FooterTemplate>

            </asp:Repeater>

        </div>

        <br />
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>

    </form>
</body>
</html>
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;

using System.Web.UI.HtmlControls;

public partial class _testPWforSO :  System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Collection<string> questions = new Collection<string>() { "Test Question 1", "Test Question 2", "Test Question 3" };
            rptTest.DataSource = questions;
            rptTest.DataBind();
        }
    }

    protected void rptTest_DataBinding(object sender, RepeaterItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            string row = Convert.ToString(e.Item.DataItem);

            if (row != null)
            {
                Label question = (Label)e.Item.FindControl("lblQuestion");

                if (question != null)
                {
                    question.Text = row;
                }
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        // you'd probably want some validation on the radio buttons to ensure they've been clicked, either server or client side
        if (Page.IsValid)
        {
            foreach (RepeaterItem row in rptTest.Items)
            {
                if (row.ItemType == ListItemType.Item || row.ItemType == ListItemType.AlternatingItem)
                {
                    // need to look up the radio buttons. If you used an Asp:RadioButton or something else, it'll probably be easier, but generic <input type='radio'> offer better styling options
                    HtmlInputRadioButton notGood = (HtmlInputRadioButton)row.FindControl("notGood");
                    if (notGood != null && notGood.Checked)
                    {
                        // do something, and then continue to next row
                        continue;
                    }


                }
            }

            // either save data and then rebind new questions, or do something else
            Collection<string> questions = new Collection<string>() { "Test Question 4", "Test Question 5", "Test Question 6" };
            rptTest.DataSource = questions;
            rptTest.DataBind();
        }
        else
        {
            // display error
        }
    }
} 

我只是在问题中使用了Collection&lt;string&gt;,这就是为什么我在 DataBinding 中转换为字符串。如果你传入一个不同的对象,那么显然你会强制转换为那个对象。

【讨论】:

    猜你喜欢
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2022-11-01
    • 1970-01-01
    • 2013-03-06
    • 2017-10-23
    相关资源
    最近更新 更多