Friday 30 September 2011

sharepoint inputformtextbox not working updatepanel

side updatepanel






Recently I faced a weird issue while using Richtext or InputFormTextBox control of SharePoint. Actually there were many fields in the custom form, so to avoid postback I decided to use UpdatePanel for partial page update. Unfortunately my richtext contol was inside UpdatePanel and started showing weird behavior after asynchronous postback.

Before postback richtext control
Before Postback Richtext Control


After postback richtext control

After Postback Richtext Control

Actually SharePoint calls few JavaScript methods to maintain the toolbar and look of richtext control on page load. But while partial page update browser don't trigger window onload event. That is why, JavaScript methods associated with richtext control don't get triggered and control looks completely different after asynchronous postback.

It means, somehow we have to call these method after asynchronous postback complete. To solve this problem you can use client side endRequest event of asynchronous postback to call required javaScript methods richtext control.
Sample Code:

<script type="text/javascript">

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

function EndRequestHandler()

{

var richtextContolId = '<%= richtextControlId.ClientID %>';

if (browseris.ie5up && browseris.win32 && !IsAccessibilityFeatureEnabled()){

g_aToolBarButtons = null;

g_fRTEFirstTimeGenerateCalled=true;

RTE_ConvertTextAreaToRichEdit(richtextContolId, true, true, "", "1033",null, null, null, null, null,"FullHtml", "",null,null,null,null);

RTE_TextAreaWindow_OnLoad(richtextContolId);

}

else{

document.write("
Click for help about adding basic HTML formatting.
"
);

};

}

script>


That's all you need to do to fix the above issue. After each asynchronous postback "EndRequestHandler" method will get called and it will maintain the state of richtext control. Go through the following blog to get good idea about client side events:http://www.a2zmenu.com/Blogs/Ajax/Call-JavaScript-method-after-Ajax-call-complete.aspx .


Working with multiple Inputformtexboxes :

Problem with sharepoint inputformtextbox control with updatepanel

When using SharePoint:InputFormTextBox control in UpdatePanel I faced one issue. On my webpart part page I had one SharePoint:InputFormTextBox control with one asp:DropDown control with autopostback property true. On SelectedIndexChanged event of that dropdown, InputformTextBox control appears without toolbar. This behaviour was coming because the InputformTextBox is a TextArea control. It needs script to achieve the rich text box feature when loading the page and in update panel due to partial postback the script was not loading. To fix this issue we are required to load that script. I am giving you the steps to fix this issue:
Step 1: Put this script function in design code.
<script language="javascript" type="text/javascript">
function CreateRichEdit(elementid)
{
if (browseris.ie5up && browseris.win32 && !IsAccessibilityFeatureEnabled()){
g_aToolBarButtons = null;
g_fRTEFirstTimeGenerateCalled=true;
RTE_ConvertTextAreaToRichEdit(elementid, true, true, "", "1033", null, null, null, null, null,"FullHtml","\u002f",null,null,null,null);
RTE_TextAreaWindow_OnLoad(elementid);
RTE_DocEditor_AdjustHeight(elementid);
RTE_DocEditor_AdjustWidth(elementid);
}
else{
};
}
script>


Step 2: Put this code in code behind file

protected void Page_PreRender(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "@@CreateRichEdit", "", false);
}

Tuesday 13 September 2011

Custom new form validation on Save Ok button

Another undocumented piece of SharePoint.

I want to validate two fields on a new list item form by invoking JavaScript custom function. They are two date fields and I want to ensure that the end date can't happen before the start date. My first idea was to attach a validation function on the onclick event of the Submit button.

I started by inspecting the generated HTML of the form. The Submit button already has a onclick() code which is:

if (!PreSaveItem()_) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl13$g_740c1963_b0da_4b45_9b71_0dcca5d082b0$ctl00$toolBarTbl$RightRptControls$ctl00$ctl00$diidIOSaveItem", "", true, "", "", false, true))

Searching in the SharePoint JavaScript files in the LAYOUT folder, I found the definition of PreSaveItem function in FORMS.JS file. It simply invokes PreSaveAction function, if defined.

Finally, it was just a matter of inserting a custom function named PreSaveAction in a

Monday 12 September 2011

Classic ASP Read Excel with Microsoft.Jet.OLEDB.4.0

Reading Excel 2003 file using  Microsoft.Jet.OLEDB.4.0

In connection string
HDR=No - returns column headers  as row
HDR=Yes -  returns only rows without column headers
IMEX=1 - entire first row cells are converted as text



M-1
Dim conn,rs
set conn=Server.CreateObject("ADODB.Connection")
dim sFileConnectionString,sFileSQL, curValue
sFileConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"&_
"Data Source=c:\mdalldownload.xls;Extended Properties=""Excel 8.0;HDR=No;IMEX=1;"";"
    sFileSQL =  "SELECT * FROM [mdalldownload$]"
            dim oFileCN, objRS, aSourceData
            set oFileCN = server.createobject("ADODB.Connection")
            oFileCN.Open sFileConnectionString
            set objRS = oFileCN.Execute(sFileSQL)
            Response.Write("<table border=""1"">")
Response.Write("<tr>")
For x=0 To objRS.Fields.Count-1
   'Response.Write("<th>" & objRS.Fields(x).Name & "</th>")
Next
Response.Write("</tr>")
o Until objRS.EOF
Response.Write("<tr>")
For x=0 To objRS.Fields.Count-1
curValue = objRS.Fields(x).Value
If IsNull(curValue) Then
curValue="N/A"
End If
curValue = CStr(curValue)
Response.Write("<td>" & curValue & "</td>")
Next
Response.Write("</tr>")
objRS.MoveNext
Loop
objRS.Close
Response.Write("</table>")

M-2
Function ReadExcel( myXlsFile, mySheet, my1stCell, myLastCell, blnHeader )
' Function :  ReadExcel
' Version  :  2.00
' This function reads data from an Excel sheet without using MS-Office
'
' Arguments:
' myXlsFile   [string]   The path and file name of the Excel file
' mySheet     [string]   The name of the worksheet used (e.g. "Sheet1")
' my1stCell   [string]   The index of the first cell to be read (e.g. "A1")
' myLastCell  [string]   The index of the last cell to be read (e.g. "D100")
' blnHeader   [boolean]  True if the first row in the sheet is a header
'
' Returns:
' The values read from the Excel sheet are returned in a two-dimensional
' array; the first dimension holds the columns, the second dimension holds
' the rows read from the Excel sheet.
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com
    Dim arrData( ), i, j
    Dim objExcel, objRS
    Dim strHeader, strRange

    Const adOpenForwardOnly = 0
    Const adOpenKeyset      = 1
    Const adOpenDynamic     = 2
    Const adOpenStatic      = 3

    ' Define header parameter string for Excel object
    If blnHeader Then
        strHeader = "HDR=YES;"
    Else
        strHeader = "HDR=NO;"
    End If

    ' Open the object for the Excel file
    Set objExcel = CreateObject( "ADODB.Connection" )
    ' IMEX=1 includes cell content of any format; tip by Thomas Willig
    objExcel.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
                  myXlsFile & ";Extended Properties=""Excel 8.0;IMEX=1;" & _
                  strHeader & """"

    ' Open a recordset object for the sheet and range
    Set objRS = CreateObject( "ADODB.Recordset" )
    strRange = mySheet & "$" & my1stCell & ":" & myLastCell
    objRS.Open "Select * from [" & strRange & "]", objExcel, adOpenStatic

    ' Read the data from the Excel sheet
    i = 0
    Do Until objRS.EOF
        ' Stop reading when an empty row is encountered in the Excel sheet
        If IsNull( objRS.Fields(0).Value ) Or Trim( objRS.Fields(0).Value ) = "" Then Exit Do
        ' Add a new row to the output array
        ReDim Preserve arrData( objRS.Fields.Count - 1, i )
        ' Copy the Excel sheet's row values to the array "row"
        ' IsNull test credits: Adriaan Westra
        For j = 0 To objRS.Fields.Count - 1
            If IsNull( objRS.Fields(j).Value ) Then
                arrData( j, i ) = ""
            Else
                arrData( j, i ) = Trim( objRS.Fields(j).Value )
            End If
        Next
        ' Move to the next row
        objRS.MoveNext
        ' Increment the array "row" number
        i = i + 1
    Loop

    ' Close the file and release the objects
    objRS.Close
    objExcel.Close
    Set objRS    = Nothing
    Set objExcel = Nothing

    ' Return the results
    ReadExcel = arrData
End Function