开发者

Databinding to a property?

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 pa开发者_运维知识库ge 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?


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> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜