Monday, August 31, 2009

Disable /Readonly /Hiding a form field in newform.aspx

Never thought that I could disallow user to edit a field in newform.aspx using simple javascript function. As soon as the page gets loaded i m finding the input box (need to disable a input field if you need to disable any other field change the params for getTagFromIdentifierAndTitle) and disabling it, so user cannot modify it. One can make use of the PreSaveAction function which is always called before request gets posted to server, where I am re-enabling the textbox.

Need to copy this Javascript function in newform.aspx with the help of Sharepoint designer in Placeholder main. Remember that I have a list with column name JSReadOnly so one will change it if they are having some other name. I havent tried but I hope it should also work field types other than TextField with little change.

function PreSaveAction()
{
var field = getTagFromIdentifierAndTitle("INPUT","TextField","JSReadOnly");
//alert(field.value);
field.disabled=false;
setTimeout('setFieldDisable()',1000);
return true;
}

function getTagFromIdentifierAndTitle(tagName, identifier, title)
{
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tempstring =" tags[i].id;" title ="=" identifier ="=">

function setFieldDisable()
{
var field1 = getTagFromIdentifierAndTitle("INPUT","TextField","JSReadOnly");
if(field1!=null)
field1.disabled=true;
else
setTimeout('setFieldDisable()',500);
}

setTimeout('setFieldDisable()',500);



Beware that there are pros and cons of SPD :)

Friday, August 7, 2009

Chrome Type and Title getting blank

Today one issue kept me real busy. Somehow I was able to solve.

The issue I faced was, after saving the properties of the webpart the "Title" and "Chrome Type" of webpart was getting blank. Weird, but it was working some days ago.

Googled it but couldn't find any solution. Aha I like it when I stuck with something new unique problem... sometimes gotta use my own head.
Thought may be either of the overridden method must be getting failed but in each method Try Catch was taking care of this.
I noticed that some of the custom properties were able to save themselves and retained their values, quite amazing. So thought some Custom Properties might be throwing error .... bingo

In one of the property I had used page.session to get and set value.

public string StringTopicID
{
get
return Convert.ToString(Page.Session["ID"]);
}
set
{
Page.Session["ID"] = value;
}
}
I cleared the code

public string StringTopicID
{
get { return "125";}
set { ; }
}
And it work back again, I was able to retain the value of Title and Chrome Type.... wow.
Removed the session thing and used viewstate instead

I am still finding the exact reason... Will update once I got time to find it.

Long live Mi ros ft, luv U!!!