Bind ASP.NET to a property of a class
-
Hi folks! Maybe anyone can help me with this. I'm from WPF world and am used to bind to objects in other classes. I found many examples to bind to lists, but none how to bind to a property of another class. So here's the deal: Default.aspx.cs
private DefaultVM vm;
protected void Page_Load(object sender, EventArgs e)
{
this.vm = new DefaultVm();
}DefaultVM.cs
public int TestValue { get;set;}
public void DefaultVM()
{
TestValue = 10;
}Default.aspx
<asp:Label runat="server" Text='<%# DataBinder.Eval(vm,"TestValue")%>'/>
None of this works. Tried a few other things as well, but without result. What am I doing wrong?
-
Hi folks! Maybe anyone can help me with this. I'm from WPF world and am used to bind to objects in other classes. I found many examples to bind to lists, but none how to bind to a property of another class. So here's the deal: Default.aspx.cs
private DefaultVM vm;
protected void Page_Load(object sender, EventArgs e)
{
this.vm = new DefaultVm();
}DefaultVM.cs
public int TestValue { get;set;}
public void DefaultVM()
{
TestValue = 10;
}Default.aspx
<asp:Label runat="server" Text='<%# DataBinder.Eval(vm,"TestValue")%>'/>
None of this works. Tried a few other things as well, but without result. What am I doing wrong?
Try this:
then, in Default.aspx.cs (perhaps in OnLoad or wherever you deem appropriate)
vm = new DefaultVM();
lblTest.Text = vm.TestValue.ToString();Mod: Sorry, yes, just thought: that probably wasn't the answer you were looking for. Make
private DefaultVM vm;
topublic DefaultVM vm;
that should do it.Tychotics: take us back to the moon "Life, for ever dying to be born afresh, for ever young and eager, will presently stand upon this earth as upon a footstool, and stretch out its realm amidst the stars." H. G. Wells
modified on Friday, February 26, 2010 6:05 AM
-
Try this:
then, in Default.aspx.cs (perhaps in OnLoad or wherever you deem appropriate)
vm = new DefaultVM();
lblTest.Text = vm.TestValue.ToString();Mod: Sorry, yes, just thought: that probably wasn't the answer you were looking for. Make
private DefaultVM vm;
topublic DefaultVM vm;
that should do it.Tychotics: take us back to the moon "Life, for ever dying to be born afresh, for ever young and eager, will presently stand upon this earth as upon a footstool, and stretch out its realm amidst the stars." H. G. Wells
modified on Friday, February 26, 2010 6:05 AM