Sometimes, we will have requirements to access codebehind variable in ASPX page. It can be done by the following syntax.

The codebehind variable should be public variable to access in ASPX page.
ASPX

CodeBehind

public partial class _Default : System.Web.UI.Page
{
public string Name = "ASP.Net";

Calling CodeBehind Method in ASPX

CodeBehind

public DataTable GetAllRoles()
{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString);
con.Open();
SqlCommand com = new SqlCommand("SP_GetAllRoles", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ada = new SqlDataAdapter(com);
DataTable dt = new DataTable();
ada.Fill(dt);
return dt;
}

ASPX

<asp:DropDownList ID="ddlRoles" DataSource=''
DataTextField="Role" DataValueField="RoleID"
runat="server">

<a href=''>

Advertisements