我正在制作一个非常简单的标签云"项目数据库的功能。我希望用户能够检查各种标签,然后在选中的框上显示自动回送,显示包含这些标签的相关项目。
它可以工作,但复选后的复选框正在重置。我希望复选框保持选中状态(或取消选中,视具体情况而定),以便用户可以选择多个标签。
我已经搜索并看到有人说将数据绑定事件放在if(!isPostBack)中。我已经尝试过了,但这似乎并不重要。
aspx代码:
<asp:DataList ID="TagDataList" runat="server" OnDataBinding="databindthesucker" DataKeyField="TagName">
<ItemTemplate>
<asp:CheckBox AutoPostBack="true" ID="TagCheckbox" runat="server" Text='<%#Eval ("dZTagName") %>' />
<asp:HiddenField ID="TagNameHidden" runat="server" Value='<%#Eval ("dZTagName") %>'/><br />
</ItemTemplate>
</asp:DataList>
<asp:DataList ID="ProjectDataList" runat="server" DataKeyField="projectID">
<ItemTemplate>
<asp:HyperLink ID="ProjectLink" runat="server" NavigateUrl='<%# "/Project/Default.aspx?PID=" + Eval ("projectID") %>' Text='<%# Eval ("projectID") + ": " + Eval ("projectName") %>'> </asp:HyperLink><br />
</ItemTemplate>
</asp:DataList>
然后是cs ......
protected void Page_Load(object sender, EventArgs e)
{
displaytags(sender, e);
}
protected void displaytags(object sender, EventArgs e)
{
var qrygettags = (from t in db.TagTables
select new { t.TagName }).Distinct().ToList();
TagDataList.DataSource = qrygettags;
TagDataList.DataBind();
CheckBox TagCheckbox = (CheckBox)TagDataList.Items[0].FindControl("TagCheckbox");
}
protected void databindthesucker(object sender, EventArgs e)
{
foreach (DataListItem item in TagDataList.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
CheckBox TagCheckbox = (CheckBox)item.FindControl("TagCheckbox");
HiddenField TagNameHidden = (HiddenField)item.FindControl("TagNameHidden");
string Tagstring = TagNameHidden.Value.ToString();
if (TagCheckbox.Checked)
{
var qrygetprojects = (from p in db.projects
join pt in db.TagTables on p.projectID equals pt.projectID
where pt.TagName == Tagstring
select new
{
pt.projectID,
p.projectName
}).ToList();
ProjectDataList.DataSource = qrygetprojects;
ProjectDataList.DataBind();
}
}
}
}
有什么想法吗?