Tuesday, March 25, 2008

Display ToolTip in Javascripts

Following are the HTML and Javascripts code to display tooltip:

Html Code ---


<span id="spanName" style="font-weight: bold;" onmouseover="javascripts:DisplayToolTip('true');"
onmouseout="javascripts:DisplayToolTip('false');"></span>


<div id="divToolTip" style="position: absolute; display: none; z-index: 20;
background-color: #FFFFE1; border: solid 1px Black;">
</div>


<script type="text/javascript">
function DisplayToolTip(flag)
{
document.getElementById('divToolTip').style.left = window.event.x;
document.getElementById('divToolTip').style.top = window.event.y;

if(flag == 'true')
document.getElementById('divToolTip').style.display = 'block';
else if(flag == 'false')
document.getElementById('divToolTip').style.display = 'none';
}
</script>



Following image, shows how the ToolTip will appear after implementing the above code:








AJAX Rating control

Html code for Rating control ---

Page register directive ---


<%@ Register Assembly="AjaxControlToolkit, Version=1.0.11119.29799, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"
Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

AJAX Rating Control code ---


<ajaxToolkit:Rating ID="RORating" runat="server" BehaviorID="RORatingBehavior" CurrentRating="0" MaxRating="5" StarCssClass="ratingStar" WaitingStarCssClass="savedRatingStar" FilledStarCssClass="filledRatingStar" EmptyStarCssClass="emptyRatingStar" RatingAlign="Horizontal" RatingDirection="LeftToRightTopToBottom" Style="padding-top: 5px;" />

If you want to access the Rating control in Client side or in Javascrips then you have to set the BehaviorID of control.

Few examples of Get/Set of Rating Control:


//Rating
$find("RORatingBehavior").set_Rating(Number("2"));
var ratingValue = $find("RORatingBehavior").get_Rating();
$find("RORatingBehavior").set_ReadOnly(false);
$find("RORatingBehavior").set_ReadOnly(true);

To view the complete list of Get/Set APIs, you have open the Rating Control (in Javascripts) in Debug mode (Quick Watch) in Visual Studio. Following are the snapshots of Rating control in Quick Watch:

Note: APIs names are self-explanatory. 

JavaScript’s side APIs Snapshots:









If you are unable to see the contents of snapshots properly then following are its contents:

Get APIs :


get_AutoPostBack()
get_CallbackID()
get_ClientState()
get_ClientStateFieldID()
get_element() get_element()
get_EmptyStarCssClass()
get_events()
get_FilledStarCssClass()
get_id()
get_isInitialized()
get_isUpdating()
get_MaxRating()
get_name()
get_Rating()
get_RatingDirection()
get_ReadOnly()
get_StarCssClass()
get_Stars()
get_Tag()
get_WaitingStarCssClass()


Set APIs :


set_AutoPostBack()
set_CallbackID()
set_ClientState()
set_ClientStateFieldID()
set_EmptyStarCssClass()
set_FilledStarCssClass()
set_id()
set_MaxRating()
set_name()
set_Rating()
set_RatingDirection()
set_ReadOnly()
set_StarCssClass()
set_Tag()
set_WaitingStarCssClass()


Methods/Events Handlers :


_keyDownHandler()
_mouseOutHandler()
_onError()
_onKeyDownBack()
_onMouseOut()
_onStarClick()
_onStarMouseOver()
_partialUpdateBeginRequest()
_partialUpdateEndRequest()
_receiveServerData()
_starClickHandler()
_starMouseOverHandler()
_update()
_waitingMode()
add_disposing()
add_EndClientCallback()
add_MouseOut()
add_MouseOver()
add_propertyChanged()
add_Rated()
beginUpdate()
constructor()
dispose()
endUpdate()
initialize()
raiseEndClientCallback()
raiseMouseOut()
raiseMouseOver()
raisePropertyChanged()
raiseRated()
registerPartialUpdateEvents()
remove_disposing()
remove_EndClientCallback()
remove_MouseOut()
remove_MouseOver()
remove_propertyChanged()
remove_Rated()
updated()

Friday, March 14, 2008

Hide Web part title


There are two ways to hide the Title of a particular web part:
1) Modify the *.webpart file.
By changing the settings related ChromeState and ChromeType we can hide Title.



<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<data>
<properties>
<property name="ChromeState"
type="chromestate">Normal</property>
<property name="ChromeType" type="chrometype">None</property>
</properties>
</data>
</webPart>
</webParts>





2) Modify the Web part settings by SharePoint site.
a) Click "Modify Shared Web Part" menu.




b) Select "Chrome State" as "Normal" and "Chrome Type" as "None".




3) So the final result of changes will be…




Note: Some others interesting Web part properties are:





<webparts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<data>
<properties>
<property name="AllowMinimize" type="bool">False</property>
<property name="AllowClose" type="bool">False</property>
<property name="AllowEdit" type="bool">False</property>
<property name="ShowActionLinks" type="bool">False</property>
</properties>
</data>
</webPart>
</webparts>




Properties names are self-explanatory.


Thursday, March 13, 2008

JavaScript usage in Web Part


If you want to use JavaScript in your Web part then you have two options:


1) Refer to some external JavaScript file.



ClientScriptManager cs = Page.ClientScript;
// Include the required javascript file.
if (!cs.IsClientScriptIncludeRegistered("tab_javascript"))
cs.RegisterClientScriptInclude(this.GetType(), "tab_javascript",
"/_layouts/BCLBlast/tab.js");


I placed the JavaScript file at following location:


C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\BCLBlast







2) Write JavaScript in Web Part file.



// Initialize javascript variables to have list of all tabs and headers
if (!cs.IsClientScriptBlockRegistered("tabs_list"))
{
string tabVariables =
@"<script type=""text/javascript"">" +Environment.NewLine +
@"var tabs = [{0}] ;" + Environment.NewLine +
@"var tabheaders = [{1}] ;"+Environment.NewLine+
@"var _currentTab = null ;" + Environment.NewLine +
@"</script>";

string tabPages = "", tabHeaders = "";
for (int i = 0; i < SearchInformationSources.TotalSearchProviders; i++)
{
tabPages += "\"tab" + i.ToString() + "\", ";
tabHeaders += "\"tabheader" + i.ToString() + "\", ";
}

if (tabHeaders.Length > 1)
{
tabPages = tabPages.Remove(tabPages.Length - 2, 2);
tabHeaders = tabHeaders.Remove(tabHeaders.Length - 2, 2);
}

cs.RegisterClientScriptBlock(GetType(), "tabs_list",
String.Format(tabVariables, tabPages, tabHeaders));
}



Whole code snippet is as follows:





protected override void OnPreRender(EventArgs e)
{
ClientScriptManager cs = Page.ClientScript;
//Include the required javascript file.
if (!cs.IsClientScriptIncludeRegistered("tab_javascript"))
cs.RegisterClientScriptInclude(this.GetType(),
"tab_javascript", "/_layouts/BCLBlast/tab.js");


// Initialize javascript variables to have list of all tabs and headers
if (!cs.IsClientScriptBlockRegistered("tabs_list"))
{
string tabVariables =
@"<script type=""text/javascript"">" + Environment.NewLine +
@"var tabs = [{0}] ;" + Environment.NewLine +
@"var tabheaders = [{1}] ;" + Environment.NewLine +
@"var _currentTab = null ;" + Environment.NewLine +
@"</script>";

string tabPages = "", tabHeaders = "";
for (int i = 0; i < SearchInformationSources.TotalSearchProviders; i++)
{
tabPages += "\"tab" + i.ToString() + "\", ";
tabHeaders += "\"tabheader" + i.ToString() + "\", ";
}

if (tabHeaders.Length > 1)
{
tabPages = tabPages.Remove(tabPages.Length - 2, 2);
tabHeaders = tabHeaders.Remove(tabHeaders.Length - 2, 2);
}

cs.RegisterClientScriptBlock(GetType(), "tabs_list",
String.Format(tabVariables, tabPages, tabHeaders));
}
}




Here you to have to implement the OnPreRender method to write the JavaScript code.

AJAX in SharePoint 2007

Scenario:
I like to use AJAX Tab control in SharePoint as a web part.
For this I created an ASCX user control that contains the AJAX tab control and test the user control in ASP.Net's ASPX page. After successfully running the AJAX tab control in ASPX page, I use the tab control in Web part (regarding how to use ASCX in Web part, please refer earlier post).


Note: While testing the tab control in ASPX page, I kept the Script Manager element in the ASCX page. Later on while deploying the tab control in SharePoint, I moved the Script Manager element to Master page (mentioned in detail in current article at a later stage).


Steps:



1) Install ASPAJAXExtSetup.msi

C:\Program Files\Microsoft ASP.NET\ASP.NET 2.0 AJAX Extensions\v1.0.61025



2) Made following changes to site web.config :

Note: In this link it is mentioned in detail ---
http://www.asp.net/ajax/documentation/live/ConfiguringASPNETAJAX.aspx



a) <sectionGroup>



<configuration>
<configSections>
<sectionGroup name="system.web.extensions"
type="System.Web.Configuration.SystemWebExtensionsSectionGroup,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35">
<sectionGroup name="scripting"
type="System.Web.Configuration.ScriptingSectionGroup,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35">
<section name="scriptResourceHandler"
type="
System.Web.Configuration.ScriptingScriptResourceHandlerSection,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"
requirePermission="false"
allowDefinition="MachineToApplication" />
<sectionGroup name="webServices"
type="System.Web.Configuration.ScriptingWebServicesSectionGroup,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35">
<section name="jsonSerialization"
type="System.Web.Configuration.ScriptingJsonSerializationSection,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"
requirePermission="false"
allowDefinition="Everywhere" />
<section name="profileService"
type="System.Web.Configuration.ScriptingProfileServiceSection,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"
requirePermission="false"
allowDefinition="MachineToApplication" />
<section name="authenticationService"
type="
System.Web.Configuration.ScriptingAuthenticationServiceSection,
System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"
requirePermission="false"
allowDefinition="MachineToApplication" />
</sectionGroup>
</sectionGroup>
</sectionGroup>

-------
-------

</configSections>

-------
-------

</configuration>




b) <controls>



<pages enableSessionState="false" enableViewState="true"
enableViewStateMac="true" validateRequest="false"
pageParserFilterType="
Microsoft.SharePoint.ApplicationRuntime.SPPageParserFilter,
Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" asyncTimeout="7">

<controls>
<add tagPrefix="asp" namespace="System.Web.UI"
assembly="System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

</controls>

-----
-----
</pages>




c) <assemblies>



<compilation batch="false" debug="true">
<assemblies>
-----
-----
<add assembly="System.Web.Extensions,
Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" />
</assemblies>
-----
-----
</compilation>




d) <httpHandlers>


<httpHandlers>

------
------

<add verb="*" path="Reserved.ReportViewerWebControl.axd"
type="Microsoft.Reporting.WebForms.HttpHandler,
Microsoft.ReportViewer.WebForms,
Version=8.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
<add verb="*" path="*.asmx" validate="false"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions,
Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" />
<add verb="*" path="*_AppService.axd"
validate="false"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions,
Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" />
<add verb="GET,HEAD" path="ScriptResource.axd"
type="System.Web.Handlers.ScriptResourceHandler,
System.Web.Extensions,
Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" validate="false" />

</httpHandlers>




e) <httpModules>



<httpModules>
------
------
<add name="ScriptModule"
type="System.Web.Handlers.ScriptModule, System.Web.Extensions,
Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" />
</httpModules>



f) <SafeControls>


<SafeControls>
-----
-----
<SafeControl Assembly="System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TypeName="*"
Safe="True" />

</SafeControls>



g) <system.web.extensions>

<configuration>
------
------
<system.web.extensions>
<scripting>
<webServices>
<!-- Uncomment this line to enable the authentication
service. Include requireSSL="true"
if appropriate. -->
<!--
<authenticationService enabled="true"
requireSSL = "truefalse"/>
-->
<!-- Uncomment these lines to enable the profile service.
To allow profile properties to be retrieved and modified in
ASP.NET AJAX applications, you need to add
each property name to the readAccessProperties and
writeAccessProperties attributes.
-->
<!--
<profileService enabled="true"
readAccessProperties="propertyname1,propertyname2"
writeAccessProperties="propertyname1,propertyname2
" />
-->
</webServices>
<!--
<scriptResourceHandler enableCompression="true"
enableCaching="true" />
-->
</scripting>
</system.web.extensions>

</configuration>




h) <system.webServer>


<configuration>
------
------
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<add name="ScriptModule"
preCondition="integratedMode"
type="System.Web.Handlers.ScriptModule,
System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated" />
<add name="ScriptHandlerFactory" verb="*"
path="*.asmx"
preCondition="integratedMode"
type="
System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptHandlerFactoryAppServices"
verb="*" path="*_AppService.axd"
preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptResource"
preCondition="integratedMode"
verb="GET,HEAD"
path="ScriptResource.axd"
type="System.Web.Handlers.ScriptResourceHandler,
System.Web.Extensions, Version=1.0.61025.0,
Culture=neutral,
PublicKeyToken=31bf3856ad364e35" />
</handlers>
</system.webServer>

</configuration>




3) Adding a ScriptManager into a SharePoint MasterPage

Note: If you are using custom master page, then directly made required change in that. Otherwise, if you are using the default master page, then open your desired SharePoint site in SharePoint Designer. Open the default.master file located at “http:///_catalogs/masterpage/default.master” in SharePoint designer and made the required change in it.


Add the following into the markup of your page. A recommended location is right beneath the
WebPartManager registration (search for <WebPartPages:SPWebPartManager id="m" runat="Server" />).



<asp:ScriptManager runat="server" ID="
ScriptManager1 "></asp:ScriptManager>



4) In Web part solution you have to add the reference of System.Web.Extensions.

5) Following is the code of Web part file:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace BlastWP
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:SampleAJAXTab runat=server>")]
public class SampleAJAXTab : WebPart
{
private Control _control;

protected override void CreateChildControls()
{
this.EnsureUpdatePanelFixups();

_control = this.Page.LoadControl
(@"~/UserControls/BlastWP/TabbedPhases.ascx");
this.Controls.Add(_control);
}

private void EnsureUpdatePanelFixups()
{
if (this.Page.Form != null)
{
string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
{
this.Page.Form.Attributes["onsubmit"] =
"_spFormOnSubmitWrapper();";
}
}

ScriptManager.RegisterStartupScript(this,
typeof(SampleAJAXTab), "UpdatePanelFixup",
"_spOriginalFormAction = document.forms[0].action;
_spSuppressFormOnSubmitWrapper=true;", true);
}
}
}




Note: Here there is a hack related to AJAX Update Panel (refer method EnsureUpdatePanelFixups()).



6) After building the Web part solution, you have to follow normal steps like:


a) Strong name the Web part solution.
b) Create a *.webpart file.
c) Register dll in GAC.
d) Make entry of dll in site's config file.
e)Import *.webpart file in SharePoint.




Note: Currently the Tab headers are not coming properly in AJAX tab control.


Sample References:



  1. AJAX Control Toolkit
    http://www.codeplex.com/AtlasControlToolkit

  2. Installing ASP.NET AJAX
    http://www.asp.net/ajax/documentation/live/InstallingASPNETAJAX.aspx

  3. ASP.Net AJAX
    http://asp.net/ajax/default.aspx

  4. Configuring ASP.NET AJAX
    http://www.asp.net/ajax/documentation/live/ConfiguringASPNETAJAX.aspx

  5. Using the AJAX Control Toolkit in SharePoint
    http://weblogs.asp.net/jan/archive/2007/02/26/using-the-ajax-control-toolkit-in-sharepoint.aspx


Google