databinding at runtime.

A

Al Smith

I kind of understand how the below works, however I am trying to do it at runtime vrs design time.

<asp:TextBox id="TextBox1" runat="server" Text='<%# StateList.SelectedItem.Text %>'></asp:TextBox>

So what I really want to do is something like I show below.

In Page_Load I have:

TextBox txt;
txt= new TextBox();
Color clr = new Color() ;
clr = Color.DeepSkyBlue;
txt.BackColor = clr;
txt.Style["Left"] = 10 + "px";
txt.Style["Top"] = 200 + "px";
txt.Style["Width"] = 200 + "px";
txt.Style["Height"] = 80 + "px";
txt.Text = "<%# StateList.SelectedItem.Text %>"; // <--- Should I be able to do this at runtime?
PlaceHolder1.Controls.Add(txt);

And in aspx I have:
<asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder>

StateList is a list control but when setup using the PlaceHolder I do not get any binding to take place.

Thanks for your pointers!

Al
 
B

Brock Allen

txt.Text = "<%# StateList.SelectedItem.Text %>"; // <--- Should I be

The <%# %> syntax simply created this line of code:

txt.Text = StateList.SelectedItem.Text;

The main thing is when the line of code executes. <%# %> happens during databinding,
whereas <%= %> and <% %> happen during Rendering. DataBinding in v1.1 is
explicit, so it happens when you call it.
 
A

Al Smith

I think I understand what you said but my question is can I set the tags at runtime using a placeholder. My tests do confirm that is does not work as in my original post. So I am not sure if I am doing something wrong or that's the way it is..

<%# %> happens during databinding.... OK, it happens during databinding, but can I build it up at runtime as in...

TextBox txt;
txt= new TextBox();
Color clr = new Color() ;
clr = Color.DeepSkyBlue;
txt.BackColor = clr;
txt.Style["Left"] = 10 + "px";
txt.Style["Top"] = 200 + "px";
txt.Style["Width"] = 200 + "px";
txt.Style["Height"] = 80 + "px";
txt.Text = "<%# StateList.SelectedItem.Text %>"; // <--- Should I be able to do this at runtime for it does not seem to work?
PlaceHolder1.Controls.Add(txt);


Thanks
Al
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top