Referencing controls in a template programmatically

G

Guest

Hi

Anyone know the code required to reference, i.e set and modify the controls
inside a template, for example I have a form control that contains a number
of textboxes in a template. I want to avoid setting the reference on the
html page such as

Text='<%# Bind("FORNumber") %>'

Instead I want to bind them programmatically, I can't find out where or how
to do this, any help appreciated,

Cheers

Karl Edwards
 
A

agapeton

Try stuff like this...

controlName.Controls[0].Controls[0].Controls[1]

combined with

controlName.FindControl("controlName2")

to make something that looks like this

gvMyData.Controls[0].Controls.[1].Controls[0].FindControl("ddlMyNames")

Thats how you can grab the control...

Then you can do something like this

((DropDownList)(gvMyData.Controls[0].Controls.[1].Controls[0].FindControl("ddlMyNames")).DataSource
= GetData( );

((DropDownList)(gvMyData.Controls[0].Controls.[1].Controls[0].FindControl("ddlMyNames")).DataBind(
);
 
G

Guest

Thanks I'll give it a try but it does seem so messy
any idea why I just can't say formName.controlName = "whatever"
 
G

Guest

Just to make it a bit clearer, the following is the way asp.net 2 creates
the code placing the controls in the template and binding them on the aspx
page. I don't want any code on the aspx page.

FORNumber:
<asp:Label ID="txtFORNumber" runat="server" Text='<%# Bind("FORNumber")
%>'></asp:Label><br />

I want to do this programmatically so the <%# Bind("FORNumber") %> is done
in the code behind. The controls are inside a form control on the
ItemTemplate

Cheers

Thanks I'll give it a try but it does seem so messy
any idea why I just can't say formName.controlName = "whatever"


Try stuff like this...

controlName.Controls[0].Controls[0].Controls[1]

combined with

controlName.FindControl("controlName2")

to make something that looks like this

gvMyData.Controls[0].Controls.[1].Controls[0].FindControl("ddlMyNames")

Thats how you can grab the control...

Then you can do something like this

((DropDownList)(gvMyData.Controls[0].Controls.[1].Controls[0].FindControl("ddlMyNames")).DataSource
= GetData( );

((DropDownList)(gvMyData.Controls[0].Controls.[1].Controls[0].FindControl("ddlMyNames")).DataBind(
);
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,126
Latest member
FastBurnketoIngredients
Top