我有一个.net网络应用程序,c#backend,sql server数据层。我有一个asp:checkboxlist,我通过直接数据绑定从存储过程填充:
ASP.net
<asp:CheckBoxList ID="cblMyPlans" runat="server" RepeatColumns="4">
</asp:CheckBoxList>
C#
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConn"].ToString());
SqlCommand cmd = new SqlCommand("SelectMyPlans", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using(SqlDataReader dr = cmd.ExecuteReader()) {
while (dr.Read()) {
cblMyPlans.Items.Add(new ListItem(dr["Name"].ToString(), dr["PlanID"].ToString()));
}
}
conn.Close();
conn.Dispose();
我不确定它是否可能,但基于SqlDataReader
出来的某个标志,我需要动态地向cbl的每个项添加一个小textbox
以满足该标志
如何在asp:TextBox
?
databind
答案 0 :(得分:2)
您可以使用“手动”元素动态添加CheckBoxList。
此代码将创建与CheckBoxList相同的html,但您可以根据需要添加其他元素......
代码背后:
public void FillPH()
{
var MyList = new List<ListItem>
{
new ListItem("One", "1"),
new ListItem("Two", "2"),
new ListItem("Three", "3")
};
Table myTable = new Table();
foreach (var item in MyList)
{
//Create new checkbox
CheckBox CB = new CheckBox();
CB.Text = item.Text;
CB.ID = item.Value;
//Create tablr row and td, then adds them accordignly
TableRow TR = new TableRow();
TableCell TD = new TableCell();
TD.Controls.Add(CB);
TR.Controls.Add(TD);
//IF <YOUR FLAG GOES HERE>-->
if (item.Value == "2")
{
//Create your input element and place it in a new Table cell (TD2)
TextBox TB = new TextBox();
TB.ID = string.Format("tb_{0}", item.Value);
TableCell TD2 = new TableCell();
TD2.Controls.Add(TB);
TR.Controls.Add(TD2);
}
myTable.Controls.Add(TR);
}
fillMe.Controls.Add(myTable);
}
前:
<div>
<asp:PlaceHolder ID="fillMe" runat="server"></asp:PlaceHolder>
</div>