Showing posts with label Development. Show all posts
Showing posts with label Development. Show all posts

Wednesday, January 26, 2011

SPFieldUser is not persistent during postback

If you are using a custom listform and refering to a field of type SPFieldUser. The field tend to loose the content (users) during postback. Some times not easily detected because it will keep the viewstate sometimes.

This has been seen on versions 2007 and 2010.

The fix is relatively simple, by putting the contents into the viewstate in code.

Example:

In the listform.aspx

<SharePoint:FormField runat="server" id="Fld_Present" ControlMode="Edit" FieldName="Present" />

Snippets from codebehind

protected FormField Fld_Present; // Define the field in codebehind

// Define a private String for holding the value during postbacks

 

private String Present

{

    get

    {

        object obj = ViewState["Present"];

        return (obj == null) ? String.Empty : (String)obj;

    }

    set

    {

        ViewState["Present"] = value;

    }

}

 

protected override void CreateChildControls()

        {

            base.CreateChildControls();

 

            if (Fld_Attendees.Value.ToString() != "")

                this.Attendees = Fld_Attendees.Value.ToString();

            else

                Fld_Attendees.Value = new SPFieldUserValueCollection(SPContext.Current.Web, this.Attendees);

 

To keep values when the user field is edited, also add some code before postbacks that is defined in codebehind:

 

if (Fld_Attendees.Value.ToString() != "")

                    this.Attendees = Fld_Attendees.Value.ToString();

 

Thursday, December 11, 2008

Regular expression to get ALL images in a page

I have been using a RegEx for a while thar matches all image tags in a HTML document. Suddenly the application one of the applications that is using the function ignored images with "space" in the name.

I had to modify my RegEx a bit... A great tool to use when you need a RegEx Editor is RegEx Buddy. It always helps me in figuring out what I'm doing wrong, and it helps in figuring out expressions gathered from the Internet.

Here is my RegEx that matches all images including src tags with a space in the filename:

<img[^>]*?src\s*=\s*[""']?(<?Filename>[^'"">]+\s*)[ '""][^>]*?>


And the C# function that gets the images into a list


public static List<String> GetImagesFromContent(String Html)

{


List<string> UrlList = new List<string>();


string regExPattern = @"<img[^>]*?src\s*=\s*[""']?(<?Filename>[^'"">]+\s*)[ '""][^>]*?>";


Regex r = new Regex(regExPattern, RegexOptions.IgnoreCase RegexOptions.Singleline);

MatchCollection matches = r.Matches(Html);


foreach (Match m in matches)

{

UrlList.Add(m.Groups["Filename"].Value);

}


return UrlList;


}

Wednesday, December 3, 2008

Visualize web page performance in Safari

When developing ASP.NET solutions I always check the rendered pages in a lot of browsers. After checking that the output is "OK" I sometimes check the page performance with Fiddler (Not every page and solution is tested...) After downloading Safari I have begun to debug and test performance using Safari.

First off you must enable the "Develop" menuitem, or enable debug mode in Safari. Many posts describes a way that edits the Safari Preferences.plist files. With the current version of Safari this is NOT the way to enable this. It will not make all the utilities work, it displays the Develop menu. But it does not execute the Web Inspector.

The right way to enable the "Develop" menu in Safari (version 3.2.1) is to goto Edit->Preferences and choose Advanced.


Click on the "Show Develop menu in the menubar" and close the window.

If you have done the file file fiddling, just go and toggle the "Show Develop menu in the menubar" off and on again to fix the problem.

OK Now open the "Show Network Timeline" and take a look at the nice graphics displaying the breakdown of your pages.