如何在GridView中更新单个控件时防止整个页面重新加载? [英] How to prevent the entire page from reloading on updating a single control within GridView?

查看:169
本文介绍了如何在GridView中更新单个控件时防止整个页面重新加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView,其中有一个图像按钮和旁边的标签。当我单击图像按钮时,标签的值变为20(示例)。但问题是,当我这样做时,页面移动到顶部,或者可能是完整的GridView重新加载或整个页面重新加载我不确定。我只想更新下一个图像按钮的那个标签。

我尝试使用UpdatePanel,但仍然没有运气。





I have a GridView within which i have a image button and a label next to it. When i click on the imagebutton the value of the label gets changed to 20 (example). But problem is that when i do this the page moves to the top, or may be the complete GridView reloads or entire page reloads i am not sure. I just want to update that one label which is next the image button.
I tried using UpdatePanel and all but still no luck.


<asp:ImageButton ID="lnklike" runat="server" ImageUrl="~/Images/thumbsup.png" height="20px" Width="20px" CommandName="like" CommandArgument='<%# Eval("ScrapId")%>'/>

                <asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger controlid="lnklike" eventname="Click" />
        </Triggers>
            <ContentTemplate>
                &nbsp;<asp:Label ID="Label1" runat="server" Text='<%# Controls_GetUserScraps.abc((int)Eval("ScrapId")) %>' />

     </ContentTemplate>
        </asp:UpdatePanel>










protected void GridViewRowCommand(Object sender, GridViewCommandEventArgs e)
    {

        var scrapId = Int32.Parse(e.CommandArgument.ToString());
        
        
        switch (e.CommandName)
        {
            case "like":
                GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);

    int index = row.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);
                    
                    Label lbl=(Label)GridViewUserScraps.Rows[index].FindControl("Label1");
                    lbl.Text = "20";
                    //GetUserScraps(int.Parse(Request.QueryString["Id"].ToString()));
                }

推荐答案

您需要将 MaintainScrollPositionOnPostback 设置为 true @page指令中的属性。

例如:

You need to set MaintainScrollPositionOnPostback to true Property in @page directive.
E.g.:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MaintainScrollPositionOnPostback="true" %>







--Amit




--Amit


我也受苦从同样的问题然后我使用更新面板像。

< asp:updatepanel id =updpnlRefreshrunat =serverupdatemode =条件xmlns:asp =#unknown>



受保护的子UpdatePanels()

updPnlSalesLead.Update()

updpnlRefresh.Update()

结束Sub



我从按钮点击事件和网格更新和编辑事件的代码后面更新此更新面板。我的页面没有回发,即使我点击任何按钮,我的滚动也会保持在同一位置。
i also suffer from same problem then i used update panel like .
<asp:updatepanel id="updpnlRefresh" runat="server" updatemode="Conditional" xmlns:asp="#unknown">

Protected Sub UpdatePanels()
updPnlSalesLead.Update()
updpnlRefresh.Update()
End Sub

and i update this update panel from code behind on button click events and grid updation and editing events. my page does not post back and my scroll stick on same position even i click on any button.


这篇关于如何在GridView中更新单个控件时防止整个页面重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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