将 RadioButtonList 放在 table 列中
Place RadioButtonList in a table column
我在网络表单 asp.net 应用程序中动态创建标签和单选按钮列表。之后,我将它们放在 table 中。
我在 table 中显示实际的单选按钮列表控件时遇到问题。
如何显示控件而不是显示文本?
我的代码是:
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;
RadioButtonList
是一个 WebControl
,可以在另一个 WebControl
中使用。在您的代码中,您通过连接 HTML 文本创建了一个 table,因此,在 Web 窗体视图状态模型的域之外。
当您在 HTML 文本变量中连接 c
时,您只是得到 c.ToString()
返回的值,默认情况下它是类型的全名。
话虽如此,请改用 System.Web.UI.WebControls.Table
类型来构建您的 table,并向其添加 System.Web.UI.WebControls.RadioButtonList
;我在下面留下一个基本示例,您可以将其用作起点:
在您的 aspx 文件中(在您的表单元素内的某处):
<asp:Table runat="server" ID="myTable"></asp:Table>
在您的 code-behind 文件中:
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);
}
我建议你使用AspGridView
,而不是普通的HTML table。
您可以使用 <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;
}
}
我在网络表单 asp.net 应用程序中动态创建标签和单选按钮列表。之后,我将它们放在 table 中。 我在 table 中显示实际的单选按钮列表控件时遇到问题。
如何显示控件而不是显示文本? 我的代码是:
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;
RadioButtonList
是一个 WebControl
,可以在另一个 WebControl
中使用。在您的代码中,您通过连接 HTML 文本创建了一个 table,因此,在 Web 窗体视图状态模型的域之外。
当您在 HTML 文本变量中连接 c
时,您只是得到 c.ToString()
返回的值,默认情况下它是类型的全名。
话虽如此,请改用 System.Web.UI.WebControls.Table
类型来构建您的 table,并向其添加 System.Web.UI.WebControls.RadioButtonList
;我在下面留下一个基本示例,您可以将其用作起点:
在您的 aspx 文件中(在您的表单元素内的某处):
<asp:Table runat="server" ID="myTable"></asp:Table>
在您的 code-behind 文件中:
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);
}
我建议你使用AspGridView
,而不是普通的HTML table。
您可以使用 <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;
}
}