webForumDet fria alternativet

spara innehållet från editor

.NET

2 svar · 258 visningar · startad av maqe

Medlem sedan dec. 2003824 inlägg
Frågan#1

Har ett litet problem med att spara innehållet från min editor.

Såhär ser min kontroll ut:

<div id="divForm" runat="server">
<EC:EditorControl id="Editor1" runat="server" EditorText='<%#DataBinder.Eval(Container.DataItem, "head")%>' />
</div>

och såhär såg min funktion ut förut när jag använde mig av en textbox:

    '=======================================
    '## Uppdaterar vald nyhet i databasen ##

    Dim tbox As TextBox
    Dim EntryID, EntryHeader As String
    EntryID = myNewsDatalist.DataKeys(e.Item.ItemIndex)
    tbox = CType(e.Item.FindControl("header"), TextBox)
    EntryHeader = tbox.Text

    Dim objUpdateNews As myNewsClass = New myNewsClass()

    Try

      objUpdateNews.UpdateNewsEntry(EntryID, EntryHeader)
      objUpdateNews = Nothing
      myNewsDatalist.EditItemIndex = -1
      LoadData()

    Catch ex As Exception
      Throw (ex)
    End Try

Nu är frågan den hur jag ska göra för att kunna spara innehållet i min editor istället för ifrån min textbox?
Jag vet helt enkelt inte hur jag ska göra för att skicka med Editor1.EditorText som entryheader

Medlem sedan dec. 2003824 inlägg
#2

såhär har dom gjort i det kodexemplet jag utgår ifrån

lblResult.Text = Editor1.EditorText

men det fungerar inte för mig ... får felet att Editor1.EditorText is not a member of system.web.ui.webcontrol ... antar att jag ska lägga till detta innan page_load

Protected WithEvents Editor1 As System.Web.UI.UserControl

men jag får det inte att fungera endå

Medlem sedan dec. 2003824 inlägg
#3

postar lite mer kod och hoppas på lite hjälp

Editor.ascx:

<div id="EditorView" visible="true" runat="server">
	<div class="toolbar">
		<a href="javascript:void(0)" unselectable="on" onclick="callFormatting('Bold');"><img unselectable="on" src="../images/iconbold.gif" title="Fed"></a>
		<a href="javascript:void(0)" unselectable="on" onclick="callFormatting('Italic');">
			<img unselectable="on" src="../images/iconitalic.gif" title="Italic"></a> <a href="javascript:void(0)" unselectable="on" onclick="callFormatting('Underline');">
			<img unselectable="on" src="../images/iconunderline.gif" title="Understreg"></a>
		<a href="javascript:void(0)" unselectable="on" onclick="callFormatting('Cut');"><img unselectable="on" src="../images/iconcut.gif" title="Klip"></a>
		<a href="javascript:void(0)" unselectable="on" onclick="callFormatting('Copy');"><img unselectable="on" src="../images/iconcopy.gif" title="Kopier"></a>
		<a href="javascript:void(0)" unselectable="on" onclick="callFormatting('Paste');">
			<img unselectable="on" src="../images/iconpaste.gif" title="Indsæt"></a> <a href="javascript:void(0)" unselectable="on" onclick="callFormatting('InsertOrderedList');">
			<img unselectable="on" src="../images/iconnumberlist.gif" title="Opstilling med tal"></a>
		<a href="javascript:void(0)" unselectable="on" onclick="callFormatting('InsertUnorderedList');">
			<img unselectable="on" src="../images/iconbulletlist.gif" title="Opstilling med punkttegn"></a>
		<a href="javascript:void(0)" unselectable="on" onclick="callFormatting('Outdent');">
			<img unselectable="on" src="../images/iconoutdent.gif" title="Ryk ud"></a> <a href="javascript:void(0)" unselectable="on" onclick="callFormatting('Indent');">
			<img unselectable="on" src="../images/iconindent.gif" title="Indryk"></a> <a href="javascript:void(0)" unselectable="on" onclick="callFormatting('Undo');">
			<img unselectable="on" src="../images/iconundo.gif" title="Undo"></a> <a href="javascript:void(0)" unselectable="on" onclick="callFormatting('Redo');">
			<img unselectable="on" src="../images/iconredo.gif" title="Redo"></a> <a href="javascript:void(0)" unselectable="on" onclick="callFormatting('CreateLink');">
			<img unselectable="on" src="../images/iconlink.gif" title="Hyperlink"></a>
	</div>
	<div class="editor" id="oDiv" contenteditable="true" runat="server">
		<asp:Literal id="lblText2edit" runat="server" />
	</div>
	<asp:TextBox id="txtEditor" style="DISPLAY:true" runat="server" />
</div>

editor.ascx.vb:

Public MustInherit Class editor
  Inherits System.Web.UI.UserControl

  Protected WithEvents lblText2edit As System.Web.UI.WebControls.Literal
  Protected WithEvents txtEditor As System.Web.UI.WebControls.TextBox
  Protected WithEvents EditorView As System.Web.UI.HtmlControls.HtmlGenericControl
  Protected WithEvents oDiv As System.Web.UI.HtmlControls.HtmlGenericControl

  Public Property EditorText() As String
    Get
      Return txtEditor.Text
    End Get
    Set(ByVal Value As String)
      lblText2Edit.Text = Value
      txtEditor.Text = Value
    End Set
  End Property

  Dim strSubmitScript As String

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    ' Make clientside-script for formatting the text.
    ' On any change data must be copied from contenteditable-div to input-field.
    ' The scripts also focuses the contenteditable-div to make formatting possible and updates the input-field when done.
    ' RegisterClientScriptBlock is used to avoid multiple copies of the script and the editor control within a page.
    ' Find ClientId for the input-field and the div-field and make a clientside-script to do the copying
    Dim InputClientId As String = FindControl("txtEditor").ClientID.ToString
    Dim DivClientId As String = FindControl("oDiv").ClientID.ToString
    Dim strEditorScript As String
    strEditorScript = "<script language=""javascript"">"
    strEditorScript += "function setFocus(){document.getElementById('" & DivClientId & "').focus();};"
    strEditorScript += "function callFormatting(sFormatString){setFocus;document.execCommand(sFormatString);setFocus;};"
    strEditorScript += "function copyTxt(){document.getElementById('" & InputClientId & "').value=document.getElementById('" & DivClientId & "').innerHTML;}"
    strEditorScript += "</script" & ">"
    ' Register the script
    ' If already registered notify user that only one editor per page is allowed
    If (Not Page.IsClientScriptBlockRegistered("EditorScript")) Then
      Page.RegisterClientScriptBlock("EditorScript", strEditorScript)
      ' Ensure that any change is copied from contenteditable-div to input-field
      oDiv.Attributes.Add("onactivate", "copyTxt();")
      oDiv.Attributes.Add("ondeactivate", "copyTxt();")
    Else
      EditorView.Visible = False
    End If

  End Sub

End Class

överst i min viewnews.ascx har jag detta...

<%@ Register TagPrefix="EC" TagName="EditorControl" Src="editor.ascx" %>

och sedan ser det ut såhär:

<div id="divForm" runat="server">
<EC:EditorControl id="Editor1" runat="server" EditorText='<%#DataBinder.Eval(Container.DataItem, "head")%>' />
</div>
<br />
<asp:LinkButton Text="Avbryt" Tooltip="Avbryt!" CommandName="Cancel" Runat="server" ID="cancel" />
<asp:LinkButton Text="Uppdatera" Tooltip="Uppdatera nyheten!" CommandName="Update" Runat="server" ID="update" />

Näe jag sedan ska uppdatera min nyhet från viewnews.ascx.vb så ska den ju plocka värdet ifrån textboxen "txtEditor" som ligger i min editor.ascx och sätta den till entryheader.
såhär ser koden för att uppdatera i databasen ut...
viewnews.ascx.vb

    Dim tbox As TextBox
    Dim EntryID, EntryHeader As String
    EntryID = myNewsDatalist.DataKeys(e.Item.ItemIndex)
    tbox = CType(e.Item.FindControl("txtEditor"), TextBox)
    EntryHeader = tbox.Text

    Dim objUpdateNews As myNewsClass = New myNewsClass()

    Try

      objUpdateNews.UpdateNewsEntry(EntryID, EntryHeader)
      objUpdateNews = Nothing
      myNewsDatalist.EditItemIndex = -1
      LoadData()

    Catch ex As Exception
      Throw (ex)
    End Try

Förtillfället får jag detta meddelande när jag försöker spara
Objektreferensen har inte angetts till en instans av ett objekt.

264 ms totalt · 4 externa anrop · v20260731065814-full.6fe65c25
129 ms — deklarationer (db)
0 ms — hämta statistik (cache)
129 ms — hämta tråd, inlägg och bilagor (db)
132 ms — ändringar (db)