error: can't find control 'mylabel'

B

Ben

Hi,

In the code-behind, i can refer to the dropdownlist defined in the aspx file
with this (e.g.)
dropdownlist1.sekectedvalue = ...

but why can i not refer to the label defined in the TemplateField in the
same aspx file like below?
This gives the error: "can't find control 'mylabel' ":
mylabel.text="ok"

<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="mylabel" Runat="server"
Text='<%# Bind("field1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:DropDownList ID="dropdownlist1" runat="server" AutoPostBack="True" >
</asp:DropDownList>

Thanks
Ben
 
M

Marina Levit [MVP]

Because it is just a template. If there are 20 rows, there will be 20
labels. If there are 50 rows, there will be 50 labels.
So which one could 'myLabel' possibly refer to? How would the page know
which one of the unknown number of labels that will be created youw ant?

It can't.
 
B

Ben

Thanks for replying.
This is bad news for me, because i defined an InsertItemTemplate containing
a dropdownlist with the possible values into the detailsview (put into
insert mode) .
Result: the Controlparameter can't find controlID="dropdownlist1".
How can i put the selectedvalue of the dropdownlist into that template field
for inserting the new record?

Thanks again
Ben

See part of code:

<asp:SqlDataSource ID="SqlDataSource1"
...
InsertCommand="INSERT INTO [pc] ([name], [place]) VALUES (?, ?, @pl)"
"OldValuesParameterFormatString="original_{0}"
ProviderName="System.Data.OleDb">
<InsertParameters>
<asp:parameter Name="name" Type="String" />
<asp:ControlParameter ControlID="dropdownlist1" Name="pl"
PropertyName="SelectedValue" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

<asp:DetailsView ID="DetailsView1" runat="server" DataKeyNames="pcnr"
DataSourceID="SqlDataSource1" DefaultMode="Insert">
<Fields>
<asp:BoundField DataField="pcnr" HeaderText="pcnr"
ReadOnly="True"
<asp:BoundField DataField="name" />
<asp:TemplateField >
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList1"
runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2"
DataTextField="place DataValueField="placel">
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>

asp:SqlDataSource ID="SqlDataSource2"
....
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top