Et site med kursusmaterialer, der bruges i forbindelse med undervisning i forskellige it relaterede emnerEn skøn grafik du kan nyde mens der undervises.
   

Send spørgsmål eller kommentar til Thor - klik her: Send en kommentar til Thor - omkring undervisning eller kursus  Søg i kursusmaterialerne. Kan bruges i forbindelse med undervisningen eller efter kurset  Til forsiden af kursusmaterialer.dk 

 

ASP.NET II / Et lille CMS / Styr sider


ASP.NET fjernundervisningskursus

- Vil du gerne på kursus NU?

- Vil du gerne lære i dit eget tempo?

- Har du ikke tid til at tage på kursus ude i byen?

Så køb et kursus i ASP.NET som fjernundervisning på www.it-fjernundervisning.dk


Styr indholdet

Det er en større sag...

Først HTML koden:

<body>
  <form id="Form1" method="post" runat="server">
   <table border="1">
    <tr>
     <td>&nbsp;</td>
     <td>Emne</td>
     <td>&nbsp;</td>
     <td>Sider</td>
     <td>&nbsp;</td>
    </tr>
    <tr>
     <td vAlign="top">
      <P><asp:imagebutton id="EmneOp" runat="server" ImageUrl="../Billeder/op.gif"></asp:imagebutton></P>
      <P><asp:imagebutton id="EmneNed" runat="server" ImageUrl="../Billeder/ned.gif"></asp:imagebutton></P>
     </td>
     <td vAlign="top"><asp:radiobuttonlist id="rblEmne" runat="server" AutoPostBack="True"></asp:radiobuttonlist></td>
     <td vAlign="top">
      <P><asp:imagebutton id="SideOp" runat="server" ImageUrl="../Billeder/op.gif"></asp:imagebutton></P>
      <P><asp:imagebutton id="SideNed" runat="server" ImageUrl="../Billeder/ned.gif"></asp:imagebutton></P>
     </td>
     <td vAlign="top"><asp:radiobuttonlist id="rblSide" runat="server"></asp:radiobuttonlist></td>
     <td>
      <P><asp:linkbutton id="cmdRetSide" runat="server">Ret sidens indhold</asp:linkbutton><BR>
       <asp:linkbutton id="cmdRetSideTitel" runat="server">Ret sidens titel</asp:linkbutton><BR>
       <asp:linkbutton id="cmdRetEmneTitel" runat="server">Ret emnets titel</asp:linkbutton><BR>
       <asp:linkbutton id="cmdVisSide" runat="server">Vis siden</asp:linkbutton></P>
      <P><asp:linkbutton id="cmdSletSide" runat="server">Slet side</asp:linkbutton><BR>
       <asp:linkbutton id="cmdSletEmne" runat="server">Slet emne</asp:linkbutton></P>
     </td>
    </tr>
    <tr>
     <td>&nbsp;</td>
     <td>&nbsp;
      <asp:button id="cmdNytEmne" runat="server" Text="Nyt emne"></asp:button></td>
     <td>&nbsp;</td>
     <td>&nbsp;
      <asp:button id="cmdNySide" runat="server" Text="Ny side"></asp:button></td>
     <td>&nbsp;</td>
    </tr>
   </table>
   <P><asp:label id="lblMessage" runat="server"></asp:label></P>
   <P><asp:label id="lblTitel1" runat="server" Visible="False">Titel1</asp:label>&nbsp;
    <asp:textbox id="txtTitel1" runat="server" Visible="False"></asp:textbox>&nbsp;
    <asp:label id="lblTitel2" runat="server" Visible="False">Titel2</asp:label>&nbsp;
    <asp:textbox id="txtTitel2" runat="server" Visible="False"></asp:textbox></P>
   <P><asp:button id="cmdOpret" runat="server" Text="Opret" Visible="False"></asp:button></P>
  </form>
 </body>

Og dernæst den bagvedliggende kode:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmdSletEmne.Attributes("onclick") = "javascript:return confirm('Er du sikker på at du vil slette emnet - alle underliggende sider vil også blive slettet?')"
        cmdSletSide.Attributes("onclick") = "javascript:return confirm('Er du sikker på at du vil slette siden?')"
        If Not Page.IsPostBack Then
            Dim objXMLDoc As New XmlDocument()
            objXMLDoc.Load(Server.MapPath("..\db\cmsdemo\website.xml"))
            Dim objEmne, objSide As XmlNode
            Dim dtEmner, dtSider As DataTable
            Dim dsIndhold As New DataSet
            dtEmner = dsIndhold.Tables.Add("Emner")
            dtSider = dsIndhold.Tables.Add("Sider")
            dtEmner.Columns.Add("ID", System.Type.GetType("System.Int32"))
            dtEmner.Columns.Add("EmneTitel", System.Type.GetType("System.String"))
            dtSider.Columns.Add("ID", System.Type.GetType("System.Int32"))
            dtSider.Columns.Add("EmneID", System.Type.GetType("System.Int32"))
            dtSider.Columns.Add("SideTitel", System.Type.GetType("System.String"))
            Dim objDataRow As DataRow
            Dim iEmner, iSider As Integer
            iEmner = 1
            iSider = 1
            For Each objEmne In objXMLDoc.GetElementsByTagName("Emne")
                objDataRow = dtEmner.NewRow()
                objDataRow("ID") = iEmner
                objDataRow("EmneTitel") = objEmne.Attributes.ItemOf("Titel").Value
                dtEmner.Rows.Add(objDataRow)
                For Each objSide In objEmne.ChildNodes
                    objDataRow = dtSider.NewRow()
                    objDataRow("ID") = iSider
                    objDataRow("EmneID") = iEmner
                    objDataRow("SideTitel") = objSide.Attributes.ItemOf("Titel").Value
                    dtSider.Rows.Add(objDataRow)
                    iSider += 1
                Next
                iEmner += 1
            Next
            ViewState("Indhold") = dsIndhold
            doDataBind()
        End If
        lblMessage.Text = ""
    End Sub

 
   Sub doDataBind()
        Dim dsIndhold As New DataSet
        dsIndhold = ViewState("Indhold")
        Dim objDataView As New DataView(dsIndhold.Tables("Emner"))
        rblEmne.DataSource = objDataView
        rblEmne.DataTextField = "EmneTitel"
        rblEmne.DataValueField = "ID"
        rblEmne.DataBind()
        rblEmne.SelectedIndex = Session("cmsEmneIndex")
        objDataView = dsIndhold.Tables("Sider").DefaultView
        objDataView.RowFilter = "EmneID = " + CStr(Session("cmsEmneNr"))
        rblSide.DataSource = objDataView
        rblSide.DataTextField = "SideTitel"
        rblSide.DataValueField = "ID"
        rblSide.DataBind()
        rblSide.SelectedIndex = Session("cmsSideIndex")
    End Sub
 
   Private Sub rblEmne_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rblEmne.SelectedIndexChanged
        Session("cmsEmneIndex") = rblEmne.SelectedIndex
        Session("cmsEmneNr") = rblEmne.SelectedItem.Value
        Session("cmsSideIndex") = 0
        doDataBind()
    End Sub
 
   Private Sub cmdNytEmne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNytEmne.Click
        lblTitel1.Text = "Titel til omrde: "
        lblTitel1.Visible = True
        txtTitel1.Visible = True
        lblTitel2.Text = "Titel til side: "
        lblTitel2.Visible = True
        txtTitel2.Visible = True
        cmdOpret.Visible = True
        ViewState("Opret") = "Emne"
    End Sub
 
   Private Sub cmdNySide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNySide.Click
        lblTitel1.Text = "Titel til side: "
        lblTitel1.Visible = True
        txtTitel1.Visible = True
        cmdOpret.Visible = True
        viewState("Opret") = "Side"
    End Sub
  
  Private Sub cmdOpret_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpret.Click
        Dim strEmne, strSide As String
        strEmne = rblEmne.SelectedItem.Text
        strSide = rblSide.SelectedItem.Text
        Dim objXMLDoc As New XmlDocument
        Dim objNode, objNewNode, objSubNode, objSubSubNode, objStartNode As XmlNode
        Dim objAttr As XmlAttribute
        objXMLDoc.Load(Server.MapPath("..\db\cmsdemo\website.xml"))
        Dim strOpretType As String = viewstate("Opret")
        Select Case strOpretType
            Case "Side"
                'opret ny side
                objNode = objXMLDoc.SelectSingleNode("/Website/Emne[@Titel='" + strEmne + "']/Side[@Titel='" + strSide + "']")
                objStartNode = objXMLDoc.SelectSingleNode("/Website/Emne[@Titel='" + strEmne + "']")
                objNewNode = objXMLDoc.CreateElement("Side")
                objAttr = objXMLDoc.CreateAttribute("Titel")
                objAttr.Value = txtTitel1.Text
                objNewNode.Attributes.SetNamedItem(objAttr)
                objSubNode = objXMLDoc.CreateElement("Indhold")
                objNewNode.AppendChild(objSubNode)
                objStartNode.InsertAfter(objNewNode, objNode)
            Case "Emne"
                'opret nyt emne
                objNode = objXMLDoc.SelectSingleNode("/Website/Omraade[@Titel='" + strEmne + "']")
                objStartNode = objXMLDoc.SelectSingleNode("/Website")
                objNewNode = objXMLDoc.CreateElement("Emne")
                objAttr = objXMLDoc.CreateAttribute("Titel")
                objAttr.Value = txtTitel1.Text
                objNewNode.Attributes.SetNamedItem(objAttr)
                objSubNode = objXMLDoc.CreateElement("Side")
                objAttr = objXMLDoc.CreateAttribute("Titel")
                objAttr.Value = txtTitel2.Text
                objSubNode.Attributes.SetNamedItem(objAttr)
                objSubSubNode = objXMLDoc.CreateElement("Indhold")
                objSubNode.AppendChild(objSubSubNode)
                objNewNode.AppendChild(objSubNode)
                objStartNode.InsertAfter(objNewNode, objNode)
            Case "OpdaterSideTitel"
                objNode = objXMLDoc.SelectSingleNode("/Website/Emne[@Titel='" + strEmne + "']/Side[@Titel='" + strSide + "']")
                objNode.Attributes("Titel").InnerText = txtTitel1.Text
            Case "OpdaterEmneTitel"
                objNode = objXMLDoc.SelectSingleNode("/Website/Emne[@Titel='" + strEmne + "']")
                objNode.Attributes("Titel").InnerText = txtTitel1.Text
        End Select
        objXMLDoc.Save(Server.MapPath("..\db\cmsdemo\website.xml"))
        Response.Redirect("sideadmin.aspx")
    End Sub
 
   Private Sub cmdRetSide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRetSide.Click
        Dim strParam As String
        strParam = "Emne=" + Server.UrlEncode(rblEmne.SelectedItem.Text)
        strParam += "&Side=" + Server.UrlEncode(rblSide.SelectedItem.Text)
       Response.Redirect("retindhold.aspx?" + strParam)
    End Sub
  
  Private Sub cmdRetSideTitel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRetSideTitel.Click
        lblTitel1.Visible = True
        lblTitel1.Text = "Ret side titel"
        txtTitel1.Visible = True
        txtTitel1.Text = rblSide.SelectedItem.Text
        cmdOpret.Visible = True
        cmdOpret.Text = "Opdater"
        viewstate("Opret") = "OpdaterSideTitel"
    End Sub
 
   Private Sub cmdRetEmneTitel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRetEmneTitel.Click
        lblTitel1.Visible = True
        lblTitel1.Text = "Ret emne titel"
        txtTitel1.Visible = True
        txtTitel1.Text = rblEmne.SelectedItem.Text
        cmdOpret.Visible = True
        cmdOpret.Text = "Opdater"
        viewstate("Opret") = "OpdaterEmneTitel"
    End Sub
 
   Private Sub cmdSletSide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSletSide.Click
        If rblSide.Items.Count = 1 Then
            lblMessage.Text = "Du kan ikke slette den sidste side!<br/>Der SKAL være mindst én side til hvert emne"
            Exit Sub
        Else
            Dim objXMLDoc As New XmlDocument
            Dim objNode As XmlNode
            objXMLDoc.Load(Server.MapPath("..\db\cmsdemo\website.xml"))
            objNode = objXMLDoc.SelectSingleNode("/Website/Emne[@Titel='" + rblEmne.SelectedItem.Text.ToString + "']/Side[@Titel='" + rblSide.SelectedItem.Text.ToString + "']")
            objNode.ParentNode.RemoveChild(objNode)
            objXMLDoc.Save(Server.MapPath("..\db\cmsdemo\website.xml"))
            If rblSide.SelectedIndex + 1 = rblSide.Items.Count Then
                Session("cmsSideIndex") = rblSide.SelectedIndex - 1
            End If
            Response.Redirect("sideadmin.aspx")
        End If
    End Sub
 
   Private Sub cmdSletEmne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSletEmne.Click
        If rblEmne.Items.Count = 1 Then
            lblMessage.Text = "Du kan ikke slette det sidste emne!"
            Exit Sub
        Else
            Dim objXMLDoc As New XmlDocument
            Dim objNode As XmlNode
            objXMLDoc.Load(Server.MapPath("..\db\cmsdemo\website.xml"))
            objNode = objXMLDoc.SelectSingleNode("/Website/Emne[@Titel='" + rblEmne.SelectedItem.Text.ToString + "']")
            objNode.ParentNode.RemoveChild(objNode)
            objXMLDoc.Save(Server.MapPath("..\db\cmsdemo\website.xml"))
            If rblEmne.SelectedIndex + 1 = rblEmne.Items.Count Then
                Session("cmsEmneIndex") = rblEmne.SelectedIndex - 1
            End If
            Response.Redirect("sideadmin.aspx")
        End If
    End Sub
 
   Private Sub EmneOp_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles EmneOp.Click
        If rblEmne.SelectedIndex = 0 Then
            lblMessage.Text = "Du kan ikke flytte det første emne op"
            Exit Sub
        Else
            Dim objXMLDoc As New XmlDocument
            Dim objNode As XmlNode
            objXMLDoc.Load(Server.MapPath("..\db\cmsdemo\website.xml"))
            objNode = objXMLDoc.SelectSingleNode("/Website/Emne[@Titel='" + rblEmne.SelectedItem.Text.ToString + "']")
            objNode.ParentNode.InsertBefore(objNode, objNode.PreviousSibling)
            objXMLDoc.Save(Server.MapPath("..\db\cmsdemo\website.xml"))
            Session("cmsEmneIndex") = rblEmne.SelectedIndex - 1
            Session("cmsEmneNr") = rblEmne.SelectedItem.Value - 1
            Response.Redirect("sideadmin.aspx")
        End If
    End Sub
 
   Private Sub EmneNed_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles EmneNed.Click
        If rblEmne.SelectedIndex = rblEmne.Items.Count - 1 Then
            lblMessage.Text = "Du kan ikke flytte det første emne ned"
            Exit Sub
        Else
            Dim objXMLDoc As New XmlDocument
            Dim objNode As XmlNode
            objXMLDoc.Load(Server.MapPath("..\db\cmsdemo\website.xml"))
            objNode = objXMLDoc.SelectSingleNode("/Website/Emne[@Titel='" + rblEmne.SelectedItem.Text.ToString + "']")
            objNode.ParentNode.InsertAfter(objNode, objNode.NextSibling)
            objXMLDoc.Save(Server.MapPath("..\db\cmsdemo\website.xml"))
            Session("cmsEmneIndex") = rblEmne.SelectedIndex + 1
            Session("cmsEmneNr") = rblEmne.SelectedItem.Value + 1
            Response.Redirect("sideadmin.aspx")
        End If
    End Sub
 
   Private Sub SideOp_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles SideOp.Click
        If rblSide.SelectedIndex = 0 Then
            lblMessage.Text = "Du kan ikke flytte den første side op"
            Exit Sub
        Else
            Dim objXMLDoc As New XmlDocument
            Dim objNode As XmlNode
            objXMLDoc.Load(Server.MapPath("..\db\cmsdemo\website.xml"))
            objNode = objXMLDoc.SelectSingleNode("/Website/Emne[@Titel='" + rblEmne.SelectedItem.Text.ToString + "']/Side[@Titel='" + rblSide.SelectedItem.Text.ToString + "']")
            objNode.ParentNode.InsertBefore(objNode, objNode.PreviousSibling)
            objXMLDoc.Save(Server.MapPath("..\db\cmsdemo\website.xml"))
            Session("cmsSideIndex") = rblSide.SelectedIndex - 1
            Response.Redirect("sideadmin.aspx")
        End If
    End Sub

    Private Sub SideNed_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles SideNed.Click
        If rblSide.SelectedIndex = rblSide.Items.Count - 1 Then
            lblMessage.Text = "Du kan ikke flytte den første side ned"
            Exit Sub
        Else
            Dim objXMLDoc As New XmlDocument
            Dim objNode As XmlNode
            objXMLDoc.Load(Server.MapPath("..\db\cmsdemo\website.xml"))
            objNode = objXMLDoc.SelectSingleNode("/Website/Emne[@Titel='" + rblEmne.SelectedItem.Text.ToString + "']/Side[@Titel='" + rblSide.SelectedItem.Text.ToString + "']")
            objNode.ParentNode.InsertAfter(objNode, objNode.NextSibling)
            objXMLDoc.Save(Server.MapPath("..\db\cmsdemo\website.xml"))
            Session("cmsSideIndex") = rblSide.SelectedIndex + 1
            Response.Redirect("sideadmin.aspx")
        End If
    End Sub
    Private Sub cmdVisSide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdVisSide.Click
        Dim strParam As String
        strParam = "Emne=" + Server.UrlEncode(rblEmne.SelectedItem.Text)
        strParam += "&Side=" + Server.UrlEncode(rblSide.SelectedItem.Text)
        Response.Redirect("../visindhold.aspx?" + strParam)
    End Sub

 


   
Spacer Spacer Spacer Spacer