Repeater项目隐藏在ItemCommand之后

时间:2015-12-29 06:19:26

标签: c# asp.net entity-framework repeater

嗨,我不知道要说什么,但我的问题是我在转发器中显示帖子,当我按下按钮时,我有一个像按钮,帖子隐藏。 以下是itemCommand事件。

 protected void rptrPosts_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        RepeaterItem item = e.Item;
        TextBox txtComment = item.FindControl("txtComment") as TextBox;
        Label lblID = (Label)item.FindControl("lblID");
        LinkButton btnLike = (LinkButton)item.FindControl("btnLike");
        int userid = Convert.ToInt32(Session["USERID"]);
        int postid = Convert.ToInt32(lblID.Text);
        if (e.CommandName == "like")
        {
            InstagramEntity insta = new InstagramEntity();
            int count = (from like in insta.likes where like.like_from == userid & like.like_post== postid select like).Count();
            if (count == 0)
            {
                like lk = new like();
                lk.like_from = userid;
                lk.like_post = postid;
                insta.likes.Add(lk);
                insta.SaveChanges();
                this.DataBind();
            }
        }

我也试过rptrposts.DataBind()。 PS:点击按钮后,我需要点击面板。 页面加载:

if (!IsPostBack)
        {
            loadpage();
        }

LoadPage()

如果我删除(!IsPostBack)条件,项目会更新,但不会隐藏,但在更新后,会显示重复的项目。

public void loadpage()
    {
        InstagramEntity insta = new InstagramEntity();
        rptrPosts.DataSource = (from post in insta.posts
                                join user in insta.users on post.post_by equals user.user_id
                                orderby post.post_time descending
                                select new { user.user_name, post.post_time, post.post_image, post.post_caption, post.post_id, post.post_by }).ToList();
        rptrPosts.DataBind();
    }

标记:

<asp:Repeater ID="rptrPosts" OnItemCreated="rptrPosts_ItemCreated" OnItemCommand="rptrPosts_ItemCommand"  runat="server">
            <ItemTemplate>
                <div>
                    <div class="panel panel-default post">
                        <asp:UpdatePanel ID="UpdatePanel2"  runat="server">
                        <ContentTemplate>
                        <div class="panel-heading">
                            <asp:Label ID="lblID" Visible="false" runat="server" Text='<%# Eval("post_id") %>'></asp:Label>
                            <img src="Images/homepageimage.jpg" class="img-circle avator" /><a style="margin-left:10px;" href="account.aspx?id=2" ><%# Eval("user_name") %></a><label class="pull-right">Posted On <%# Eval("post_time") %></label>
                            <asp:HiddenField ID="imagePath" Value='<%# Eval("post_image") %>' runat="server" />
                        </div>
                    <div class="panel-body">
                        <img src='Images/<%# Eval("post_image") %>' class="PostImage img-responsive" />
                        <div class="well well-sm">
                            <asp:LinkButton ID="btnLike" CssClass='btn btn-link' CommandName="like" runat="server"><span style="color:red;font-size:x-large" class='<%# getLikeClass(Eval("post_id")) %>'></span></asp:LinkButton><%# getPostlikes(Eval("post_id")) %><br />
                            <%# Eval("post_caption") %><br />
                                <ul class="list-group">
                                    <li class="list-group-item"><b>Comments:</b></li>
                                    <asp:LinqDataSource ID="dsComments" runat="server" EnableInsert="true" ContextTypeName="instagram.InstagramEntity" EntityTypeName="" TableName="view_comments" Where="comment_post == @comment_post" OrderBy="comment_time">
                                        <WhereParameters>
                                            <asp:ControlParameter ControlID="lblID" PropertyName="Text" Name="comment_post" Type="Int32"></asp:ControlParameter>
                                        </WhereParameters>
                                    </asp:LinqDataSource>
                                    <asp:Repeater ID="rptrComments" runat="server">
                                        <ItemTemplate>
                                            <li class="list-group-item"><a href="#"><%# Eval("user_name") %> </a><%# Eval("comment_text") %> </li>
                                        </ItemTemplate>
                                    </asp:Repeater>
                                </ul>
                        </div>
                    </div>
                    <div class="panel-footer"><asp:LinkButton ID="btnDelete" CssClass="pull-right" Visible='<%# DelVisiblily(Eval("post_by")) %>' CommandName="delete" ToolTip="Delete this Post" runat="server"><span class="glyphicon glyphicon-remove label-danger"></span>Delete</asp:LinkButton>  </div>
                </div>
                </ContentTemplate> </asp:UpdatePanel>
                    </div>
                </div>
            </ItemTemplate>
        </asp:Repeater>

我是asp.net的新手,这是我的学期项目。帮助会很好。

1 个答案:

答案 0 :(得分:1)

loadpage();

中使用this.DataBind();代替rptrPosts_ItemCommand(..);
if (e.CommandName == "like")
    {
        //code here
        if (count == 0)
        {
         //code here
         loadpage();
        }
     }