Home All Groups Group Topic Archive Search About

Referencing controls in a template programmatically

Author
8 Dec 2005 5:46 AM
karl
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

Author
11 Dec 2005 2:49 AM
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(
);
Author
11 Dec 2005 11:08 PM
karl
Thanks I'll give it a try but it does seem so messy
any idea why I just can't say formName.controlName = "whatever"


<agape***@gmail.com> wrote in message
Show quoteHide quote
news:1134269352.451216.209530@f14g2000cwb.googlegroups.com...
> 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(
> );
>
Author
11 Dec 2005 11:47 PM
karl
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

<k***@sunesis.com.au> wrote in message
Show quoteHide quote
news:eqUgXgq$FHA.916@TK2MSFTNGP10.phx.gbl...

> Thanks I'll give it a try but it does seem so messy
> any idea why I just can't say formName.controlName = "whatever"
>
>
> <agape***@gmail.com> wrote in message
> news:1134269352.451216.209530@f14g2000cwb.googlegroups.com...
>> 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(
>> );
>>
>
>