简体   繁体   English

直到第二次单击,ASP.NET更新面板才会更新

[英]ASP.NET update panel doesn't update until second click

I've looked for answers online but there are so many that I've tried that haven't worked. 我一直在网上寻找答案,但是我尝试过的很多方法都行不通。 I have two buttons on the page a "like" and a "dislike" button. 我在页面上有两个按钮,分别是“喜欢”和“不喜欢”按钮。

When I click on a button the updatepanel doesn't update but the like data for that user is inserted into the database. 当我单击按钮时,updatepanel不会更新,但是该用户的类似数据会插入数据库中。

When I click on the button the second time the update panel gets refreshed and works for proceeding clicks. 当我第二次单击按钮时,更新面板将刷新并可以继续单击。 I want the update panel to refresh on the first click 我希望更新面板在第一次单击时刷新

<form id="Matches" runat="server" class="form-horizontal">

        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

        <asp:UpdatePanel ID="VotePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false" >
            <ContentTemplate>

                <div class="row">
              <!-- left column -->
                    <div class="col-lg-12 col-sm-12 col-xs-12">
                        <div class="text-center">
                       ail" />
                            <h1>
                                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></h1>
                            <div class="btn-group">
                                <asp:Button ID="Button1" runat="server" CssClass="btn btn-default" Text="Like" OnClick="Button1_Click" /><asp:Button ID="Button2" CssClass="btn btn-default" runat="server" Text="Dislike" OnClick="Button2_Click" />
                            </div>
                        </div>
                    </div>
                </div>

            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Button1" />

                <asp:AsyncPostBackTrigger ControlID="Button2" />
                </Triggers>
        </asp:UpdatePanel> 

C# Code C#代码

protected void Button1_Click(object sender, EventArgs e)
        {
            uservote.spAddLike(user.getId(), usermatch.getId());
            VotePanel.Update();

        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            uservote.spAddDislike(user.getId(), usermatch.getId());
            VotePanel.Update();
        }

From comments in addtion to your question, you are calling an update function before you have updated the database. 从问题中的注释中,您更新数据库之前正在调用更新函数。 You need to get a better understanding of the page life-cycle . 您需要更好地了解页面生命周期

You are getting a the updated info on page load, this happens before the on click. 您将获得有关页面加载的更新信息,这是在单击之前发生的。 Try the following 尝试以下

 private void functionToUpdateFields(some arguements)
 {
      /*Call Database and populate form fields*/
 }


 protected void Page_Load(object sender, EventArgs e)
 {
     if (!isPostback)
     {
         functionToUpdateFields(some arguements);
     }
 }

 protected void Button1_Click(object sender, EventArgs e)
 {
        uservote.spAddLike(user.getId(), usermatch.getId());
        functionToUpdateFields(some arguements);
        VotePanel.Update();
 }

 protected void Button2_Click(object sender, EventArgs e)
 {
       uservote.spAddDislike(user.getId(), usermatch.getId());
       functionToUpdateFields(some arguements);
       VotePanel.Update();
 }

When you have all this set up set break poins on each of the event handlers and note the order in which they occur when you debug. 完成所有这些设置后,将在每个事件处理程序上设置中断点,并记下调试时它们发生的顺序。 This will give you a practical demonstration on the page life cycle. 这将为您提供有关页面生命周期的实际演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM