views:

25

answers:

1

I'm using the following syntax to bind to a div element:

<div id="previewdiv"><%=Preview%></div>

Where Preview is a property on my page.

The catch is that I'm creating this in Javascript on a new page in an onclick event. On the server side, I'm able to reference the new page via this property but for some reason when the page is postback the variable is getting set to the default initialized value and not to the value that I set in my page, i.e Preview = string. When I postback a second time then the page will be updated with the value I set.

I could perhaps move the code to the Init but I need to get values from controls to Initialize this property.

Ideas?

+1  A: 

The problem you're running into is that, using traditional ASP.NET Web Forms, <%= %> code is evaluated very early in the page lifecycle, before your code has had a chance to run.

Instead, you want to use ASP.NET Data Binding, which uses a different syntax, like this: <%# %>. (note the "#"). Then, to get this code to render, you've got to call the DataBind() of some server-side control when you're ready to replace the template with your actual data.

So in your server code you do something like this:

Preview = someString;
previewDiv.DataBind();

And in your markup, something like this:

<div runat=server id="previewdiv"><%#Preview%></div> 
Justin Grant