Html 如何更改div的内部html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5703419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:04:45  来源:igfitidea点击:

how to change the inner html of a div

asp.nethtmlhtml-editor

提问by Wahtever

i have a divand an html editor and trying to:

我有一个div和一个 html 编辑器,并试图:

  1. add the div inner html to the editor. (thats done)
  2. then any changes done in the editor is saved back in the div.
  1. 将 div 内部 html 添加到编辑器。(大功告成)
  2. 然后在编辑器中所做的任何更改都会保存回 div 中。

here is the backcode :

这是回溯代码:

protected void Page_Load(object sender, EventArgs e) 
{
    Editor.Content = cvDiv.InnerHtml;
}

protected void preview(object sender, EventArgs e) //this is an onclick event
{
    cvDiv.InnerHtml = Editor.Content;
}

and the code for the editor:

和编辑器的代码:

<asp:ScriptManager runat="server" />
<cc1:Editor ID="Editor" runat="server" OnContentChanged="preview"  />
<asp:Button runat="server" ID="eButton" CssClass="eButton" Text="Edit" OnClick="Edit" />// this is the button that is supposed to save

but it doesnt work.

但它不起作用。

so what i am trying to do is save whatever changes made in the editor to the div

所以我要做的是将编辑器中所做的任何更改保存到 div

using asp.net 3.5, and the ajax toolkit editor.

使用 asp.net 3.5 和 ajax 工具包编辑器。

thanks in advance.

提前致谢。

回答by 2GDev

This is an example i've made to show you a working code.

这是我为向您展示工作代码而制作的示例。

ASPX

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
        <asp:TextBox id="editor" runat="server" AutoPostBack="true"></asp:TextBox>
        <asp:Button ID="Save" runat="server" OnClick="SaveChange" Text="Save" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
            <ContentTemplate>
                <div id="preview" runat="server"></div>    
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="editor" EventName="TextChanged" />
            </Triggers>
        </asp:UpdatePanel>
    </form>
</body>
</html>

ASPX.CS

ASPX文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack) editor.Text = preview.InnerHtml;
        else preview.InnerHtml = editor.Text;

    }

    protected void SaveChange(object sender, EventArgs e)
    {
        string thingsToSave = preview.InnerHtml;
        //Save to db/file/xml   

    }

}

回答by Bala R

Can you try using Literalcontrol rather than a server side <div />?

您可以尝试使用Literal控件而不是服务器端<div />吗?