在datalist中启用按钮

时间:2015-04-30 12:50:00

标签: c# sql asp.net datalist datarow

请保持温和 - 我对C#比较新!已经有一些线程,但它们似乎都有过于复杂的代码,我无法用模糊的解释来理解。

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

A)定位datalist中的按钮控件

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

提前致谢!

1 个答案:

答案 0 :(得分:0)

我最终解决了这个问题。我错过了DataList DataKeyField,并在修改超链接中添加了OnItemCommandCommandName

<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
 }

希望这有助于某人!