【问题标题】:Asp.net label does not updateAsp.net 标签不更新
【发布时间】:2014-07-30 04:28:47
【问题描述】:

我想在代码隐藏的事件中更新标签的文本。

在母版页标签声明如下:

<asp:Content ID="WorklistBody" ContentPlaceHolderID="BodyHolder" runat="server">


    <asp:Label id ="lblOutput" runat="server" class="textStyle4" ForeColor="Red"></asp:Label>
    <asp:CustomValidator ID="CustomValidator1" ValidationGroup="SM" runat="server" ErrorMessage="CustomValidator" class="textStyle4" OnServerValidate="CustomValidatorServerValidate">
        </asp:CustomValidator>

// tables and other items goes on here......

在我之前的开发者使用类似以下的东西来更新标签;

<script language="javascript" type="text/javascript">
        function ValidateComments() {
            var selOption = $('#<%= ddlActions.ClientID %> option:selected').val();
            if (selOption == 'C' || selOption == 'R' || selOption == 'U' || selOption == 'Q'|| selOption == 'P' || selOption == 'A') {
                var comment = $('#<%= txtComments.ClientID %>').val();
                comment = jQuery.trim(comment);
                if (comment == '') {
                    if (selOption == 'C') {
                        $('#<%= lblOutput.ClientID %>').text('Please enter comments before processing the transaction');
                    }
                    else if (selOption == 'R') {
                        $('#<%= lblOutput.ClientID %>').text('Please enter comments before cancelling the transaction');
                    }
                    else if (selOption == 'U' || selOption == 'Q' || selOption == 'P' || selOption == 'A') {
                        $('#<%= lblOutput.ClientID %>').text('Please enter comments before assigning the transaction');
                    }
                    window.scroll(0, 0); 
                    return false;
                }
            }
            return true;
        }
    </script>

但现在项目已花费,我必须检查很多逻辑。我想简单地从代码隐藏中更新标签。

public void BtnDoneclick(object sender, EventArgs e)
{  
    //logic logic logic
    lblOutput.Text = @"Please enter comments before you Process this recommendation.";


}

为什么这不更新标签?如何更新标签?

我是 .net 环境的新手

更新:Btn 标记

<td align="right">
                                                             <div class="paddingStyle6 paddingStyle2">                                                                                
                                                            <asp:Button ID="btndone" runat="server" CausesValidation="true" ValidationGroup="SM"
                                                                        Text="Done" UseSubmitBehavior="True" Width="100px" OnClientClick="return ValidateComments()" OnClick="BtnDoneclick" />     

                                                            </div> 
                                                        </td>

【问题讨论】:

  • BtnDoneclick 不遵循事件处理程序命名约定,因此不会自动连接。您是否在 OnInit 中连接了 onlcick 事件?
  • 不,我没有在 OnInit 中连接 onclick 事件。我必须这样做吗?当我放置断点时,似乎所有逻辑都在工作,而当断点到达lblOutput.Text 时,它只是没有更新它。
  • 您能发布 BtnDone 按钮的标记吗?谢谢。
  • 如果有任何更新面板,请告诉我们。
  • @PatHensel 我已将按钮标记添加到问题的底部。

标签: c# asp.net


【解决方案1】:

如果按钮在更新面板中,那么只有更新面板的内容会被更新。正在修改的标签是被忽略的页面的一部分。

尝试将按钮移出更新面板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 2020-06-22
    • 2014-08-01
    • 2021-04-28
    相关资源
    最近更新 更多