Very basic ASP.NET question

  • Thread starter Thread starter Mike Christie
  • Start date Start date
M

Mike Christie

I've got some Java experience but no JSP or ASP and am trying to get to grips
with ASP.NET. I'd appreciate some help on a simple question.

I have been able to get databinding to work, and can display a string obtained
from the database on the screen by binding it to a label. However, if I have
an object instantiated in the page's scope, it seems like there should be an
easier way to output a string that I obtain from that object. I gather that
in JSP there was some syntax like:

<%= myObject.aStringProperty %>

that would substitute the value of this as a literal in the middle of the
html. I tried this syntax in ASP.NET with no luck; is there something
analogous? The reference I'm using (Wrox's "Professional C#") while pretty
good in many ways is a bit light on detailed ASP examples, and I couldn't find
anything about this in the ASP tutorials I looked at.

Thanks for any help.

Mike
 
When you are dealing with data objects, you are not really dealing with a
string property, more an item of a datatype that may contain a string. Try
reading this, it explains a lot about databinding and covers the area you
are asking about with soem useful examples and code.

http://www.aspalliance.com/aspxtreme/webforms/multi-recordsingle-valuedatabi
nding.aspx

--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
 
Mike

In your HTML put

<asp:Label ID="lblID" runat="server"><%# myStrvar %></asp:Label>

In your Codebehind put in C#

private void Page_Load(object sender, System.EventArgs e)

{

myStrvar = "Hello Universe";

Page.DataBind(); // you need this

// Put user code to initialize the page here

}
 
Sorry missed declaring 'myStrvar'

protected string myStrvar;

private void Page_Load(object sender, System.EventArgs e)

{

myStrvar = "Hello Universe";

Page.DataBind();

// Put user code to initialize the page here

}
 
Back
Top