Thursday, September 16, 2010

Editable sitemap provider and Solve Space querystring bug

Imports System
Imports System.Data
Imports System.Configuration
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Web
Imports System.Web.Configuration
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Xml

'''
''' Extends the XmlSiteMapProvider class with edit functionality
''' Requires all siteMapNodes to have unique titles.
'''

Public Class EditableXmlSiteMapProvider
Inherits XmlSiteMapProvider

Public Sub New()
End Sub

Public Shadows Sub AddNode(ByVal parentTitle As String, ByVal title As String, ByVal url As String, ByVal roles As String)
Dim doc As XmlDocument = LoadXmlDoc()
Dim parent As XmlElement = FindNodeByTitle(doc, parentTitle)

Dim newChild As XmlElement = doc.CreateElement("siteMapNode")
newChild.SetAttribute("url", url)
' url must go in lower case to get xpath to work
newChild.SetAttribute("title", title)
' url must go in lower case to get xpath to work
newChild.SetAttribute("roles", roles)
parent.AppendChild(newChild)
SaveXmlDoc(doc)

End Sub

Public Sub UpdateNode(ByVal originalTitle As String, ByVal newParentTitle As String, ByVal title As String, ByVal url As String, ByVal roles As String)
Dim doc As XmlDocument = LoadXmlDoc()
Dim node As XmlElement = FindNodeByTitle(doc, originalTitle)

node.SetAttribute("url", url)
' url must go in lower case to get xpath to work
node.SetAttribute("title", title)
' url must go in lower case to get xpath to work
node.SetAttribute("roles", roles)

' check if the parent has changed
If node.ParentNode.Attributes("title").Value <> newParentTitle Then
node.ParentNode.RemoveChild(node)

' find the new parent
Dim newParent As XmlElement = FindNodeByTitle(doc, newParentTitle)
newParent.AppendChild(node)
End If
SaveXmlDoc(doc)
End Sub

Public Sub DeleteNode(ByVal title As String)
Dim doc As XmlDocument = LoadXmlDoc()
Dim node As XmlElement = FindNodeByTitle(doc, title)
node.ParentNode.RemoveChild(node)
SaveXmlDoc(doc)
End Sub

Private Function LoadXmlDoc() As XmlDocument
Dim doc As New XmlDocument()
doc.Load(HttpContext.Current.Server.MapPath(FilePath))
Return doc
End Function

Private Sub SaveXmlDoc(ByVal doc As XmlDocument)
Dim AbsPath As String = HttpContext.Current.Server.MapPath(FilePath)
Try
doc.Save(AbsPath)
Catch ex As UnauthorizedAccessException
Try
' try to remove 'read-only' attribute on the file.
'WebUtil.RemoveReadOnlyFileAttribute(AbsPath)
doc.Save(AbsPath)
Catch
' throw the original exception
Throw ex
End Try
End Try
MyBase.Clear()
MyBase.BuildSiteMap()
End Sub

Private Function FindNodeByTitle(ByVal doc As XmlDocument, ByVal title As String) As XmlElement
Dim xPath As String = [String].Format("//*[@title='{0}']", title)
Dim node As XmlElement = TryCast(doc.SelectSingleNode(xPath), XmlElement)
If node Is Nothing Then
Throw New Exception("Node not found with title: " & title)
End If
Return node
End Function

'''
''' The built in SiteMapNode.Url property gives a different value to the actual web.sitemap value,
''' it is a mapped value that changes depending on the v.dir of the running web site.
''' This method reads the xml attribute direct from the web.sitemap file.
'''

Public Function GetActualUrl(ByVal title As String) As String
Return Me.FindNodeByTitle(LoadXmlDoc(), title).Attributes("url").Value
End Function

Public Shared ReadOnly Property FilePath() As String
Get
' if anyone gets a nicer method to read the web siteMapFile attribute, please post it. i tried using 'System.Web.Configuration but i couldn't get it working. also, the site breaks when running off VS web server 'because of 'cannot read IIS metabase' errors.
Dim webConfigText As String = File.ReadAllText(HttpContext.Current.Server.MapPath("~/web.config"))
Dim m As Match = Regex.Match(webConfigText, "siteMapFile=""(.*?)""", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
If Not m.Success Then
Return "~/Web.sitemap"
Else
' default value. otherwise we could throw new Exception("web.config does not contain a siteMapFile element");
Return m.Groups(1).Captures(0).Value
End If
End Get
End Property

Public Overrides Function FindSiteMapNode(ByVal context As System.Web.HttpContext) As System.Web.SiteMapNode
Dim node As SiteMapNode = MyBase.FindSiteMapNode(context)

If node Is Nothing Then
If context Is Nothing Then
Return Nothing
End If

Dim queryString As String = CType(context.CurrentHandler, Page).ClientQueryString

Dim pageUrl As String = HttpUtility.UrlDecode(context.Request.Path & "?" & queryString)
node = MyBase.FindSiteMapNode(pageUrl)
End If

Return node
End Function
End Class

Saturday, September 11, 2010

refresh a treeview

http://forums.asp.net/p/1083582/1607976.aspx

changed the "EnableCaching" property of XmlDataSource1 to "false" and the treeview refreshes on button click.

XmlDataSource1.DataBind();  // Rebind datasource to xml file TreeView1.DataBind();       // Rebind tree to datasource 

Wednesday, September 1, 2010

How To Fix the: “Validation of viewstate MAC failed” Error (ASP.NET MVC)

Backup from


http://adam.kahtava.com/journal/2009/11/23/how-to-fix-the-validation-of-viewstate-mac-failed-error-aspnet-mvc/


The “Validation of viewstate MAC failed” error only occurred when a page contained an HTML form element that made use of MVC’s AntiForgeryToken. The quick fix was to delete my __RequestVerificationToken cookie, but the error would rear its ugly head the minute I touched my assemblies. The long term solution was to add a machineKey element to my Web.config file - asking visitors to delete a specific cookies when visiting my site was not a viable option.

How I fixed the “Validation of viewstate MAC failed” error on Shared Hosting:

  1. I used the Generator Tool to generate a machine key
  2. I added the machineKey element to my Web.config file

My Web.config now looks similar to this:




Anyhow, I hope this post helps anyone else that’s encountering this error.

Saturday, August 21, 2010

URL Rewriting with Session problem

URL Rewriting with Session problem

Using URL Rewriter with session

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the \\ section in the application configuration.




A solution that worked perfectly! Add the following to web.config:









Solution provided by Curt
http://forums.asp.net/t/1499512.aspx

Tuesday, July 13, 2010

simple detect iphone browser

script
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
if (document.cookie.indexOf("iphone_redirect=false") == -1) window.location = "/mobile/";
}
/script

Tuesday, June 8, 2010

Synology sometime does not recognize some NTFS External HDD

According to http://59.124.41.244/enu/viewtopic.php?f=12&p=78410&sid=e6ee050cf8f8c732278721360f16bfb5

It is because of the cluster size,

I finally used Acronis Disk Director Suite to format it,

then it WORKS!!!

Monday, April 19, 2010

black berry chinese

How do I upgrade the OS on my device? Follow these instructions:

1. Download and install to PC the latest version of Desktop Manager from here: ( BlackBerry Operating System Downloads )

2. Download the new OS you want and install it to the PC

3. Open Desktop Manager and do a complete backup of your current device.

4. Delete the vendor.xml file located in c:\program files\common files\research in motion\apploader

5. Plug in BB and open Desktop Manager

6. Select Application Loader

7. You will be prompted to upgrade




http://www.blackberrycool.com/2010/01/12/how-to-upgrade-your-blackberry-os-software-2010-version/

http://www.blackberryfaq.com/index.php/How_do_I_upgrade_the_OS_on_my_BlackBerry%3F

***** Remember to turn pc internet off and delete vendor.xml

Sunday, April 11, 2010

traditional chinese problem with extplorer

http://chs-yoga.com/fenix/component/content/article/7-extensions/41-extplorer-cht

apply to 2.01

extplorer is file manager written by php

SQL Server remote connecting problem

Issue: can't connect to sql server,

fix:
open the port http://support.microsoft.com/kb/968872
or http://support.microsoft.com/kb/914277
enable the remote connection in the properties using management studio
enable tcp/ip in configure manager

to add user
use management studio -> security

Monday, March 1, 2010

SQL Server error

Connections to SQL Server Files (*.mdf) require SQL Server Express 2005 to function properly. Please verify the installation of the component or download from the URl: http:go.microsoft.com/fwlink/?linkID=49251.

1. Open the “Add or Remove Programs” from the control panel.

2. Select “Microsoft SQL Server 2005” or “…ServerExperss 2005” and click on the Change button

3. On the “Microsoft SQL Server 2005 Maintenance” window you will see the

SQL Server 2005 instances:

: Database Engine

4. Copy the of your database engine.

5. Cancel and get out of the “Microsoft SQL Server 2005” or “…ServerExperss 2005”

6. Close the “Add or Remove Programs” window.

Open Microsoft Visual Studio

1. Select Tools-Options-Database Tools-Data Connections

2. Enter the copied earlier into the “SQL Server Instance Name” field.

3. Click OK


OR I actually download the sql server 2005 sp3 from the website provided and install it, then ok.

After that, error still occurs:
Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance

Delete C:\Documents and Settings\USERNAME\Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS.