TODO:
In Visual Studio, you try to click on Security or other items, and you receive "Team Foundation Error: Login Failure: Unknown Username or Bad Password"
SOLUTION:
You need to start Visual Studio as your Domain User
start -> run > runas /netonly /user:my domain\myuser "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe
NOTES:
There are no notes on this topic.
TODO:
You have a list of objects, and you want to update all items with a particular value, based off of a particular condition.
SOLUTION:
string newAccountNumber ="12345";
string previousAccountNumber = "54321";
foreach (var account in dsAccountList.FindAll(a => a.AccountNumber == previousAccountNumber))
{
account.AccountNumber = newAccountNumber;
}
NOTES:
The example will update the account number for all accounts with AccountNumber = previousAccountNumber
TODO:
You have an Application Page, or regular ASPX page, .Net 3.5 and you notice that each Postback is happening twice, for no apparent reason.
SOLUTION:
If you have an IMG SRC="" tag, or a placeholder like IMG SRC="#" this will cause the issue. If dynamically generating SRC values, be sure that it is actually being populated with a valid image path.
NOTES:
This bug has been fixed in .Net 4.0+
TODO:
You have a list of objects (List<T>), let's just say List<Dog> for this example. You want to sort them by a property called "Breed". This example will show you just how to do that.
SOLUTION:
List myDogList = myDogList.OrderBy(d => d.Breed).ToList();
//or
List myDogList = myDogList.OrderByDescending(d => d.Breed).ToList();
NOTES:
Your list of dogs will now be sorted by Breed.
*****Note that the default sort order is ascending.
TODO:
You impersonate a user in an event receiver, and the result of calling Update() on the list item means that the "Modified By" user is that of the impersonated account, and not the original user that was logged into the site. The solution is pretty simple, just use the code below to set that user back to the original user.
SOLUTION:
private void ApplyPermissions(SPItemEventProperties properties)
{
string debugMessage = string.Empty;
Guid webId = properties.ListItem.Web.ID;
Guid siteId = properties.SiteId;
SPUserToken sysToken = null;
SPUser currentUser = null;
try
{
currentUser = properties.Web.CurrentUser;
//open site, and get elevated user
using (SPSite site = new SPSite(siteId))
{
//use our site collection
using (SPWeb myWeb = site.OpenWeb(webId))
{
sysToken = myWeb.AllUsers["i:0#.w|mydomain\\impusr"].UserToken;
}
}
bool allowUnsafeUpdates = false;
//open site with elevated user
using (SPSite site = new SPSite(properties.SiteId, sysToken))
{
//get the web
using (SPWeb myWeb = site.OpenWeb(properties.ListItem.Web.ID))
{
try
{
allowUnsafeUpdates = myWeb.AllowUnsafeUpdates; //save unsafe update setting
myWeb.AllowUnsafeUpdates = true; //allow unsafe updates for now
SPListItem elevatedListItem = null;
elevatedListItem = myWeb.Lists[properties.ListId].Items[properties.ListItem.UniqueId]; //get elevated list item
//DO ELEVATED ACTIONS HERE
base.EventFiringEnabled = false;
elevatedListItem["Editor"] = currentUser; //set current user to editor and author
elevatedListItem["Author"] = currentUser;
elevatedListItem.Update(); //update the item
base.EventFiringEnabled = true;
}
catch (Exception x)
{
throw x;
}
finally
{
myWeb.AllowUnsafeUpdates = allowUnsafeUpdates; //put back original setting
}
}//using web
}//using site
}
catch (Exception x)
{
properties.ErrorMessage = x.ToString();
}
}
NOTES:
This code ran in an ItemAdded and ItemUpdated Event Receiver
TODO:
Have you ever wanted to add the onblur or onfocus event to a TextBox in the Codebehind?
SOLUTION:
//add focus
myTextbox.Attributes.Add("onfocus", "MyJavascriptFocusFunction(this);");
//add blur
myTextbox.Attributes.Add("onblur", "MyJavascriptBlurFunction(this);");
NOTES:
There are no notes on this topic.
TODO:
Have you ever wanted to "click" a button when someone presses return on a Textbox? The code below, goes in the codebehind of your aspx file.
SOLUTION:
myTextBox.Attributes.Add("onkeypress", "if(event.keyCode==13) {document.getElementById('" + myButton.ClientID + "').click(); return false;}");
NOTES:
There are no notes on this topic.
TODO:
Do you want to have a google search widget for BlogEngine.Net?
SOLUTION:
Download the GoogleSearch.rar file below. Create a directory for it in the widgets folder which is located under the root. Unzip all files to that folder. Now when you look at your page, you can add the widget to your pages.
NOTES:
There are no notes on this topic
GoogleSearch.rar (3.05 kb)
TODO:
You open a website in Visual Studio, and get the dreaded "It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS" error message.
SOLUTION:
1. Make sure you opened the site and not the parent folder.
2. In my case, I created a folder called "backup", and copied my entire site to it. Therefore, when I opened the site, that folder was loaded, and it too had a Web.Config file in it (since this folder contained a complete site backup). Simple remove that Web.Config and you will be set.
example showing my problematic folder structure:
root
Web.Config
site folder 1
site folder 2
backup
Web.Config
site folder 1
site folder 2
NOTES:
The presence of the backup folder in red is the culprit. It contained a backup copy of the root web.config, which contained entries only allowed in the root level Web.Config.
TODO:
Have you ever wanted to recursively clear a screen, without the need to manually clear each control? This can be extremely helpful when it comes to large forms.
SOLUTION:
public void ClearControls(Control InControl, bool ClearLists, bool SetRadioButtonDefaultToFirstItem, bool SetDropDownDefaultToFirstItem)
{
foreach (Control control in InControl.Controls)
{
//if its a check box, just clear the text
if (control is CftcTextBox)
{
((CftcTextBox)control).Text = string.Empty;
}
else if (control is CheckBoxList)
{
//deselect each check box item.
foreach (System.Web.UI.WebControls.ListItem item in ((CheckBoxList)control).Items)
{
item.Selected = false;
}
}
else if (control is DropDownList)
{
//deselect all items
foreach (System.Web.UI.WebControls.ListItem item in ((DropDownList)control).Items)
{
item.Selected = false;
}
//if we choose, we now select the first item in list
if (SetDropDownDefaultToFirstItem)
{
if(((DropDownList)control).Items.Count > 0)
{
((DropDownList)control).SelectedIndex = 0;
}
}
}
else if (control is System.Web.UI.WebControls.ListBox)
{
//if we clear lists, then remove all items. otherwise, just deselect all list items.
if (ClearLists)
{
((System.Web.UI.WebControls.ListBox)control).Items.Clear();
}
else
{
foreach (System.Web.UI.WebControls.ListItem item in ((System.Web.UI.WebControls.ListBox)control).Items)
{
item.Selected = false;
}
}
}
else if (control is RadioButtonList)
{
//default to the first item, if we choose to
if (SetRadioButtonDefaultToFirstItem)
{
if (((RadioButtonList)control).Items.Count > 0)
{
((RadioButtonList)control).Items[0].Selected = true;
}
}
}
//now call recursively
if (control.HasControls())
ClearControls(control, ClearLists, SetRadioButtonDefaultToFirstItem, SetDropDownDefaultToFirstItem);
}
}
NOTES:
There are no notes on this topic.