Author:
Joe Hakooz
Category:
ASP.NET
I'm working on a project that uses jQuery, an ASP.NET ListView, and RadAjax. The ajaxified ListView displays video thumbnails which, once clicked, opens a jQuery ColorBox to display the video.

Everything works fine until a partial page postback is triggered. Then the jQuery calls fail to work! This is of course because jQuery is only initialized after document.ready...
// JS FILE
$(document).ready(function() {
// ...jQuery code...
});
So after a partial page postback the above function is not called and it "breaks" all my jQuery calls.
The solution is very simple. I wrap all of my jQuery code in a function called initJQuery. I then call this function from document.ready...
// JS FILE
$(document).ready(function() {
initJQuery();
});
function initJQuery() {
// ...jQuery code...
};
Finally, I add ClientEvents-OnResponseEnd="OnResponseEnd" to the RadAjaxPanel declaration, add the OnResponseEnd javascript function to the top of my ASPX page, and simply call initJQuery from the OnResponseEnd function...
<!-- ASPX PAGE -->
<script type="text/javascript">
function OnResponseEnd(sender, eventArgs) {
initJQuery();
}
</script>
......
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" ClientEvents-OnResponseEnd="OnResponseEnd">
........
</telerik:RadAjaxPanel>
And presto... jQuery is initialized after each partial page postback. Of course you may need to limit when initJQuery is called for performance reasons, but you get the idea.
I hope this saves you some time!
Author:
Joe Hakooz
Category:
ASP.NET
So this one is strictly for the ASP.NET crowd...
The Scenario
I'm building an application that allows administrators to manage users from two different user databases. Let's call them dbadmins and dbusers. I need to allow administrators from dbadmin to authenticate to the application so they can manage users from both databases. Both databases have their own roles and profiles so they are completely independent of one another.
Although switching between membership providers is pretty simple, and well documented, switching between role and profile providers proved incredibly difficult with very little documentation. In other words, interacting with the non-default role and profile providers at runtime. I finally figured out how to do it with relatively little code, so I decided to post it here...
First of all, I'm using the standard user database configuration created with regsql.exe. I'm also using a custom table profile provider adapted by JB WebTech.
My web.config has two providers for membership, roles, and profiles. There are two connection strings as well. The default providers are for dbadmins. Here is a sample web.config...
......
<connectionStrings>
<clear />
<add name="AdminCS" connectionString="Initial Catalog=dbadmins;Data Source=XXX; User ID=XXX; Password=XXX;" providerName="System.Data.SqlClient" />
<add name="UsersCS" connectionString="Initial Catalog=dbusers;Data Source=XXX; User ID=XXX; Password=XXX;" providerName="System.Data.SqlClient" />
</connectionStrings>
......
<membership defaultProvider="AdminSqlMembershipProvider">
<providers>
<remove name="AspNetSqlMembershipProvider" />
<add name="AdminSqlMembershipProvider"
connectionStringName="AdminCS"
type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
applicationName="AdminApp"
passwordFormat="Hashed" />
<add name="UsersSqlMembershipProvider"
connectionStringName="UsersCS"
type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
applicationName="UsersApp"
passwordFormat="Hashed" />
</providers>
</membership>
<profile enabled="true" automaticSaveEnabled="false" defaultProvider="AdminSqlTableProfileProvider">
<providers>
<clear />
<add name="AdminSqlTableProfileProvider"
type="SqlTableProfileProvider"
connectionStringName="AdminCS"
table="aspnet_Profile"
applicationName="AdminApp" />
<add name="UsersSqlTableProfileProvider"
type="SqlTableProfileProvider"
connectionStringName="UsersCS"
table="aspnet_Profile"
applicationName="UsersApp" />
</providers>
<properties>
<add name="FName" defaultValue="[null]" customProviderData="FName;nvarchar;true" />
<add name="LName" defaultValue="[null]" customProviderData="LName;nvarchar;true" />
</properties>
</profile>
<roleManager enabled="true" defaultProvider="AdminSqlRoleProvider" cacheRolesInCookie="false">
<providers>
<clear />
<add name="AdminSqlRoleProvider"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="AdminCS"
applicationName="AdminApp" />
<add name="UsersSqlRoleProvider"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="UsersCS"
applicationName="UsersApp" />
</providers>
</roleManager>
......
Notice how the default providers point to AdminCS, which is dbadmins. The reason I did this is because only administrators from dbadmins can log into this application and default providers work much nicer with built in .NET controls like the Login control.
Editing Users in dbadmins (The default provider)
This was the easy part. I use a datagrid to pull in all my users from dbadmins. Edit and Add buttons allow me to change their information or add a new user. Here is an example of what happens when the buttons are clicked.
'VB Code Behind
' EDIT USER SAVE BUTTON
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
'Update User
Dim user As MembershipUser = Membership.GetUser(tbUserName.Text)
user.Email = tbEmail.Text
Membership.UpdateUser(user)
'Update Profile
Dim prof As ProfileBase = Profile.GetProfile(tbUserName.Text)
prof.Item("FName") = tbFName.Text
prof.Item("LName") = tbLName.Text
prof.Save()
'Update Roles
For Each li As ListItem In cblRoles.Items
If li.Selected And Not Roles.IsUserInRole(tbUserName.Text, li.Value) Then
Roles.AddUserToRole(tbUserName.Text, li.Value)
ElseIf (Not li.Selected And Roles.IsUserInRole(tbUserName.Text, li.Value)) Then
Roles.RemoveUserFromRole(tbUserName.Text, li.Value)
End If
Next
End Sub
'ADD USER BUTTON
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'Create User
Dim newUser As MembershipUser
Dim status As MembershipCreateStatus
newUser = Membership.CreateUser(tbUserName.Text, tbPass.Text, tbEmail.Text, tbQuestion.Text, tbAnswer.Text, True, status)
'Add to Roles
For Each li As ListItem In cblRoles.Items
If li.Selected Then
Roles.AddUserToRole(tbUserName.Text, li.Value)
End If
Next
'Update Profile
Dim prof As ProfileBase = Profile.GetProfile(tbUserName.Text)
prof.Item("FName") = tbFName.Text
prof.Item("LName") = tbLName.Text
prof.Save()
End Sub
Again, this is pretty straight forward and very well documented.
Editing Users in dbusers (NOT the default provider)
This was the tricky part. Remember, I'm logged in as an administrator for dbadmins (the default membership provider) so editing users in the same database was easy. But now I want to edit users in dbusers which are the non-default membership/roles/profile providers.
It was pretty easy to work with the membership and role providers by switching the provider in the code. There are still some differences but not a huge deal. But for the life of me I couldn't interact with the non-default profile provider. Behold the glory...
'VB Code Behind
' EDIT USER SAVE BUTTON
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
'Update User
Dim user As MembershipUser = Membership.Providers("UsersSqlMembershipProvider").GetUser(tbUserName.Text, False)
user.Email = tbEmail.Text
Membership.UpdateUser(user)
'Update Profile
Dim prof As SqlTableProfileProvider = Profile.Providers("UsersSqlTableProfileProvider")
Dim sc As SettingsContext = New SettingsContext()
sc.Add("UserName", tbUserName.Text)
sc.Add("IsAuthenticated", True)
Dim p As SettingsPropertyValueCollection = prof.GetPropertyValues(sc, ProfileBase.Properties)
p("FName").PropertyValue = tbFName.Text
p("LName").PropertyValue = tbLName.Text
prof.SetPropertyValues(sc, p)
End Sub
'ADD USER BUTTON
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim usernames As String() = {tbUserName.Text}
Dim addroles As String() = {tbRole.Text}
Dim newUser As MembershipUser
Dim status As MembershipCreateStatus
' Create User
newUser = Membership.Providers("UsersSqlMembershipProvider").CreateUser(tbUserName.Text, tbPass.Text, tbEmail.Text, tbQuestion.Text, tbAnswer.Text, True, Nothing, status)
' Add to Roles
Roles.Providers("UsersSqlRoleProvider").AddUsersToRoles(usernames, addroles)
' Update Profile
Dim prof As SqlTableProfileProvider = Profile.Providers("UsersSqlTableProfileProvider")
Dim sc As SettingsContext = New SettingsContext()
sc.Add("UserName", tbUserName.Text)
sc.Add("IsAuthenticated", True)
Dim p As SettingsPropertyValueCollection = prof.GetPropertyValues(sc, ProfileBase.Properties)
p("FName").PropertyValue = tbFName.Text
p("LName").PropertyValue = tbLName.Text
prof.SetPropertyValues(sc, p)
End Sub
One thing to note: Since dbusers only has one role, "Member", I did not include it in the edit button code, and I don't iterate through a checkbox list in the add button code. Also, notice how you must create a String Array when editing roles. You should be able to figure out how to do this if you have more than one role.
So that's it! When interacting with a non-default provider, you must change the provider at runtime as seen above. The code I provided has been edited for context of this post and does not contain security or error handling. There may be a typo or two but it should be more than enough to get you going.
Hopefully this will save someone out there a little pain and suffering!