How to use a variable defined in code behind file in ASP.NET on inline code for the same page
I have a page with a Textbox开发者_如何学JAVA
, a Button
and a Label
control. On Button_Click event, I store the value of Textbox in a variable
called S
using code behind.
How do I show the value of S
in the Label control using inline
code, but not using the code behind
?
This is not the ASP.NET approach, but you can add S
as a member of the code-behind class (at least protected):
protected string S { get; private set; }
And retrieve it inline in the page markup with
<%= S %>
I am not exactly sure about the requirement, but following with some modifications it will work. Embed this block in your aspx page:
If using C#
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
mylabel.Text = s; // Replace with your label control
}
</script>
If using VB.NET:
<script runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
mylabel.Text = s
End Sub
</script>
精彩评论