martedì 26 agosto 2008

WebPart e IIS

Per il corretto funzionamento delle WebPart, l’applicazione deve essere impostata con
authentication mode="Forms"
In questo modo, sulla prima pagina del sito dobbiamo gestire l’utente da caricare nei cookie.
Ad Esempio

Dim userData As String = "ApplicationSpecific data for this user."
Dim ticket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, _
"username", _
DateTime.Now, _
DateTime.Now.AddMinutes(30), _
True, _
userData, _
FormsAuthentication.FormsCookiePath)
' Encrypt the ticket.
Dim encTicket As String = FormsAuthentication.Encrypt(ticket)
' Create the cookie.
Response.Cookies.Add(New HttpCookie(FormsAuthentication.FormsCookieName, encTicket))


L’IIS 7 deve essere impostato in modo da avere abilitate:
Autenticazione Anonima: Abilitato
Autenticazione basata su Form: Abilitato
Autenticazione di base: Disabilitato
Rappresentazione ASP.NET: Abilitato

L’IIS 5 deve essere impostato in modo da avere abilitate:

Accesso Anonimo: abilitato, il nome utente (ad esempio): IUSR_ServerTest e password quella di default, comunque quelli di default entrambi.
Autenticazione di Base: Disabilitato
Autenticazione Integrata di Windows: Abilitato

Paging e Sorting di un GridView

Con questi metodi è possibile gestire il paging e il sorting di un GridView non collegato ad un DataSource.

Private Property GridViewSortDirection() As String
Get
Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection"))
End Get
Set(ByVal value As String)
ViewState("SortDirection") = value
End Set
End Property


Private Property GridViewSortExpression() As String
Get
Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression"))
End Get
Set(ByVal value As String)
ViewState("SortExpression") = value
End Set
End Property


Private Function GetSortDirection() As String
Select Case GridViewSortDirection
Case "ASC"
GridViewSortDirection = "DESC"
Case "DESC"
GridViewSortDirection = "ASC"
End Select
Return GridViewSortDirection
End Function


Protected Function SortDataTable(ByVal dataTable As DataTable, ByVal isPageIndexChanging As Boolean) As DataView
If Not dataTable Is Nothing Then
Dim dataView As New DataView(dataTable)
If GridViewSortExpression <> String.Empty Then
If isPageIndexChanging Then
dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)
Else
dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())
End If
End If
Return dataView
Else
Return New DataView()
End If
End Function

Nell'evento Paging viene passato al SortDataTable il datasource della griglia, che molto probabilmente sarà vuoto. E' conveniente salvare precedentemente il datasource nel viewState e poi passare tale campo al SortDataTable


Protected Sub gridAgenti_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gridAgenti.PageIndexChanging
Try
gridAgenti.DataSource = SortDataTable(gridAgenti.DataSource, True)
gridAgenti.PageIndex = e.NewPageIndex
gridAgenti.DataBind()
Catch ex As Exception
Session.Remove("Exception")
Session.Add("Exception", ex)
Response.Redirect("ErrorPage.aspx")
End Try
End Sub

Protected Sub gridAgenti_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles gridAgenti.Sorting
GridViewSortExpression = e.SortExpression
Dim pageIndex As Integer = gridAgenti.PageIndex
gridAgenti.DataSource = SortDataTable(gridAgenti.DataSource, False)
gridAgenti.DataBind()
gridAgenti.PageIndex = pageIndex
End Sub