如何实现部分GridView更新? [英] How to achieve Partial GridView Update?

查看:72
本文介绍了如何实现部分GridView更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网格中有一个图像按钮,当我点击它时,我希望它旁边的标签得到更新。问题是现在,当我这样做整个页面重新加载,这是令人讨厌的。我想让它像facebook一样,当你喜欢评论时,整个页面都不会重新加载只有一小部分重新加载。



I have a imagebutton in a grid and when i click on it i want the label next to it to get updated. The problem is right now when i do this entire page gets reloaded which is annoying. I want to make it like facebook where when you like a comment the entire page does not reload only a small fraction is reloaded.

<asp:ImageButton ID="lnklike" runat="server" ImageUrl="~/Images/thumbsup.png" height="20px" Width="20px" CommandName="like" CommandArgument='<%# Eval("ScrapId")%>'/> &nbsp;
<asp:UpdatePanel ID="UpdatePanel1" runat="Server">
    <Triggers>
        <asp:AsyncPostBackTrigger controlid="lnklike" eventname="click"  />
    </Triggers>
    <ContentTemplate>  <asp:Label ID="Label1" runat="server" Text='<%# Controls_GetUserScraps.abc((int)Eval("ScrapId")) %>' />







protected void GridViewRowCommand(Object sender, GridViewCommandEventArgs e)
    {
        var scrapId = Int32.Parse(e.CommandArgument.ToString());
        GridViewRow gvr = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
        int index = gvr.RowIndex;

        switch (e.CommandName)
        {
            case "like":
                //GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                //int index = gvr.RowIndex;

                string chklike = "select likestatus from tbl_like where fromid='" + Session["UserId"] + "' and scrapid='" + scrapId + "'";
                int a = dbo.GetLikesMethod(chklike);
                string chkthumbsdown = "select thumbsdownstatus from tbl_like where fromid='" + Session["UserId"] + "' and scrapid='" + scrapId + "'";
                int b = dbo.GetLikesMethod(chkthumbsdown);

                if (a == 0 && b == 0)
                {
                    string sendlike = "insert into tbl_like (ScrapId,FromId,LikeStatus) values('" + scrapId + "','" + Session["UserId"] + "',1)";
                    dbo.insert(sendlike);
                   int likes= abc(scrapId);
                    //GetUserScraps(int.Parse(Request.QueryString["Id"].ToString()));
                    Label lnklike = (Label)GridViewUserScraps.Rows[index].FindControl("Label1");
                        lnklike.Text= likes.ToString();
                }
                else if (a != 0)
                {

                    Response.Write("already liked");
                }
                else if (b != 0)
                {
                    Response.Write("you can not like something you already downvoted!");
                }

                break;


public void GetUserScraps(int Id)
   {

       string getUserScraps = "SELECT u.Id as UserId,u.firstname,u.ImageName,s.FromId,s.ToId,s.Message,s.SendDate,s.ID as ScrapId FROM [tbl_user] as u, Scrap as s WHERE u.Id=s.FromId AND s.ToId='" + Request.QueryString["Id"].ToString() + "'";
       //string getlikes = "select COUNT(*) from tbl_like inner join Scrap on tbl_like.scrapid=Scrap.Id where tbl_like.likestatus=1 and tbl_like.scrapid='"+<%#DataBinder.Eval(Container.DataItem,"ScrapId")%>+"'";
       //  <%#DataBinder.Eval(Container.DataItem,"ScrapId")%>


       dt = dbClass.ConnectDataBaseReturnDT(getUserScraps);
       if (dt.Rows.Count > 0)
       {
           GridViewUserScraps.DataSource = dt;
           GridViewUserScraps.DataBind();

       }

推荐答案

将标签放在UpdatePanel中,并在需要时更新面板。

试试这个:

HTML:

Put the label in UpdatePanel and update the panel whenever you need.
Try this:
HTML:
<asp:updatepanel id="UpdatePanel1" runat="Server" xmlns:asp="#unknown">
    <contenttemplate>  
          <asp:label id="Label1" runat="server" text="<%# Controls_GetUserScraps.abc((int)Eval("ScrapId")) %>" />
    </contenttemplate>
</asp:updatepanel>



CS:


CS:

Label lnklike = (Label)GridViewUserScraps.Rows[index].FindControl("Label1");
lnklike.Text= likes.ToString();
UpdatePanel UpdatePanel1= (UpdatePanel)GridViewUserScraps.Rows[index].FindControl("UpdatePanel1");
UpdatePanel1.Update();







--Amit




--Amit


这篇关于如何实现部分GridView更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆