
How to call a variable or method in code behind in aspx page in asp.net
Asp.net Although the foreground code is separated from the code behind, sometimes it is necessary to call the methods or variables of the code behind(cs file) in the foreground(aspx file). For example, if you want to pass the value of a code behind variable to a variable in the javascript code of foreground page, i.e. call the variable in code behind in the aspx; if binding data displays different values according to the true or false in the aspx, you need to write a method in the code behind to check, returning different values according to the true or false of value is the case which aspx calls the cs method.
Whether it is aspx calling the method or variable in code behind, there is little difference from binding data in the aspx page, so write the code in this symbol <%# %>. Let's first look at how to call a variable in code behind in aspx page in asp.net, and then discuss how to call the method in code behind.
1. How to call a variable in code behind in aspx page
If you want to pass the value of a variable in the code behind to a variable in javascript code in the aspx, the implementation code is as follows:
cs code:
namespace testpro
{
public partial class TestEg : System.Web.UI.Page
{
protected string img = "";//Variable passed to the aspx
protected void Page_Load(object sender, EventArgs e)
{
img = "/images/ads.jpg";
}
}
}
aspx code:
<script type="text/javascript">
var img = "<% =img %>";
</script>
As can be seen from the above code, the variable that needs to be called in aspx must be defined as a protected member or public member, otherwise it cannot get the value of the code behind variable in the aspx. In addition, double quotation marks must be added to the javascript character variable, otherwise it cannot be assigned.
2. How to call a method in code behind in aspx page
If binding data in the aspx, there is a value that needs to display different prompt texts based on true or false. Writing the code in the aspx is not much better than writing it in the code behind. Put the code in the code behind cs file. The implementation code is as follows:
cs method:
/// <summary>
/// call a method in code behind in aspx page
/// </summary>
/// <param name="isTrue">Object variable from the aspx</param>
/// <returns>True: male; false: female</returns>
protected string GetValue(object isTrue)
{
bool flag = Convert.ToBoolean(isTrue);
if (flag)
return "male";
else
return "female";
}
Call a method in code behind in aspx:
<div><%# GetValue(Eval("sex")) %></div>
The method in the code behind should also be defined as protected or public, otherwise the aspx will not be called; in addition, the bound value Eval("sex") is an object type, which needs to be converted to the corresponding type in the code behind method.
-
Related Reading
- C# listview select row(a row and multiple rows)
- Solve the flickering problem of C# listview multithr
- C# convert punctuation marks to Unicode encoding
- C# Read and write to text file Newline, with one lin
- C# merge binary array(byte array)
- C# Winform button transparent background with proper
- Convert Unicode to string C#(with string to unicode)
- C# set Winform icon and version number
- C# float, double range, precision, definition and ty
- C# split string with \n pay attention to the problem
- C# Static constructor does not allow access modifier
- C# efficiency comparison of multiple strings concate