0

我正在为项目数据库创建一个非常简单的“标签云”功能。我希望用户能够检查各种标签,然后在复选框上自动回发,显示具有这些标签的关联项目。

它有效,但回发后复选框正在重置。我希望复选框保持选中(或未选中,视情况而定),以便用户可以选择多个标签。

我已经搜索并看到有人说将数据绑定事件放在 if(!isPostBack) 中。我已经尝试过了,但这似乎并不重要。

asp代码:

<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();

                    }

                }
            }

    }

有任何想法吗?

4

0 回答 0