How to Call a Variable in Code Behind to Aspx Page

how to call a variable in code behind to aspx page

The field must be declared public for proper visibility from the ASPX markup. In any case, you could declare a property:


private string clients;
public string Clients { get { return clients; } }

UPDATE: It can also be declared as protected, as stated in the comments below.

Then, to call it on the ASPX side:

<%=Clients%>

Note that this won't work if you place it on a server tag attribute. For example:

<asp:Label runat="server" Text="<%=Clients%>" />

This isn't valid. This is:

<div><%=Clients%></div>

How can I call a csharp variable in .aspx page?

You can create a variable of type string with protected access modifier and call it on your web page using:

<%= login_status %>

Not able to access a variable from code behind in aspx page

All variables in C# are local variables, and they're only visible inside the function they were declared in. Make your thing a field or a property instead, and make sure it's either protected or public.

asp.net unable to reference variable from code behind

At the <@ Page ...> tag at the top of your ASPX, change the value of the attribute Inherits="TestApp.About" to Inherits="TestApp_About". Underscore instead of point/period. I was able to replicate the problem.

Can't access variable from code behind

Sounds like your page isn't properly hooked up to the class. Make sure you've got Inherits="go._default" in your page header in your aspx page.

EDIT After your edit, this is definitely your problem.

Also, you can definitely use fields if you want instead of properties (but I always recommend properties), and you can use protected instead of public. Things shouldn't be public unless you really do want to access them from outside of this class or inheritance chain.

how to access List variables in code behind to aspx page

You can use:

<asp:Repeater ID="Rep" runat="server">
<ItemTemplate>
<a href="<%# Eval("url") %>"><%# Eval("title") %></a>
<p><%# Eval("Url")%> </p>
</ItemTemplate>
</asp:Repeater>

Or, you can hook into the Rep.ItemDataBound event, find your controls, and populate them in the code-behind.

Get variable value from code behind and use in aspx page control

Solved this, solution below

Since I used a web user control in this case the usual scenarios would not work. But by putting a databind in the page that controls the user control, or any materpage in the chain above the web user control the code started to work

MasterPage codebehind

public partial class MasterPages_MyTopMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
// Databind this to ensure user controls will behave
this.DataBind();
}
}

Ascx file, all suggested solutions below works

<%@ Control Language="C#" AutoEventWireup="False" CodeFile="Header.ascx.cs" Inherits="Site.UserControls.Base.Header" %>
1: <asp:Literal runat="server" Text='<%# DataBinder.GetPropertyValue(this, "Testing") %>' />
2: <asp:Literal runat="server" Text='<%# DataBinder.Eval(this, "Testing") %>' />
3: <asp:Literal runat="server" Text='<%# Testing2 %>' />

Codebehind of ascx

namespace Site.UserControls.Base
{
public partial class Header : UserControlBase //UserControl
{
public string Testing { get { return "hello world!"; } }
public string Testing2 = "hello world!";

protected void Page_Load(object sender, EventArgs e)
{ }
}
}

Thanks for the inspiration!



Related Topics



Leave a reply



Submit