数据列表中的启用按钮

Enable button in datalist

请保持温和 - 我是 C# 的新手!已经有一些关于此的主题,但它们大多似乎都有过于复杂的代码,我无法理解模糊的解释。

我有一个数据列表,由 SQL 数据库提供,它将充当评论部分。我想启用一个按钮来编辑评论,如果用户创建了评论或者是管理员但似乎无法弄清楚如何:

A) 以数据列表中的按钮控件为目标并且

B) 仅在评论时启用它,用户应有权这样做

<asp:DataList ID="DataList2" runat="server">
        <ItemTemplate>
            <br />
            Comment:
            <asp:Label Text='<%# Eval("comment") %>' runat="server" ID="commentLabel" /><br />
            Posted on:
            <asp:Label Text='<%# Eval("postedDate") %>' runat="server" ID="postedDateLabel" /><br />
            Posted by:
            <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# "user.aspx?user=" + Eval ("userName") %>'><%# Eval("userName") %></asp:HyperLink><br />
    <!-- Comment edit begins ------------------------------------------------------------------------>
            <asp:Button ID="commentEditButton" runat="server" Text="Edit" visible="false"/>

    <!-- Comment edit ends -------------------------------------------------------------------------->
            </ItemTemplate>
        </asp:DataList> 

C#:

             conn1.Open();

        String qry1 = "SELECT comments.comment, comments.postedDate, users.userName FROM comments INNER JOIN users ON comments.userId=users.Id WHERE comments.imgId=@Id ORDER BY comments.postedDate DESC";

        SqlCommand cmd1 = new SqlCommand(qry1, conn1);

        SqlDataAdapter da1 = new SqlDataAdapter(cmd1);

        cmd1.Parameters.AddWithValue("@Id", Request.QueryString["imgid"]);

        DataSet ds1 = new DataSet();

        //Derp?
        foreach(DataRow row in ds1.Tables)
        {
            Int32 userIdData = Int32.Parse(row["comments.userId"].ToString());
            if (userIdData.Equals(Int32.Parse(Session["userId"].ToString()))) {
                //DataList2. commentEditButton.Visible = true;
            }   
        }

        da1.Fill(ds1);

        DataList2.DataSource = ds1;
        DataList2.DataBind();

        conn1.Close();

提前致谢!

处理数据绑定上的项目数据绑定事件,并设置启用按钮的条件

   protected void DataList2_ItemDataBound1(object sender, DataListItemEventArgs e)
    {
        if (userIdData.Equals(Int32.Parse(Session["userId"].ToString())))
        {
            Button cmdButton = e.Item.FindControl("commentEditButton") as Button;
            if (cmdButton != null) cmdButton.Visible = true;
        }
    }

最后我自己解决了这个问题。我错过了给 DataList 一个 DataKeyField 并添加了一个 OnItemCommand 和一个 CommandNameEdit 超链接。

<asp:DataList ID="DataList2" runat="server" DataKeyField="Id" OnItemCommand="DataList2_ItemCommand">
    <ItemTemplate>
        <br />
        Comment:
        <asp:Label Text='<%# Eval("comment") %>' runat="server" ID="commentLabel" /><br />
        Posted on:
        <asp:Label Text='<%# Eval("postedDate") %>' runat="server" ID="postedDateLabel" /><br />
        Posted by:
        <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# "user.aspx?user=" + Eval ("userName") %>'><%# Eval("userName") %></asp:HyperLink><br />
<!-- Comment edit begins ------------------------------------------------------------------------>
        <asp:LinkButton ID="commentEditButton" runat="server" CommandName="editComment" Visible="false">Edit</asp:LinkButton>
<!-- Comment edit ends -------------------------------------------------------------------------->
        </ItemTemplate>
    </asp:DataList> 

设置命令:

 if (e.CommandName == "editComment")
 {
     //Set condition for button visibility etc here
     String editId = userSearchDataList.DataKeys[e.Item.ItemIndex].ToString();
     //do stuff with editId
 }

希望这对某人有所帮助!