difference between <%# %> and <$%= %> block

Z

z. f.

Hi,

i'm using code in my aspx page.

i have data binding where i use <%# Container.DataItem("DateStart") %>
i also use code that makes a loop inside a regular
<%
%>
block
how can i pass data from the databinding to the loop that runs in a regular
block.
if i try to call the Container.DataItem from the regular code block i get
error it is not defined in this stage.
how can i make loop in my data binding stage?
why is this not documented so well?
if there is a link to some good documentation it will be halpfull also.

TIA, z.
 
J

Jos

z. f. said:
Hi,

i'm using code in my aspx page.

i have data binding where i use <%# Container.DataItem("DateStart") %>
i also use code that makes a loop inside a regular
<%
%>
block
how can i pass data from the databinding to the loop that runs in a regular
block.
if i try to call the Container.DataItem from the regular code block i get
error it is not defined in this stage.
how can i make loop in my data binding stage?
why is this not documented so well?
if there is a link to some good documentation it will be halpfull also.

You need to understand that the <%# %> block is executed
when you call the DataBind() method of the page or control.

The <% %> block is executed during the rendering of the page,
this is after all the events have been handled, all the other code
has been executed, and right before the page is sent to the client.
At that time, the datasource has already been destroyed, which
is why you don't have access to the DataItem.

A good way to solve your problem in ASP.NET would be to
replace the <% %> block by a Literal or Label server control, and
to set the contents of the Literal or Label control right before or
after the DataBind() call.

Jos
 
Z

z. f.

i would be glad to accept your suggestion,
the problem is that my code runs in a loop inside a repeater, this why i
had the problem in the first place.
 
S

Stephen Woolhead

Move all the code into the OnItemDataBound event handler something like this

private void dlMailing_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.Item)
{
DbDataRecord dbr = (DbDataRecord)e.Item.DataItem;
CheckBox cb = (CheckBox )e.Item.FindControl ("cbSubscribe") ;
Label lb = (Label )e.Item.FindControl ("lblMailingList") ;
cb.Text = dbr["Name"].ToString () ;
lb.Text = dbr["Description"].ToString () ;

// Some loop here....

}
}


Stephen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top