【问题标题】:How to avoid page refresh in asp.net while click on link button the entire page is refreshing单击链接按钮整个页面正在刷新时如何避免在asp.net中刷新页面
【发布时间】:2023-03-13 00:29:01
【问题描述】:

实际上,如果数据超过 40 个字符,我使用了 read more 并隐藏两个按钮,它工作正常,但是当单击按钮时它正在刷新页面如何禁用刷新。在 Asp.net 中

.aspx 文件中的代码

 <asp:TemplateField  HeaderText="UserdetailsDescription" ItemStyle-Width="50">
                                <ItemTemplate>
                                        <asp:Label ID="lblDescription" runat="server"
                                              Text='<%# Limit(Eval("UserdetailsDescription"),40) %>' 
                                              Tooltip='<%# Eval("UserdetailsDescription") %>'>
                                        </asp:Label>
                                 <asp:LinkButton ID="ReadMoreLinkButton" runat="server"
                                               Text="Read More"
                                               autopostback="false"
                                               Visible='<%# SetVisibility(Eval("UserdetailsDescription"), 40) %>'
                                               OnClick="ReadMoreLinkButton_Click">
                                 </asp:LinkButton>
                           </ItemTemplate>
                          </asp:TemplateField>

.CS 文件后面的##code

 ##  protected bool SetVisibility(object desc, int maxLength)
    {
        var description = (string)desc;
        if (string.IsNullOrEmpty(description)) { return false; }
        return description.Length > maxLength;
    }

    protected void ReadMoreLinkButton_Click(object sender, EventArgs e)
    {
        LinkButton button = (LinkButton)sender;
        GridViewRow row = button.NamingContainer as GridViewRow;
        Label descLabel = row.FindControl("lblDescription") as Label;
        button.Text = (button.Text == "Read More") ? "Hide" : "Read More";
        string temp = descLabel.Text;
        descLabel.Text = descLabel.ToolTip;
        descLabel.ToolTip = temp;
    }

    protected string Limit(object desc, int maxLength)
    {
        var description = (string)desc;
        if (string.IsNullOrEmpty(description)) { return description; }
        return description.Length <= maxLength ?
            description : description.Substring(0, maxLength) + ".....";
    }

【问题讨论】:

  • 一个 LinkBut​​ton 完全按照它所说的那样做,它是一个按钮,当点击时,它使浏览器导航到一个新链接(刷新)。如果您不希望这种情况发生,则需要使用常规按钮并使用 JavaScript 实现它。

标签: c# asp.net asp.net-mvc asp.net-core


【解决方案1】:

我认为UpdatePnaelPostBackTrigger 可以帮助您在不完全回发的情况下部分更新页面。在链接按钮周围添加更新面板并添加 PostBackTrigger 可以帮助您解决问题。更多详情see this answer

【讨论】:

    【解决方案2】:

    据我所知,链接按钮上没有 autopostback 属性。

    尝试使用OnClientClick,并确保从您在那里调用的函数中返回 false。

    <asp:LinkButton ID="ReadMoreLinkButton" runat="server"
                   Text="Read More"
                   Visible='<%# SetVisibility(Eval("UserdetailsDescription"), 40) %>'
                   OnClientClick="HideReadMoreLinkButton(); return false"/>
    <script>
       function HideReadMoreLinkButton() {
           //your code to hide button here
       }
    </script>
    

    注意:如果您通过另一个按钮获得页面回发,则该按钮将返回可见状态,因为这是它保持在视图状态中的状态。所以一个选项是有一个隐藏字段维护它的客户端状态和页面上的一些 JS 来将链接恢复到隐藏状态。

    另请参阅:Disable the postback on an <ASP:LinkButton>

    【讨论】:

      猜你喜欢
      • 2017-05-17
      • 2021-06-30
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 2021-09-12
      • 2014-04-10
      相关资源
      最近更新 更多