Set property in user control

S

stan

I have a user control where I defined a boolean property. I cannot set the
property in the code behind - the control is part of the Datagrid. I tried
to pass the property the following way:
<aaa:MyControl runat="server" id="stan" IsReady="<%# booleanReady %>"/>
However, that does not work. It looks like the "set" for the property does
not get executed. However, it works if I hardcode "true" or "false". I am
using .Net 1.1, VB. Can someone help?

TIA,
Stan
 
A

Alessandro Zifiglio

hi Stan, i think what you are missing is the call to your pages DataBind
method. Call this method versus only calling your DataGrids databind method.
The following test works and i dont see why it shouldnt work for you.
eg.

Dim booleanReady As Boolean = True
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
if (not ispostback) then
DataGrid1.DataSource = CreateDataSource() ' to your datasource
' Note that following line is commented out
'DataGrid1.DataBind()
Me.DataBind()
end if
End Sub

Regards,
Alessandro Zifiglio
http://www.AsyncUI.net
 
A

Alessandro Zifiglio

Stan, following is the come code i used to test under asp.net 1.1 :
Regards,
Alessandro Zifiglio
http://www.AsyncUI.net

Containing page .aspx :
<%@ Page Language="VB"%>
<html>
<head id="Head1">
<title>Untitled Page</title>
<script runat="server">
Dim booleanReady As Boolean = True
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
DataGrid1.DataSource = CreateDataSource()
'DataGrid1.DataBind()
Me.DataBind()
End Sub
Function CreateDataSource() As ICollection

' Create sample data for the DataList control.
Dim dt As DataTable = New DataTable()
Dim dr As DataRow

' Define the columns of the table.
dt.Columns.Add(New DataColumn("BoolValue", GetType(Boolean)))

' Populate the table with sample values.
dr = dt.NewRow()
dr(0) = true
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = false
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = true
dt.Rows.Add(dr)

Dim dv As DataView = New DataView(dt)
Return dv

End Function

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="booleanReady">
<ItemTemplate>
<uc1:WebUserControlTest id="WebUserControlTest1" IsReady='<%#
booleanReady %>' runat="server">
</uc1:WebUserControlTest>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>


your user control .ascx :
------------------------------
<%@ Control Language="VB"%>
<script runat="server">
Private isReadyValue As Boolean = False
Public Property IsReady() As Boolean
Get
Return isReadyValue
End Get
Set(ByVal value As Boolean)
isReadyValue = value
End Set
End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Label1.Text = IsReady.ToString()
End Sub
</script>
<asp:Label id="Label1" runat="server"></asp:Label>
 
S

Stan

Allessandro,
this is alomost the same thing I do. All works if the variable is
assigned a value that does not change. However, I set the value based on
a condition. it happens just before the bind. It looks like the "set"
is bypassed. I do the debug statement inside the "set" - it works only
when the value is hardcoded or defined in the code and not changed.

Stan
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top