将RadioButtonList放在表格列中

时间:2019-05-15 18:38:22

标签: c# html asp.net ado

我在Web表单asp.net应用程序中动态创建标签和单选按钮列表。然后,我将它们放在桌子上。 我在表中显示实际的单选按钮列表控件时遇到问题。

如何显示控件而不是显示文本? 我的代码是:

string cs = ConfigurationManager.ConnectionStrings["OnlineCheckListConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();
                DataTable dt = new DataTable();
                using (SqlDataAdapter sda = new SqlDataAdapter("spGetApplications", con))
                {
                    sda.SelectCommand.CommandType = CommandType.StoredProcedure;
                    sda.SelectCommand.Parameters.AddWithValue("@uname", "rbrown");

                    sda.Fill(dt);
                }
                if (dt.Rows.Count > 0)
                {
                    string tablestring = "<table border = \"1\" CssClass=\"TestClass\">" +
                        "<tr><td>First Column Heading</td><td>second Column</td></tr>";
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {                
                        Label lbl = new Label();
                        RadioButtonList c = new RadioButtonList();

                        lbl.ID = "Label" + i.ToString();
                        c.ID = "cbl" + i.ToString();

                        lbl.Text += dt.Rows[i][1].ToString() + "<br/>";
                        c.Items.Add(new ListItem("Yes"));
                        c.Items.Add(new ListItem("NO"));

                        c.RepeatDirection = RepeatDirection.Horizontal;
                        //this.Controls.Add(lbl);
                        //this.Form.Controls.Add(c);
                        tablestring = tablestring + "<tr><td>" + lbl.Text.ToString() + "</td><td>" + c + "</td></tr>";
                    }
                    divTable.InnerHtml = tablestring;

Image showing the current result of code

2 个答案:

答案 0 :(得分:1)

RadioButtonList是一个WebControl,可以在另一个WebControl中使用。因此,在代码中,您是通过串联HTML文本来创建表的,因此该表不在Web Forms视图状态模型的域之内。

在HTML文本变量中串联c时,您只是获得c.ToString()返回的值,默认情况下,该值是类型的全名。

话虽如此,请改用System.Web.UI.WebControls.Table类型来建立表格,并向其中添加System.Web.UI.WebControls.RadioButtonList;我在下面留下一个基本示例,您可以将其用作起点:

在您的aspx文件中(在表单元素中的某个位置):

<asp:Table runat="server" ID="myTable"></asp:Table>

在您的代码隐藏文件中:

using System.Web.UI.WebControls;

...

void SomeMethod()
{
    var row = new TableRow();

    var cell = new TableCell();

    var radioButtonList = new RadioButtonList();
    radioButtonList.Items.Add(new ListItem("Yes"));
    radioButtonList.Items.Add(new ListItem("NO"));

    cell.Controls.Add(radioButtonList);

    row.Cells.Add(cell);

    myTable.Rows.Add(row);
}

答案 1 :(得分:1)

我建议您使用AspGridView,而不是纯HTML表。 您可以使用<ItemTemplate><EditItemTemplate>在aspx中添加RadioButtonList

<asp:GridView ID="gvOrders" DataKeyNames="OrderId" runat="server" AutoGenerateColumns="false"
    OnRowEditing="EditCustomer" OnRowDataBound="RowDataBound" OnRowUpdating="UpdateCustomer"
    CssClass="Grid" OnRowCancelingEdit="CancelEdit">
    <Columns>
        <asp:BoundField DataField="ContactName" HeaderText="Customer Name" ReadOnly="true" />
        <asp:BoundField DataField="ShipCity" HeaderText="Ship City" ReadOnly="true" />
        <asp:TemplateField HeaderText="Shipper">
            <ItemTemplate>
                <asp:Label ID="lblShipper" runat="server" Text='<%# Eval("CompanyName")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="lblShipper" runat="server" Text='<%# Eval("ShipperId")%>' Visible="false"></asp:Label>
                <asp:RadioButtonList ID="rblShippers" runat="server">
                </asp:RadioButtonList>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
</asp:GridView>

然后通过RowDataBound

填充数据
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && gvOrders.EditIndex == e.Row.RowIndex)
    {
        RadioButtonList rblShippers = (RadioButtonList)e.Row.FindControl("rblShippers");
        string query = "SELECT * FROM Shippers";
        SqlCommand cmd = new SqlCommand(query);
        rblShippers.DataSource = GetData(cmd);
        rblShippers.DataTextField = "CompanyName";
        rblShippers.DataValueField = "ShipperId";
        rblShippers.DataBind();
        rblShippers.Items.FindByValue((e.Row.FindControl("lblShipper") as Label).Text).Selected = true;
    }
}

enter image description here

这是演示: https://www.aspsnippets.com/demos/406/default.aspx

完整示例: https://www.aspsnippets.com/Articles/Populate-and-save-ASPNet-RadioButtonList-with-Selected-Value-in-Edit-ItemTemplate-of-GridView.aspx