ViewState
Om du vill behålla dina värden på variabler mellan postbacks, använd ViewState:
if (ViewState\["MaxNumberOfArticles"\] != null)
{
MaxNumberOfArticles = (int) ViewState\["MaxNumberOfArticles"\];
}
MaxNumberOfArticles += 10;
ViewState.Add("MaxNumberOfArticles", MaxNumberOfArticles);
RenderNews();
Webpart Livscykel
http://nishantrana.wordpress.com/2009/02/14/understanding-web-part-life-cycle/ OnInit – Configuration values set using WebBrowsable properties and those in web part task pane are loaded into the web part. LoadViewState – The view state of the web part is populated over here. CreateChildControls – All the controls specified are created and added to controls collection. When the page is being rendered for the first time the method generally occurs after the OnLoad() event. In case of postback, it is called before the OnLoad() event. We can make use of EnsureChildControls() - It checks to see if the CreateChildControls method has yet been called, and if it has not, calls it. OnLoad User Generated Event – for e.g. button click on the web part. OnPreRender – Here we can change any of the web part properties before the control output is drawn. RenderContents – Html Output is generated. SaveViewState - View state of the web part is serialized and saved. Dispose UnLoad.
Kontrollera om SPView finns
bool exists = splist.Views.Cast().Any(view => string.Equals(view.Title, viewName));
Lägg till en sida
Här är ett litet exempel på hur man kan lägga till sidor:
private void PublishNews(SPWeb web, string spamnewsname)
{
PublishingSite pSite = new PublishingSite(web.Site);
SPContentType ctype = pSite.ContentTypes\["NewsPageContentType"\];
PageLayoutCollection pageLayouts = pSite.GetPageLayouts(ctype, true);
PageLayout pageLayout = pageLayouts\["/\_catalogs/masterpage/MyNewsLayout.aspx"\];
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
PublishingPageCollection pPages = pWeb.GetPublishingPages();
PublishingPage pPage = pPages.Add(spamnewsname, pageLayout);
SPListItem newpage = pPage.ListItem;
newpage\["Title"\] = "Page added programmatically";
newpage\["NewsContent"\] = lorem;
newpage.Update();
newpage.File.CheckIn("all looks good");
newpage.File.Publish("all looks good");
}
formatera int
Här finns en lista över formateringsmöjligheter med C# string.Format
String.Format("{0:00000}", 15); // "00015"
resharper
Bara att rekommendera. Dagens: Ctrl-Shift-Backspace: Go to last edit. http://www.jetbrains.com/resharper/webhelp/Reference__Keyboard_Shortcuts.html http://blog.drorhelper.com/2009/05/15-resharper-keyboard-shortcuts-you.html
commaseparatedaccounts
om du lägger till commaseparatedaccounts i PeopleEditor, använd ett komma, inte semikolon, som man kan tro…
jQuery validation with asp.net
De kan var lite jobbigt att få till jQuery validate att funka ihop med aspnetForm…
$(document).ready(function () {
newsLetterForm = $("#aspnetForm");
newsLetterForm.validate({
onsubmit: false,
rules: {
"<%= emailTextBox.UniqueID %>": {
required: true,
email: true
}
}
});
$("#<%= sendButton.ClientID %>").live("click", function {
var isValid = newsLetterForm.valid();
if (!isValid) {
e.preventDefault();
}
});
});
jQuery validate kräver name på elementen inuti formen. För att ta reda på name, måste man använda Control.UniqueId För att det ska fungera måste du köra preventDefault()
webpart för att uppdatera title och description av web
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WelcomeTextUserControl.ascx.cs"
Inherits="MyProject.WelcomeTextUserControl" %>
<style type="text/css">
#welcome-text-wp-input-title
{
font-size: 2em;
}
.welcome-text-wp-edit-mode, #welcome-text-wp-updateImage
{
display: none;
}
</style>
<img src="/\_layouts/Images/homepageSamplePhoto.jpg" alt="sample" class="teamsite-welcome-text-image">
<h1 id="welcome-text-wp-web-title" class="welcome-text-wp-show-mode">
<%= SPContext.Current.Web.Title %></h1>
<p id="welcome-text-wp-web-description" class="welcome-text-wp-show-mode">
<%= SPContext.Current.Web.Description %></p>
<img src="../\_layouts/images/loading16.gif" id="welcome-text-wp-updateImage" />
<SharePoint:SPSecurityTrimmedControl ID="SPSecurityTrimmedControlUpdateWelcomeText"
runat="server" Permissions="ManageLists">
<div>
<div>
<input type="text" id="welcome-text-wp-input-title" value="<%= SPContext.Current.Web.Title %>"
class="welcome-text-wp-edit-mode input" />
</div>
<textarea rows="5" cols="25" id="welcome-text-wp-input-description" class="welcome-text-wp-edit-mode input"><%= SPContext.Current.Web.Description %></textarea>
<p id="welcome-text-wp-edit-button" class="welcome-text-wp-show-mode" onclick="javascript:toggleInputsVisibility();">
edit</p>
</div>
<div>
<input type="button" id="welcome-text-wp-input-submit" value="spara" class="welcome-text-wp-edit-mode button"
disabled="disabled" />
<input type="button" id="welcome-text-wp-input-cancel" value="avbryt" class="welcome-text-wp-edit-mode button" />
</div>
<script type="text/javascript">
function updateWeb(webTitle, webDescription) {
var ctx = new SP.ClientContext.get\_current();
this.web = ctx.get\_web();
web.set\_title(webTitle);
web.set\_description(webDescription);
this.web.update();
toggleUpdateImageVisibility();
ctx.executeQueryAsync(Function.createDelegate(this, this.onSuccessfulWebUpdate),
Function.createDelegate(this, this.onFailedWebUpdate));
}
function onSuccessfulWebUpdate(sender, args) {
var webTitle = jQuery("#welcome-text-wp-input-title").attr("value");
var webDescription = jQuery("#welcome-text-wp-input-description").attr("value");
jQuery("#welcome-text-wp-web-title").text(webTitle);
jQuery("#welcome-text-wp-web-description").text(webDescription);
toggleInputsVisibility();
toggleUpdateImageVisibility();
showWelcomeTextUpdateMessage("Välkomsttext uppdaterad")
}
function onFailedWebUpdate(sender, args) {
resetValuesAndToggleInputsVisibility();
toggleUpdateImageVisibility();
showWelcomeTextUpdateError('failed to update title. Error:' + args.get\_message())
}
function toggleInputsVisibility() {
jQuery(".welcome-text-wp-show-mode, .welcome-text-wp-edit-mode").toggle();
}
function toggleUpdateImageVisibility() {
jQuery("#welcome-text-wp-updateImage").toggle();
}
function resetValuesAndToggleInputsVisibility() {
var webTitle = jQuery("#welcome-text-wp-web-title").text().trim();
var webDescription = jQuery("#welcome-text-wp-web-description").text().trim();
jQuery("#welcome-text-wp-input-title").attr("value", webTitle);
jQuery("#welcome-text-wp-input-description").attr("value", webDescription);
toggleInputsVisibility();
}
function showWelcomeTextUpdateError(errorMessage) {
showWelcomeTextUpdateMessage(errorMessage);
}
function showWelcomeTextUpdateMessage(msg) {
SP.UI.Notify.addNotification(msg, false)
}
jQuery(document).ready(function () {
jQuery(".welcome-text-wp-edit-mode.input").live("keyup", function () {
jQuery(".welcome-text-wp-edit-mode.button").removeAttr("disabled");
});
jQuery("#welcome-text-wp-input-submit").live("click", function () {
var webTitle = jQuery("#welcome-text-wp-input-title").attr("value");
var webDescription = jQuery("#welcome-text-wp-input-description").attr("value");
updateWeb(webTitle, webDescription);
});
jQuery("#welcome-text-wp-input-cancel").live("click", function () {
resetValuesAndToggleInputsVisibility();
});
});
</script>
</SharePoint:SPSecurityTrimmedControl>