C# 通过ASP.NET HTML代码访问codebehind字段值

C# 通过ASP.NET HTML代码访问codebehind字段值,c#,asp.net,C#,Asp.net,简单问题 在代码隐藏(.cs)中,我们有 在ASP(.aspx)中,我们有: 因此,如何做到这一点,从cs->saved error list将值分配到html?只需将其添加到.cs中即可。。如果我理解正确的话 string error="11000114S"; textValidator.ErrorMessage = error ; // or what ever you want 这应该可以 protected string error="11000114S"; <asp:Te

简单问题

在代码隐藏(.cs)中,我们有

在ASP(.aspx)中,我们有:



因此,如何做到这一点,从cs->saved error list将值分配到html?

只需将其添加到.cs中即可。。如果我理解正确的话

string error="11000114S";
textValidator.ErrorMessage = error ; // or what ever you want
这应该可以

protected string error="11000114S";

<asp:TextBox ID="text" runat="server" EnableViewState="false" AutoPostBack="false"/>
<asp:RequiredFieldValidator id="textValidator" runat="server" controlToValidate="text"
errormessage='<%# error %>'> 
protectedstring error=“11000114S”;

您可以将错误消息动态分配给代码隐藏中的验证器。即:

this.textValidator.ErrorMesssage = error;

我不建议从视图中引用代码隐藏,尽量使视图与代码隐藏分离,只需在代码隐藏本身中初始化错误消息。
protected string error="11000114S";

<asp:TextBox ID="text" runat="server" EnableViewState="false" AutoPostBack="false"/>
<asp:RequiredFieldValidator id="textValidator" runat="server" controlToValidate="text"
errormessage='<%# error %>'> 
this.textValidator.ErrorMesssage = error;