URL mapping/rewriting

Here is something to check out.

http://www.urlrewriting.net/en/Default.aspx

I may try to incorporate this into my CMS.

Posted in Random | Tagged | Leave a comment

Using multiple sitemaps in an application

I wanted to have more than one .sitemap file in my aplication.  The first one is for the navigation of the the user portion of the cms.  The second one would be in a subfolder and be used for the admin part of the site. 

 The first one for the user part of the site is the default one called web.sitemap.  Then I created a second one called admin.sitemap in the secure folder of my website where all the other admin pages reside. 

Then in the <system.web> section of the web.config file I registered the sitemaps in the following way:

<siteMap defaultProvider=AspNetXmlSiteMapProvider><providers>< add name=AdminXmlSiteMapProvider type=System.Web.XmlSiteMapProvider siteMapFile=“~/Secure/admin.sitemap /></providers></siteMap>First it gives the default provider which has to be named “AspNetXmlSiteMapProvider”.  Then I added the second provider, giving it whatever name I choose.  Then siteMapFile shows the path to the file. 

Next I used a SiteMapDataSource control to get the data to my Treeview control.  The SiteMapDataSource control is configured in the following way:

<asp:SiteMapDataSource ID=”SiteMapDataSource1″ runat=”server” SiteMapProvider=”AdminXmlSiteMapProvider”/>Thanks to DMan100 from this thread : http://forums.asp.net/t/1010684.aspx

Posted in .aspx | Tagged , , , , | Leave a comment

Pre-Init handling

Here are some links from OdetoCode.com that talk about the  pre_Init event. 

http://odetocode.com/Blogs/scott/archive/2005/12/09/2604.aspx

http://www.odetocode.com/articles/450.aspx

I was looking for a way for the administrators of my cms to choose a template for their website.  I created a class and put it in the App_Code Folder and used the the example code for using the HttpModule.  It worked beautifully.  Here is the code I used to choose the template.  It the template names  are stored in an SQL Server database. Eventually the administrator will be able to add new templates to the cms.   

Dim value As StringDim connStr As String = ConfigurationManager.ConnectionStrings(“TobetConnectionString”).ConnectionStringDim myConn As New SqlConnection(connStr)Dim strQuery As String = “select top 1 [ID], [Template] From [SiteConfig]”Dim myCommand As New SqlCommand(strQuery, myConn)myConn.Open()Dim myReader As SqlDataReader = myCommand.ExecuteReader()myReader.Read()value = myReader.GetString(1)myReader.Close()

myConn.Close()

Return valueEnd Function Now, however, I am working on the administrative part of the web site, and I want a different master page for my admin pages.  Since the admin is still part of the same application, I am going to try to create a BasePage class that all of the user pages of my application can use, but the admin pages will have the master page hard coded and not use the BasePage class. 

The way I did it is to take the code out of the web.config for the HttpModule, and create the following Base Page Class in the App_Code Directory.

Imports Microsoft.VisualBasic

Imports System

Imports System.Data

Imports System.Collections

Imports System.Configuration

Imports System.Web

Imports System.Web.Security

Imports System.Web.UI

Imports System.Web.UI.Adapters

Imports System.Web.UI.WebControls

Imports System.Web.UI.WebControls.WebParts

Imports System.Web.UI.HtmlControls

Imports System.Data.SqlClient

Public Class BasePageInherits Page

Public Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreInitDim template As String

Dim connStr As String = ConfigurationManager.ConnectionStrings(“TobetConnectionString”).ConnectionStringDim myConn As New SqlConnection(connStr)Dim strQuery As String = “select top 1 [ID], [Template] From [SiteConfig]”

Dim myCommand As New SqlCommand(strQuery, myConn)myConn.Open()

Dim myReader As SqlDataReader = myCommand.ExecuteReader()myReader.Read()

template = myReader.GetString(1)

myReader.Close()

myConn.Close()

Me.MasterPageFile = template & “.master”

Me.Theme = templateEnd SubEnd Class

Then in all the pages that I need to use this master page and theme I changed the codebehind file to inherit from BasePage instead of System.Web.UI.Page. 

Posted in .aspx, Code Behind, SQL | Tagged , , , , , | Leave a comment

Terms of Use checkbox validation in a gridview

I was trying to validate that a checkbox was checked using a custom code validator in a gridview.  I kept getting the error:

 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

This was the original code which was the same as the code I used when validating a checkbox on a formview:

Public Sub ValidateTerms(ByVal sender As System.Object, ByVal args As ServerValidateEventArgs)

If CType(Me.GridView1.FindControl(“TermsOfUse”), CheckBox).Checked Then

args.IsValid = True

Else : args.IsValid = False

End If

End Sub

I found this thread on forums.asp.net that helped me come up with the following code that worked:

Public Sub ValidateTerms(ByVal sender As System.Object, ByVal args As ServerValidateEventArgs)

Dim cv As CustomValidator = sender

Dim row As GridViewRow = cv.NamingContainer

Dim chkBox As CheckBox = row.FindControl(“TermsOfUse”)

If (chkBox.Checked) Then

args.IsValid = True

Else : args.IsValid = False

End If

End Sub

Posted in .aspx, Code Behind, Controls | Tagged , , , , , | Leave a comment

Formatting a date declaratively

I have a label bound to a DateTime field in my SQL Server Database.  When I enter the data into the field I only enter in the date in mm/dd/yyyy format.  However, since it is a DateTime field, SQL Server automatically adds the time.  Therefore when it shows on my site it shows the date and the time like this :

 Event Date: 03/13/2008 12:00:00PM

I just wanted to show the date so instead of setting 

Text=’<%# Eval(“EventDate”) %>

which give the above results,  I did the following:

Text=’<%# DataBinder.Eval(Container.DataItem,”EventDate”, “{0:MM/dd/yyyy}” ) %>

This gave me the results I was looking for where

Event Date: 03/13/2008

Posted in .aspx | Tagged , , , | Leave a comment

Return the value of the ID field after updating database

Serveral times I have done an insert or update on the database and then needed to know the automatically generated id of the record.  One of the reasons I need it was to do a Response.Redirect to the a page that displays the data that I just inserted or edited.  Another reason is after inserting a record, I needed to update the url field of the same record where the url contains that automatically generated ID.  This is how I am setting up my content management system that I am working on for a non profit organization.  I found a great article that tells what needs to be done to return a value that was just inserted.  Here is the article.

 http://www.mikesdotnetting.com/Article.aspx?ArticleID=54

I was using a FormView control with a SQLDataSource.   

The table I am inserting into has the following fields

  ID

GroupName

Email

Website

Location

ContactPhone

LastUpdated

DateTime

Description

 

OriginalEmail

 

 The InsertCommand is as follows: “INSERT INTO [tblTalks] ([GroupName], [Email], [Location], [ContactPhone], [LastUpdated], [DateTime], [Description], [OriginalEmail]) Values (@GroupName, @Email,  @Location, @ContactPhone, GetDate(), @DateTime, @Description, @Email)” 

The ID field auto increments when the record is added.   

What I needed to do was get the ID of the record that was just added so I could update the website field to be similar to this:  http://www.mysite.org/talks.aspx?p= ” & ID 

What I ended up doing was adding ;SET @NewId = Scope_Identity()to the end of my INSERTCOMMAND. 

Then I added another Parameter to my INSERT PARAMETERS collection that has the DIRECTION set to OUTPUT like this:

<asp:Parameter Direction=”Output” Name=”NewId” Type=”Int32″ />  

Now we want to do something with the output parameter that we just created.  This must be done in the Inserted event of the sqldatasource.  So I add the following code in my codebehind. 

Protected Sub SqlDataSource1_Inserted(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Inserted

     Dim newid As Integer = e.Command.Parameters(“@NewId”).Value

     Response.Redirect( “~/StoryBook.aspx?mode=3&ID=” & newid.ToString())

End Sub

In this example I just used the output to redirect to a page that would show the data that was just entered.   I could have used the output to update the record I just inserted or any number of other things.  Thanks to Mike at mikesdotnetting for the article above.  In his article he shows how to to this from codebehind as well using both C# and VB.NET, as well as how to do it in Access. 

Posted in Code Behind, SQL | Tagged , , , , , | Leave a comment

Using querystring parameters

This is a simple one but I just want to put it here to remind myself.  If url is as following:

http://www.mysite.org/page.aspx?id=1

 Then you must have a querystring parameter for your datasource.  In this case it is for the select statement.

<SelectParameters>

<asp:QueryStringParameterName=”ID”QueryStringField=”ID”/>

</SelectParameters>

Then you select statement is as follows. SelectCommand=”Select [Field1],[Field2] From [table] Where [idfield] = @ID”

Posted in .aspx, SQL | Tagged , , , , | Leave a comment

Dynamically set Hyperlink NavigateUrl property within a Gridview

I wanted to set my NavigateUrl Property to go to a different page and pass a querystring variable called ID that was retrieved from the database.  This is the solution

<asp:HyperLink ID=”LinkStory” runat=”server” NavigateUrl=’<%# Eval(“StoryID”, “StoryBook.aspx?mode=3&ID={0}”) %>

Text=’<%# Eval(“Title”) %> Width=”218px”></asp:HyperLink>

I found this on the codenewsgroups forum. here is the link:

http://www.codenewsgroups.net/group/microsoft.public.dotnet.framework.aspnet.webcontrols/topic9795.aspx

Posted in Controls | Tagged , , , , , | 5 Comments

Change to Edit or New mode of formview from a link on another page

I have a  page called contents.aspx that contains a Hyperlink Control to add a new record and a gridview that is set up to be a table of contents.  The Hyperlink control needs to open a formview on a different .aspx page in New mode.  To do this I pass a variable in the URL to the page Called story.aspx with the formview and handle it in the Code Behind.   First I set NavigateURL property of the Hyperlink Control on contents contents.aspx like this: 

NavigateUrl=”~/StoryBook.aspx?mode=0″>Add My Story</asp:HyperLink> 

Then in the codebehind of the of story.aspx I put the following code in the Page Load Event:

If mode = “0” Then

FormView1.ChangeMode(FormViewMode.Insert)

ElseIf mode = “1” Then

FormView1.ChangeMode(FormViewMode.Edit)

End If

If I wanted the page to open in edit view, I would have set the NavigateURL property in contents.aspx to be NavigateUrl=”~/StoryBook.aspx?mode=1″>Edit My Story</asp:HyperLink>Here is the link to the site that let me to this conclusion:

http://www.crossedconnections.org/w/?p=70

Posted in Code Behind, Controls | Tagged , , , , , | 6 Comments

Referencing Controls within a Formview

Reference a control that is inside a formview you must use the FindControl method of the System.Web.UI.Control class.  Here is an example:

Dim idvar As String = CType(FormView1.FindControl(“HiddenStoryID”), HiddenField).Value 

Here I am setting the value of the variable idvar equal to the value of the Hidden Field named HiddenStoryID.  You must use the CType to make sure that it is converted correctly when using vb.net.  See MSDN for detailsThis only seems to work for some controls.  It does not seem to work for a hyperlink control.  I am working on another solution for getting the value from the hyperlink in a GridView.  Right now the most likely seems to be to make it a templated field.  I will post again when I find the answer. 

Posted in Code Behind, Controls | Leave a comment