Hi All!
Instead of using the session to save/load the DockState for a page, I would like to use the Profiles. I've not been able to figure out how though. Has anyone got a working example - including the web.config profile definitions?
I'm more than a little desparate. hehe
Cheers
Mark.
Instead of using the session to save/load the DockState for a page, I would like to use the Profiles. I've not been able to figure out how though. Has anyone got a working example - including the web.config profile definitions?
I'm more than a little desparate. hehe
Cheers
Mark.
10 Answers, 1 is accepted
0
BSolveIT
Top achievements
Rank 2
answered on 02 Oct 2007, 03:33 PM
On second thoughts, forget the above.
I've been spending some time going through the PortalSiteVB project, and I really like the way it works. However, has anyone extended it such that it works on a per user basis?
I've been spending some time going through the PortalSiteVB project, and I really like the way it works. However, has anyone extended it such that it works on a per user basis?
0
Hello BSolveIT,
I believe this was discussed in another thread with you. I kindly suggest that we continue the discussion there. The community of course is welcome to share their experience on the matter.
Best wishes,
Petya
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
I believe this was discussed in another thread with you. I kindly suggest that we continue the discussion there. The community of course is welcome to share their experience on the matter.
Best wishes,
Petya
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
0
BSolveIT
Top achievements
Rank 2
answered on 03 Oct 2007, 11:19 AM
Hi Petya
The other place this is being discussed is in a support ticket thread, not a public forum thread. You might want to copy the answer you gave me in the support ticket to here though?
Cheers
Mark
The other place this is being discussed is in a support ticket thread, not a public forum thread. You might want to copy the answer you gave me in the support ticket to here though?
Cheers
Mark
0
Hello BSolveIT,
Here is the code as requested by you, now attached in the forum thread. The application demonstrates our PortalSiteVB demo modified to have a Login page and store information about each user in the database so docks state is saved for each user and loaded when this exact user logs in. The application does not use Profiles, but it demonstrates a possible approach to saving state as per user basis.
Sincerely yours,
Petya
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Here is the code as requested by you, now attached in the forum thread. The application demonstrates our PortalSiteVB demo modified to have a Login page and store information about each user in the database so docks state is saved for each user and loaded when this exact user logs in. The application does not use Profiles, but it demonstrates a possible approach to saving state as per user basis.
Sincerely yours,
Petya
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
0
BSolveIT
Top achievements
Rank 2
answered on 04 Oct 2007, 12:40 PM
This is brilliant! Thanks!
However, it still leaves the problem of anonymous users? We want all users, including anonymous, to be able to see our pages and move things around, etc. We don't want to keep any data relating to the anonymous users after their session has expired though.
Any thoughts?
Cheers
Mark.
However, it still leaves the problem of anonymous users? We want all users, including anonymous, to be able to see our pages and move things around, etc. We don't want to keep any data relating to the anonymous users after their session has expired though.
Any thoughts?
Cheers
Mark.
0
Hi BSolveIT,
For the anonymous users here is one suggestion. We do not have an example to demonstrate it however.
1. Add a new user account for anonymous users in the database table Users
2. You could add a link in the Login page and in code-behind if this link was pressed store in Session["UserID"] the ID of an Anonymous account in the database that you have added.
3. Redirect the anonymous user to the main page where instead of loading information from the database with some preserved state for this special account, you delete all information in DB for the anonymous user and start with no information. In this way the anonymous user will always have an initial start with no previously saved data.
4. Throughout the work of the anonymous user, information will be updated in DB (if you want otherwise, then you will have to modify the current code a lot)
This is just a suggestion and it might have its drawbacks. Hope you still find it useful. The question is general as a whole and a bit aside from RadDock as a control but we still tried to offer an idea. In case some other questions concerning specifically our controls arise, feel free to ask.
Greetings,
Petya
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
For the anonymous users here is one suggestion. We do not have an example to demonstrate it however.
1. Add a new user account for anonymous users in the database table Users
2. You could add a link in the Login page and in code-behind if this link was pressed store in Session["UserID"] the ID of an Anonymous account in the database that you have added.
3. Redirect the anonymous user to the main page where instead of loading information from the database with some preserved state for this special account, you delete all information in DB for the anonymous user and start with no information. In this way the anonymous user will always have an initial start with no previously saved data.
4. Throughout the work of the anonymous user, information will be updated in DB (if you want otherwise, then you will have to modify the current code a lot)
This is just a suggestion and it might have its drawbacks. Hope you still find it useful. The question is general as a whole and a bit aside from RadDock as a control but we still tried to offer an idea. In case some other questions concerning specifically our controls arise, feel free to ask.
Greetings,
Petya
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
0
BSolveIT
Top achievements
Rank 2
answered on 05 Oct 2007, 04:36 PM
I've been working on a very similar type of solution.
I've decided to assume that all users are anonymous, at least to begin with (which of course they are - at least to begin with) and so in the web.config I have:
in the Global.asax:
I then use this as the Username to store in the users table, and get the ID of this new user, which is stored in the users profile.
I then had to update the function GetPageIdForCurrentUser in the PageInfo class, so that it adds a new entry in the pages table for new users:
So the database is then used as normal for this users session.
If the user then registers (using the proper memberhsips and roles system), I migrate the anonymous users profile.
(how to: MSDN: Migrate Anonymous Users)
Thats about it.
I've decided to assume that all users are anonymous, at least to begin with (which of course they are - at least to begin with) and so in the web.config I have:
<anonymousIdentification enabled="true" cookieSlidingExpiration="true" /> |
in the Global.asax:
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) |
If Not User.Identity.IsAuthenticated Then |
Session("Anonymous") = Request.AnonymousID |
Dim userID As Object = UserInfo.GetUserID(Session("Anonymous")) |
If userID IsNot Nothing Then |
Session("UserID") = Int32.Parse(userID.ToString()) |
Else |
Using connection As SqlConnection = CommonDataAccess.GetOpenConnection() |
Using command As New SqlCommand("INSERT INTO Users (username) VALUES(@Username);" _ |
& "SELECT CAST(scope_identity() AS int);", connection) |
command.Parameters.AddWithValue("@Username", Session("Anonymous")) |
userID = command.ExecuteScalar() |
Session("UserID") = Int32.Parse(userID.ToString()) |
End Using |
End Using |
End If |
Else |
Dim userID As Object = UserInfo.GetUserID(User.Identity.Name) |
If userID IsNot Nothing Then |
Session("UserID") = Int32.Parse(userID.ToString()) |
Else |
End If |
End If |
End Sub |
I then use this as the Username to store in the users table, and get the ID of this new user, which is stored in the users profile.
I then had to update the function GetPageIdForCurrentUser in the PageInfo class, so that it adds a new entry in the pages table for new users:
Public Shared Function GetPageIdForCurrentUser(ByVal userID As Integer, ByVal PageRef As Integer) As Integer |
Using connection As SqlConnection = CommonDataAccess.GetOpenConnection() |
Using command As New SqlCommand("SELECT * FROM Pages WHERE UserID = @UserID AND PageRef = @PageRef", connection) |
command.Parameters.AddWithValue("@UserID", userID) |
command.Parameters.AddWithValue("@PageRef", PageRef) |
Dim asdasd As Object = command.ExecuteScalar() |
Dim PageID As Integer = CInt(asdasd) |
If PageID = 0 Then |
Using connection2 As SqlConnection = CommonDataAccess.GetOpenConnection() |
Using command2 As New SqlCommand("INSERT INTO Pages (PageRef, UserID) VALUES(@PageRef, @UserID);" _ |
& "SELECT CAST(scope_identity() AS int);", connection) |
command2.Parameters.AddWithValue("@UserID", userID) |
command2.Parameters.AddWithValue("@PageRef", PageRef) |
asdasd = command2.ExecuteScalar() |
PageID = CInt(asdasd.ToString()) |
End Using |
End Using |
End If |
Return PageID |
End Using |
End Using |
End Function |
So the database is then used as normal for this users session.
If the user then registers (using the proper memberhsips and roles system), I migrate the anonymous users profile.
(how to: MSDN: Migrate Anonymous Users)
Thats about it.
0
BSolveIT
Top achievements
Rank 2
answered on 06 Oct 2007, 03:45 PM
By the way, the pages table in the database in the LoginPortalSiteVB.zip project above doesn't match up with the PageInfo class.
In my solution (snippets above) I altered both the pages table and the PageInfo class, but only slightly.
My new PageInfo.vb class is as follows:
I thought I should post this in case anyone is trying to follow/implement the same.
- Mark
In my solution (snippets above) I altered both the pages table and the PageInfo class, but only slightly.
My new PageInfo.vb class is as follows:
Imports System |
Imports System.Data |
Imports System.Configuration |
Imports System.Web |
Imports System.Web.Security |
Imports System.Web.UI |
Imports System.Web.UI.WebControls |
Imports System.Web.UI.WebControls.WebParts |
Imports System.Web.UI.HtmlControls |
Imports System.Data.SqlClient |
Imports System.Collections.Generic |
Public Class PageInfo |
Public Shared Function [Select](id As Integer) As PageInfo |
Dim connection As SqlConnection = CommonDataAccess.GetOpenConnection() |
Try |
Dim command As New SqlCommand("SELECT * FROM Pages WHERE PageID = @PageID", connection) |
Try |
command.Parameters.AddWithValue("@PageID", id) |
Dim reader As SqlDataReader = command.ExecuteReader() |
Try |
If reader.Read() Then |
Return New PageInfo(reader) |
End If |
Finally |
reader.Dispose() |
End Try |
Finally |
command.Dispose() |
End Try |
Finally |
connection.Dispose() |
End Try |
Return Nothing |
End Function 'Select |
Public Shared Function GetPageIdForCurrentUser(ByVal userID As Integer, ByVal PageRef As Integer) As Integer |
Using connection As SqlConnection = CommonDataAccess.GetOpenConnection() |
Using command As New SqlCommand("SELECT * FROM Pages WHERE UserID = @UserID AND PageRef = @PageRef", connection) |
command.Parameters.AddWithValue("@UserID", userID) |
command.Parameters.AddWithValue("@PageRef", PageRef) |
Dim asdasd As Object = command.ExecuteScalar() |
Dim PageID As Integer = CInt(asdasd) |
If PageID = 0 Then |
Using connection2 As SqlConnection = CommonDataAccess.GetOpenConnection() |
Using command2 As New SqlCommand("INSERT INTO Pages (PageRef, UserID) VALUES(@PageRef, @UserID);" _ |
& "SELECT CAST(scope_identity() AS int);", connection) |
command2.Parameters.AddWithValue("@UserID", userID) |
command2.Parameters.AddWithValue("@PageRef", PageRef) |
asdasd = command2.ExecuteScalar() |
PageID = CInt(asdasd.ToString()) |
End Using |
End Using |
End If |
Return PageID |
End Using |
End Using |
End Function |
Public Sub New(reader As SqlDataReader) |
_PageID = CInt(reader("PageID")) |
_PageRef = CInt(reader("PageRef")) |
_UserID = CStr(reader("UserID")) |
End Sub 'New |
Public ReadOnly Property ID() As Integer |
Get |
Return _PageID |
End Get |
End Property |
Public Property PageRef() As Integer |
Get |
Return _PageRef |
End Get |
Set(ByVal value As Integer) |
_PageRef = value |
End Set |
End Property |
Public Property UserID() As Integer |
Get |
Return _UserID |
End Get |
Set(ByVal value As Integer) |
_UserID = value |
End Set |
End Property |
Private _PageID As Integer |
Private _PageRef As Integer |
Private _UserID As Integer |
End Class 'PageInfo |
I thought I should post this in case anyone is trying to follow/implement the same.
- Mark
0
Hi BSolveIT,
Thank you for the provided code and suggestions. Your active participation in our community has been greatly appreciated.
Regards,
Petya
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Thank you for the provided code and suggestions. Your active participation in our community has been greatly appreciated.
Regards,
Petya
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
0
Joff
Top achievements
Rank 1
answered on 05 Sep 2012, 02:33 AM
For those wishing to do this in C#
I wanted to create this as a custom control that I could drop into a web application, so instead of Default.aspx, I created PageWidgets.ascx - I also bypassed the login page, as I will be using authentication built into our CMS to authenticate users. In the example below, I have just used a set user ID of 2.
This could quite easily be customised back to using the Login process as the original example showed.
I hope the following is of assitance to someone, as the forums here have been very sueful to me.
Cheers
BaseWidget.cs
CommonDataAccess.cs
I wanted to create this as a custom control that I could drop into a web application, so instead of Default.aspx, I created PageWidgets.ascx - I also bypassed the login page, as I will be using authentication built into our CMS to authenticate users. In the example below, I have just used a set user ID of 2.
This could quite easily be customised back to using the Login process as the original example showed.
I hope the following is of assitance to someone, as the forums here have been very sueful to me.
Cheers
BaseWidget.cs
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
namespace
WebUI.CustomControlsIntranet
{
public
abstract
class
BaseWidget : System.Web.UI.UserControl
{
public
string
Configuration
{
get
{
return
_configuration; }
set
{
_configuration = value;
Initialize();
}
}
protected
abstract
void
Initialize();
private
string
_configuration;
}
}
CommonDataAccess.cs
PageInfo.csusing
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Data.SqlClient;
namespace
WebUI.CustomControlsIntranet
{
public
class
CommonDataAccess
{
public
static
SqlConnection GetOpenConnection()
{
SqlConnection connection =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
"ConnectionString1"
].ToString());
connection.Open();
return
connection;
}
}
}
PageWidgetInfo.csusing
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Data.SqlClient;
using
System.Collections.Generic;
namespace
WebUI.CustomControlsIntranet
{
public
class
PageInfo
{
public
static
PageInfo Select(
int
id)
{
SqlConnection connection = CommonDataAccess.GetOpenConnection();
try
{
SqlCommand command =
new
SqlCommand(
"SELECT * FROM Pages WHERE PageID = @PageID"
, connection);
try
{
command.Parameters.AddWithValue(
"@PageID"
, id);
SqlDataReader reader = command.ExecuteReader();
try
{
if
(reader.Read()) {
return
new
PageInfo(reader);
}
}
finally
{
reader.Dispose();
}
}
finally
{
command.Dispose();
}
}
finally
{
connection.Dispose();
}
return
null
;
}
//Select
public
static
int
GetPageIdForCurrentUser(
int
userID)
{
using
(SqlConnection connection = CommonDataAccess.GetOpenConnection()) {
using
(SqlCommand command =
new
SqlCommand(
"SELECT * FROM Pages WHERE UserID = @UserID"
, connection)) {
command.Parameters.AddWithValue(
"@UserID"
, userID);
object
asdasd = command.ExecuteScalar();
return
Convert.ToInt32(asdasd);
}
}
}
public
PageInfo(SqlDataReader reader)
{
_id = Convert.ToInt32(reader[
"PageID"
]);
_name = Convert.ToString(reader[
"Name"
]);
_path = Convert.ToString(reader[
"Path"
]);
}
//New
public
int
ID {
get
{
return
_id; }
}
public
string
Path {
get
{
return
_path; }
set
{ _path = value; }
}
public
string
Name {
get
{
return
_name; }
set
{ _name = value; }
}
private
int
_id;
private
string
_name;
private
string
_path;
}
}
UserInfo.csusing
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
Telerik.Web.UI;
namespace
WebUI.CustomControlsIntranet
{
public
class
PageWidgetInfo : IComparable<PageWidgetInfo>
{
public
static
List<PageWidgetInfo> SelectAllWidgets(
int
userId)
{
List<PageWidgetInfo> widgets =
new
List<PageWidgetInfo>();
using
(SqlConnection connection = CommonDataAccess.GetOpenConnection())
{
using
(SqlCommand command =
new
SqlCommand(
"SELECT * FROM PageWidget INNER JOIN Widgets ON PageWidget.WidgetID = Widgets.WidgetID INNER JOIN Pages ON Pages.PageID = PageWidget.PageID WHERE Pages.UserID = @UserID"
, connection))
{
command.Parameters.AddWithValue(
"@UserID"
, userId);
using
(SqlDataReader reader = command.ExecuteReader())
{
while
(reader.Read())
{
widgets.Add(
new
PageWidgetInfo(reader));
}
}
}
}
return
widgets;
}
public
static
PageWidgetInfo Select(
int
pageWidgetID)
{
List<PageWidgetInfo> widgets =
new
List<PageWidgetInfo>();
using
(SqlConnection connection = CommonDataAccess.GetOpenConnection())
{
using
(SqlCommand command =
new
SqlCommand(
"SELECT * FROM PageWidget INNER JOIN Widgets ON PageWidget.WidgetID = Widgets.WidgetID WHERE PageWidgetID = @PageWidgetID"
, connection))
{
command.Parameters.AddWithValue(
"@PageWidgetID"
, pageWidgetID);
using
(SqlDataReader reader = command.ExecuteReader())
{
if
(reader.Read())
{
return
new
PageWidgetInfo(reader);
}
}
}
}
return
null
;
}
public
static
List<PageWidgetInfo> SelectPageWidgets(
int
pageID)
{
List<PageWidgetInfo> widgets =
new
List<PageWidgetInfo>();
using
(SqlConnection connection = CommonDataAccess.GetOpenConnection())
{
using
(SqlCommand command =
new
SqlCommand(
"SELECT * FROM PageWidget INNER JOIN Widgets ON PageWidget.WidgetID = Widgets.WidgetID WHERE PageWidget.PageID = @PageID"
, connection))
{
command.Parameters.AddWithValue(
"@PageID"
, pageID);
using
(SqlDataReader reader = command.ExecuteReader())
{
while
(reader.Read())
{
widgets.Add(
new
PageWidgetInfo(reader));
}
}
}
}
return
widgets;
}
public
static
void
Delete(
int
pageWidgetID)
{
using
(SqlConnection connection = CommonDataAccess.GetOpenConnection())
{
using
(SqlCommand command =
new
SqlCommand(
"DELETE FROM PageWidget WHERE PageWidgetID = @PageWidgetID"
, connection))
{
command.Parameters.AddWithValue(
"@PageWidgetID"
, pageWidgetID);
command.ExecuteNonQuery();
}
}
}
public
static
void
DeletePageWidgets(
int
pageID)
{
using
(SqlConnection connection = CommonDataAccess.GetOpenConnection())
{
using
(SqlCommand command =
new
SqlCommand(
"DELETE FROM PageWidget WHERE PageID = @PageID"
, connection))
{
command.Parameters.AddWithValue(
"@PageID"
, pageID);
command.ExecuteNonQuery();
}
}
}
public
static
PageWidgetInfo Insert(
int
pageID,
int
widgetID,
string
configuration,
string
state)
{
using
(SqlConnection connection = CommonDataAccess.GetOpenConnection())
{
using
(SqlCommand command =
new
SqlCommand(
"INSERT INTO PageWidget (PageID, WidgetID, Configuration, State) VALUES (@PageID, @WidgetID, @Configuration, @State) SELECT @@IDENTITY"
, connection))
{
command.Parameters.AddWithValue(
"@PageID"
, pageID);
command.Parameters.AddWithValue(
"@WidgetID"
, widgetID);
command.Parameters.AddWithValue(
"@Configuration"
, configuration);
command.Parameters.AddWithValue(
"@State"
, state);
object
newID = command.ExecuteScalar();
return
Select(Convert.ToInt32(Convert.ToDecimal(newID)));
}
}
}
public
void
Update()
{
using
(SqlConnection connection = CommonDataAccess.GetOpenConnection())
{
using
(SqlCommand command =
new
SqlCommand(
"UPDATE PageWidget SET Configuration=@Configuration, State=@State WHERE PageWidgetID = @PageWidgetID"
, connection))
{
command.Parameters.AddWithValue(
"@State"
, State);
command.Parameters.AddWithValue(
"@Configuration"
, Configuration);
command.Parameters.AddWithValue(
"@PageWidgetID"
, ID);
command.ExecuteNonQuery();
}
}
}
public
PageWidgetInfo(SqlDataReader reader)
{
_id = (
int
)reader[
"PageWidgetID"
];
_pageID = (
int
)reader[
"PageID"
];
_widgetID = (
int
)reader[
"WidgetID"
];
_name = (
string
)reader[
"Name"
];
_path = (
string
)reader[
"Path"
];
_configuration = (
string
)reader[
"Configuration"
];
_state = (
string
)reader[
"State"
];
}
public
int
ID
{
get
{
return
_id; }
}
public
int
PageID
{
get
{
return
_pageID; }
}
public
int
WidgetID
{
get
{
return
_widgetID; }
}
public
string
Name
{
get
{
return
_name; }
}
public
string
Path
{
get
{
return
_path; }
}
public
string
Configuration
{
get
{
return
_configuration; }
set
{ _configuration = value; }
}
public
string
State
{
get
{
return
_state; }
set
{ _state = value; }
}
public
DockState DockState
{
get
{
if
(_dockState ==
null
)
{
_dockState = Telerik.Web.UI.DockState.Deserialize(_state);
}
return
_dockState;
}
}
private
string
_configuration;
private
int
_id;
private
int
_pageID;
private
int
_widgetID;
private
string
_name;
private
string
_path;
private
string
_state;
private
DockState _dockState;
public
int
CompareTo(PageWidgetInfo obj)
{
return
DockState.Index.CompareTo(obj.DockState.Index);
}
}
}
WidgetInfo.csusing
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Data.SqlClient;
namespace
WebUI.CustomControlsIntranet
{
public
class
UserInfo
{
public
static
object
GetUserID(
string
username,
string
password)
{
using
(SqlConnection connection = CommonDataAccess.GetOpenConnection())
{
using
(SqlCommand command =
new
SqlCommand(
"SELECT [userID] FROM Users WHERE [username]=@Username AND [password]=@Password"
, connection))
{
command.Parameters.AddWithValue(
"@Username"
, username);
command.Parameters.AddWithValue(
"@Password"
, password);
return
command.ExecuteScalar();
}
}
}
}
}
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Collections.Generic;
using
System.Data.SqlClient;
namespace
WebUI.CustomControlsIntranet
{
public
class
WidgetInfo
{
public
static
WidgetInfo Select(
int
widgetID)
{
using
(SqlConnection connection = CommonDataAccess.GetOpenConnection())
{
using
(SqlCommand command =
new
SqlCommand(
"SELECT * FROM Widgets WHERE WidgetID = @WidgetID"
, connection))
{
command.Parameters.AddWithValue(
"@WidgetID"
, widgetID);
using
(SqlDataReader reader = command.ExecuteReader())
{
if
(reader.Read())
{
return
new
WidgetInfo(reader);
}
}
}
}
return
null
;
}
public
static
List<WidgetInfo> Select()
{
List<WidgetInfo> widgets =
new
List<WidgetInfo>();
using
(SqlConnection connection = CommonDataAccess.GetOpenConnection())
{
using
(SqlCommand command =
new
SqlCommand(
"SELECT * FROM Widgets"
, connection))
{
using
(SqlDataReader reader = command.ExecuteReader())
{
while
(reader.Read())
{
widgets.Add(
new
WidgetInfo(reader));
}
}
}
}
return
widgets;
}
public
WidgetInfo(SqlDataReader reader)
{
_id = (
int
)reader[
"WidgetID"
];
_name = (
string
)reader[
"Name"
];
_path = (
string
)reader[
"Path"
];
}
public
int
ID
{
get
{
return
_id; }
}
public
string
Path
{
get
{
return
_path; }
}
public
string
Name
{
get
{
return
_name; }
}
private
int
_id;
private
string
_name;
private
string
_path;
}
}
With the code behind<%@ Control Language=
"C#"
AutoEventWireup=
"true"
CodeBehind=
"PageWidgets.ascx.cs"
Inherits=
"WebUI.CustomControlsIntranet.PageWidgetClass"
%>
<%@ Register Assembly=
"Telerik.Web.UI"
Namespace=
"Telerik.Web.UI"
TagPrefix=
"telerik"
%>
<div>
<asp:scriptmanager runat=
"server"
id=
"ScriptManager1"
>
</asp:scriptmanager>
Widget Configuration:
<asp:textbox runat=
"server"
id=
"TextConfiguration"
></asp:textbox>
<asp:dropdownlist runat=
"server"
id=
"ListWidgets"
>
</asp:dropdownlist>
<asp:button runat=
"server"
id=
"ButtonAddDock"
text=
"Add Dock"
onclick=
"ButtonAddDock_Click"
/>
<telerik:raddocklayout id=
"DockLayout"
runat=
"server"
onloaddocklayout=
"DockLayout_LoadDockLayout"
onsavedocklayout=
"DockLayout_SaveDockLayout"
storelayoutinviewstate=
"false"
>
<div>
<telerik:raddockzone runat=
"server"
id=
"ZoneLeft"
style=
"float: left; margin-right: 10px;"
width=
"45%"
minheight=
"200px"
>
</telerik:raddockzone>
<telerik:raddockzone runat=
"server"
id=
"ZoneRight"
style=
"float: left;"
minheight=
"200px"
width=
"45%"
>
</telerik:raddockzone>
</div>
<div style=
"display: none"
>
<asp:updatepanel runat=
"server"
id=
"UpdatePanel1"
>
<triggers>
<asp:asyncpostbacktrigger controlid=
"ButtonAddDock"
eventname=
"Click"
/>
</triggers>
</asp:updatepanel>
</div>
</telerik:raddocklayout>
<asp:loginstatus id=
"LoginStatus1"
runat=
"server"
logoutpageurl=
"~/Login.aspx"
/>
<asp:hiddenfield id=
"currentPageId"
runat=
"server"
/>
</div>
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Collections.Generic;
using
Telerik.Web.UI;
namespace
WebUI.CustomControlsIntranet
{
public
partial
class
PageWidgetClass : System.Web.UI.UserControl
{
//Private PageID As Integer = 1
private
List<PageWidgetInfo> _pageWidgets;
protected
List<PageWidgetInfo> PageWidgets {
get
{
if
(_pageWidgets ==
null
) {
// int userId = Int32.Parse(Session["UserId"].ToString());
int
userId = 2;
currentPageId.Value = PageInfo.GetPageIdForCurrentUser(userId).ToString();
_pageWidgets = PageWidgetInfo.SelectAllWidgets(userId);
}
return
_pageWidgets;
}
}
protected
override
void
OnInit(EventArgs e)
{
foreach
(PageWidgetInfo pageWidget
in
PageWidgets) {
RadDock dock = CreateDock(pageWidget);
DockLayout.Controls.Add(dock);
CreateSaveStateTriggers(dock);
}
base
.OnInit(e);
}
protected
override
void
OnLoad(EventArgs e)
{
if
(!IsPostBack) {
ListWidgets.DataSource = WidgetInfo.Select();
ListWidgets.DataTextField =
"Name"
;
ListWidgets.DataValueField =
"ID"
;
ListWidgets.DataBind();
}
base
.OnLoad(e);
}
protected
RadDock CreateDock(PageWidgetInfo pageWidgetInfo)
{
RadDock dock =
new
RadDock();
dock.ApplyState(pageWidgetInfo.DockState);
dock.ID = pageWidgetInfo.ID.ToString();
dock.Tag = pageWidgetInfo.ID.ToString();
BaseWidget widget = LoadControl(pageWidgetInfo.Path)
as
BaseWidget;
if
(widget ==
null
) {
throw
new
Exception(
string
.Format(
"The UserControl with path '{0}' does not inherit from BaseWidget. All widgets must inherit from BaseWidget."
, pageWidgetInfo.Path));
}
dock.ContentContainer.Controls.Add(widget);
widget.Configuration = pageWidgetInfo.Configuration;
//Currently, there is a problem with the RadDock command items:
// if they are not explicitly created, the Command event it not fired.
dock.Commands.Add(
new
DockCloseCommand());
dock.Commands.Add(
new
DockExpandCollapseCommand());
dock.Command += Dock_Command;
return
dock;
}
protected
RadDock CreateDock(
int
widgetID,
string
widgetConfiguration)
{
WidgetInfo widget = WidgetInfo.Select(widgetID);
if
(widget ==
null
) {
throw
new
Exception(
string
.Format(
"Cannot find a Widget with ID={0}."
, widgetID));
}
PageWidgetInfo newPageWidget = PageWidgetInfo.Insert(Int32.Parse(currentPageId.Value), widgetID, widgetConfiguration,
string
.Empty);
PageWidgets.Add(newPageWidget);
RadDock dock = CreateDock(newPageWidget);
dock.Title = widget.Name;
return
dock;
}
protected
void
Dock_Command(
object
sender, DockCommandEventArgs e)
{
if
(e.Command.Name ==
"Close"
) {
ScriptManager.RegisterStartupScript(UpdatePanel1,
this
.GetType(),
"RemoveDock"
,
string
.Format(
"function _removeDock() {{"
+
"Sys.Application.remove_load(_removeDock);"
+
"$find('{0}').undock();"
+
"$get('{1}').appendChild($get('{0}'));"
+
"$find('{0}').doPostBack('DockPositionChanged');"
+
"}};"
+
"Sys.Application.add_load(_removeDock);"
, ((RadDock)sender).ClientID, UpdatePanel1.ClientID),
true
);
}
}
private
PageWidgetInfo FindWidget(
int
id)
{
foreach
(PageWidgetInfo info
in
PageWidgets) {
if
(info.ID == id) {
return
info;
}
}
return
null
;
}
protected
void
DockLayout_SaveDockLayout(
object
sender, DockLayoutEventArgs e)
{
foreach
(DockState state
in
DockLayout.GetRegisteredDocksState()) {
int
stateWidgetID =
int
.Parse(state.Tag);
PageWidgetInfo pageWidget = FindWidget(stateWidgetID);
if
((pageWidget !=
null
) && !state.Closed) {
pageWidget.State = state.ToString();
pageWidget.Update();
}
else
{
PageWidgetInfo.Delete(stateWidgetID);
}
}
}
private
void
CreateSaveStateTriggers(RadDock dock)
{
dock.AutoPostBack =
true
;
dock.CommandsAutoPostBack =
true
;
AsyncPostBackTrigger saveStateTrigger =
new
AsyncPostBackTrigger();
saveStateTrigger.ControlID = dock.ID;
saveStateTrigger.EventName =
"DockPositionChanged"
;
UpdatePanel1.Triggers.Add(saveStateTrigger);
saveStateTrigger =
new
AsyncPostBackTrigger();
saveStateTrigger.ControlID = dock.ID;
saveStateTrigger.EventName =
"Command"
;
UpdatePanel1.Triggers.Add(saveStateTrigger);
}
protected
void
ButtonAddDock_Click(
object
sender, EventArgs e)
{
RadDock dock = CreateDock(
int
.Parse(ListWidgets.SelectedValue), TextConfiguration.Text);
UpdatePanel1.ContentTemplateContainer.Controls.Add(dock);
CreateSaveStateTriggers(dock);
ScriptManager.RegisterStartupScript(dock,
this
.GetType(),
"AddDock"
,
string
.Format(
"function _addDock() {{"
+
"Sys.Application.remove_load(_addDock);"
+
"$find('{1}').dock($find('{0}'));"
+
"$find('{0}').doPostBack('DockPositionChanged');"
+
"}};"
+
"Sys.Application.add_load(_addDock);"
, dock.ClientID, ZoneLeft.ClientID),
true
);
}
protected
void
DockLayout_LoadDockLayout(
object
sender, DockLayoutEventArgs e)
{
foreach
(PageWidgetInfo pageWidget
in
PageWidgets)
{
e.Positions[pageWidget.DockState.UniqueName] = pageWidget.DockState.DockZoneID;
e.Indices[pageWidget.DockState.UniqueName] = pageWidget.DockState.Index;
}
}
}
}