PDA

View Full Version : Call control in Template Field


creativegenius
10-16-2009, 07:33 PM
This is the code a use to programingly change the attributes to a asp control. But when the control is in a DataList and ItemTemplate and I call it programingly it says the contol is not declared.

How do I call a control from a template field and change its attributes.

m.Attributes("src") = (msong.Text)

SouthwaterDave
10-17-2009, 05:11 PM
I am not sure I understand you question, but the following example is one of way of achiveing what I thought you asked:
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Function ChangeColour(ByVal value As Integer) As String
If value = 1 Then
Return "#f00"
Else
Return "#000"
End If
End Function
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Server=.\sqlexpress;Database=master;Trusted_Connection=yes" SelectCommand="select 1 as Num union select 2 union select 3" />
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<div style='color: <%#ChangeColour(Eval("Num"))%>'><%#Eval("Num")%></div>
</ItemTemplate>
</asp:DataList>
</body>
</html>