C# 用数据库中的动态数据替换HTML页面中的静态数据
我的asp.net web应用程序有一个HTML代码,当我按下Submit按钮时,它调用一个函数来读取bd中的内容,并将其加载到带有特定模板的HTML页面中,我想知道如何使用C将原始HTML文件中的静态数据替换为mysql数据库中的动态数据。在我的代码中,我确实读取了输入文件并我确实创建了一个要写入的输出文件,如下所示:C# 用数据库中的动态数据替换HTML页面中的静态数据,c#,asp.net,html,mysql,C#,Asp.net,Html,Mysql,我的asp.net web应用程序有一个HTML代码,当我按下Submit按钮时,它调用一个函数来读取bd中的内容,并将其加载到带有特定模板的HTML页面中,我想知道如何使用C将原始HTML文件中的静态数据替换为mysql数据库中的动态数据。在我的代码中,我确实读取了输入文件并我确实创建了一个要写入的输出文件,如下所示: StreamReader sr = new StreamReader("filepath/inputcv.html"); StreamWriter sw = n
StreamReader sr = new StreamReader("filepath/inputcv.html");
StreamWriter sw = new StreamWriter("filepath/outputcv.html");
这是代码的一部分,我需要用数据库中的内容替换这一段的内容
<div class="sectionContent">
<p>@1</p>
</div>
可以将外部数据存储在文本文件、XML文件或数据库中,并将其用于动态更新HTML页面内容。需要澄清的第一个要求是:什么将作为动态数据更新的过滤器工作,换句话说,对于mySQL,代码的哪一部分将执行select语句的动态更新?如果该语句保持不变,则意味着基础数据正在更改,但是什么在控制数据库中的更改呢?页面更新的理想数据刷新率是多少?在进行实际代码之前,应首先澄清这一点
希望这会有所帮助。我最好的朋友,AB以下是我根据您提供的内容提出的建议。如果这还不够,请评论更多细节
string strHTMLPage = "";
string strNewHTMLPage = "";
int intStartIndex = 0;
int intEndIndex = 0;
string strNewDataToBeInserted = "Assumes you loaded this string with the
data you want inserted";
StreamReader sr = new StreamReader("filepath/inputcv.html");
strHTMLPage = sr.ReadToEnd();
sr.Close();
intStartIndex = strHTMLPage.IndexOf("<div class=\"sectionContent\">", 0) + 28;
intStartIndex = strHTMLPage.IndexOf("<p>", intStartIndex) + 3;
intEndIndex = strHTMLPage.IndexOf("</p>", intStartIndex);
strNewHTMLPage = strHTMLPage.Substring(0, intStartIndex);
strNewHTMLPage += strNewDataToBeInserted;
strNewHTMLPage += strHTMLPage.Substring(intEndIndex);
StreamWriter sw = new System.IO.StreamWriter("filepath/outputcv.html", false, Encoding.UTF8);
sw.Write(strNewHTMLPage);
sw.Close();
您是否将HTML文件的内容加载到要写回的字符串中?或者这是在活动的ASPX页面上?您是否编写了从数据库获取数据的部分?在字符串中插入数据准备好了吗?使用C+MVC,创建动态页面是一种自然的方式。这太容易了,我不明白你为什么不使用它?如果您不使用MVC,您仍然可以使用带有模型的独立Razor引擎并创建动态内容。例如
string strHTMLPage = "";
string strNewHTMLPage = "";
int intStartIndex = 0;
int intEndIndex = 0;
string strNewDataToBeInserted = "Assumes you loaded this string with the
data you want inserted";
StreamReader sr = new StreamReader("filepath/inputcv.html");
strHTMLPage = sr.ReadToEnd();
sr.Close();
intStartIndex = strHTMLPage.IndexOf("<div class=\"sectionContent\">", 0) + 28;
intStartIndex = strHTMLPage.IndexOf("<p>", intStartIndex) + 3;
intEndIndex = strHTMLPage.IndexOf("</p>", intStartIndex);
strNewHTMLPage = strHTMLPage.Substring(0, intStartIndex);
strNewHTMLPage += strNewDataToBeInserted;
strNewHTMLPage += strHTMLPage.Substring(intEndIndex);
StreamWriter sw = new System.IO.StreamWriter("filepath/outputcv.html", false, Encoding.UTF8);
sw.Write(strNewHTMLPage);
sw.Close();