将标签转换为复选框列表
Converting label to a checkboxlist
注意:我是 asp 和 sql 的新手。正在尝试修复一些不属于我的旧代码。
我有一个页面,其中包含一个 DropDownList,其中包含一些来自数据库的用户名。单击其中一个用户名后,会根据用户从数据库中提取日期的 DropDownList 上方显示一个列表。现在,"list" 的日期是一个标签,该标签的 .text 正在更改。相反,我想将这些日期中的每一个都与一个复选框相关联,然后我可以根据它是否被选中来操作它。
.aspx 页:
<anthem:Label ID="OpenTime" runat="server" />
.cs 页面:
SqlCommand command = new SqlCommand();
command.Connection = gConn;
if (Roles.IsUserInRole("Approver") == true)
{
DropDownList ddlActingAs = (DropDownList)LoginView1.FindControl("ddlActingAs");
OpenTime.Text = "";
String sql = "SELECT StartDate FROM Periods WHERE User_ID = @userid AND (PeriodStatus_ID = 1 OR PeriodStatus_ID = 2) ORDER BY StartDate DESC";
command.CommandText = sql;
command.Parameters.Add(new SqlParameter("userid", ddlActingAs.SelectedValue.ToString()));
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
OpenTime.Text += "<br>" + reader.GetDateTime(0).ToString("MM/dd/yyyy") + " is open";
OpenTime.UpdateAfterCallBack = true;
reader.Close();
}
示例:
首先,我用复选框列表替换了标签,如下所示:
<anthem:CheckBoxList ID="CheckTest" runat="server" />
然后我基本上尝试用 "CheckTest" 替换所有 "OpenTime"。我弄乱了复选框列表的 .DataSource 和 .Databind 方法,但没有成功。
不确定解决此问题的正确方法是什么。
嗯,首先您需要将日期存储到一个列表中,以便稍后能够将其绑定到 Grid 或 Repeater .由于您需要 CheckBoxList,您可以使用 TemplateField。
将数据库中的日期添加到列表中。
IList<DateTime> dateList = new List<DateTime>();
while (reader.Read())
{
//Check if your reader is not empty and has a valid date before adding.
dateList.add(reader.GetDateTime(0).ToString("MM/dd/yyyy"))
}
编辑
假设你有这个class
public class MyCustomDate
{
public DateTime Date { get; set; }
public bool IsChecked { get; set; }
public string Description { get; set; }
}
在您的页面加载中,代码将类似于此
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IList<MyCustomDate> dates = new List<MyCustomDate>();
dates.Add(new MyCustomDate() { Date = DateTime.Now.AddYears(1), IsChecked = true, Description = "First Date" });
dates.Add(new MyCustomDate() { Date = DateTime.Now.AddYears(2), IsChecked = false, Description = "Second Date" });
dates.Add(new MyCustomDate() { Date = DateTime.Now.AddYears(3), IsChecked = true, Description = "Third Date" });
this.rptTest.DataSource = dates;
this.rptTest.DataBind();
}
}
你的HTML会是
<asp:Repeater runat="server" ID="rptTest" >
<HeaderTemplate>
<table style="width: 100%">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<span><%# Eval("Description") %></span>
</td>
<td>
<asp:CheckBox ID="chkTest" runat="server" Checked='<%#Convert.ToBoolean(Eval("IsChecked"))%>' />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
完成后,您可以将列表绑定到带有模板字段的转发器或网格中。 Repeater Example Link
注意:我是 asp 和 sql 的新手。正在尝试修复一些不属于我的旧代码。
我有一个页面,其中包含一个 DropDownList,其中包含一些来自数据库的用户名。单击其中一个用户名后,会根据用户从数据库中提取日期的 DropDownList 上方显示一个列表。现在,"list" 的日期是一个标签,该标签的 .text 正在更改。相反,我想将这些日期中的每一个都与一个复选框相关联,然后我可以根据它是否被选中来操作它。
.aspx 页:
<anthem:Label ID="OpenTime" runat="server" />
.cs 页面:
SqlCommand command = new SqlCommand();
command.Connection = gConn;
if (Roles.IsUserInRole("Approver") == true)
{
DropDownList ddlActingAs = (DropDownList)LoginView1.FindControl("ddlActingAs");
OpenTime.Text = "";
String sql = "SELECT StartDate FROM Periods WHERE User_ID = @userid AND (PeriodStatus_ID = 1 OR PeriodStatus_ID = 2) ORDER BY StartDate DESC";
command.CommandText = sql;
command.Parameters.Add(new SqlParameter("userid", ddlActingAs.SelectedValue.ToString()));
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
OpenTime.Text += "<br>" + reader.GetDateTime(0).ToString("MM/dd/yyyy") + " is open";
OpenTime.UpdateAfterCallBack = true;
reader.Close();
}
示例:
首先,我用复选框列表替换了标签,如下所示:
<anthem:CheckBoxList ID="CheckTest" runat="server" />
然后我基本上尝试用 "CheckTest" 替换所有 "OpenTime"。我弄乱了复选框列表的 .DataSource 和 .Databind 方法,但没有成功。
不确定解决此问题的正确方法是什么。
嗯,首先您需要将日期存储到一个列表中,以便稍后能够将其绑定到 Grid 或 Repeater .由于您需要 CheckBoxList,您可以使用 TemplateField。
将数据库中的日期添加到列表中。
IList<DateTime> dateList = new List<DateTime>();
while (reader.Read())
{
//Check if your reader is not empty and has a valid date before adding.
dateList.add(reader.GetDateTime(0).ToString("MM/dd/yyyy"))
}
编辑
假设你有这个class
public class MyCustomDate
{
public DateTime Date { get; set; }
public bool IsChecked { get; set; }
public string Description { get; set; }
}
在您的页面加载中,代码将类似于此
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IList<MyCustomDate> dates = new List<MyCustomDate>();
dates.Add(new MyCustomDate() { Date = DateTime.Now.AddYears(1), IsChecked = true, Description = "First Date" });
dates.Add(new MyCustomDate() { Date = DateTime.Now.AddYears(2), IsChecked = false, Description = "Second Date" });
dates.Add(new MyCustomDate() { Date = DateTime.Now.AddYears(3), IsChecked = true, Description = "Third Date" });
this.rptTest.DataSource = dates;
this.rptTest.DataBind();
}
}
你的HTML会是
<asp:Repeater runat="server" ID="rptTest" >
<HeaderTemplate>
<table style="width: 100%">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<span><%# Eval("Description") %></span>
</td>
<td>
<asp:CheckBox ID="chkTest" runat="server" Checked='<%#Convert.ToBoolean(Eval("IsChecked"))%>' />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
完成后,您可以将列表绑定到带有模板字段的转发器或网格中。 Repeater Example Link