【发布时间】: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 我已将按钮标记添加到问题的底部。