How to add a new event to controls in ListFormWebPart on the EditFrom.aspx / NewFrom.aspx pages

by James 2/24/2008 3:29:00 PM

Background - The controls in EditForm.aspx and NewForm.aspx pages you see when you add/edit a item in SharePoint list library are generated by ListFormWebPart.

The problem - We want to attach a new event to the button (or any controls in this page). For example, we want to popup a javascript alert message when user click on "OK" button without breaking current onclick event.

Solution - This can be easily done by using javascript. And re-register onclick event for the OK button.

First, we need to find out the ID used by OK button control. When you view the source html of EditForm.aspx/NewForm.aspx, you can see the ID of OK button is ending with "SaveItem".

button_id

We also need to find out the default onclick event script of this OK button. So that we can insert our custom script at beginning of the default script or append it after.

button_onclick

From above screen shot we can see that default onclick event begins with string "If (PreSaveItem()).....". So we are going to replace this with our custom script. "alert('you have clicked OK');if (!PreSaveItem())"

The actual JavaScript like following: (The script can be warpped within <script> tag and put insided <asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server"> in NewForm.aspx and EditForm.aspx )

[code:js] 

_spBodyOnLoadFunctionNames.push("StartUpCustomScript");
function StartUpCustomScript() {    
    ChangeOkButtonOnclickEvent("Input","SaveItem");

function ChangeOkButtonOnclickEvent(tagName, identifier) {
    var len = identifier.length;
    var tags = document.getElementsByTagName(tagName);  /*find all Input type controls on page (ie. control with tag <Input/>)*/
    for (var i=0; i < tags.length; i++) {
        var tempString = tags[i].name;       
        if(tempString.indexOf(identifier) == tempString.length-len ) /*find any Input type controls on page has its name ending with 'SaveItem'*/
        {
           /*if found, replace it default onclick with our custom script*/
           var func = tags[i].attributes["onclick"].value.replace("if (!PreSaveItem())","alert('you have clicked OK');if (!PreSaveItem())");
           /*remove its default onclick event*/
           tags[i].onclick = null;
           /*re-register its onlick event with new script*/
           tags[i].attachEvent("onclick",new Function(func));
       }
    }
    return null;
}  

[/code]

And now we click on OK button, we can see our Alert message :)

button_clicked

Currently rated 2.5 by 2 people

  • Currently 2.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

Programming | SharePoint

Comments

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen Adopted by James Tsai

About the author

Name of author James Tsai
.NET / SharePoint Consultant
Columbus, OH

E-mail me Send mail

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
View posts in large calendar

Certifications

MCPD
MCTS

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in