Hi @Lan Huang-MSFT ,
My HTML
<table class="col-sm-10" border="0" cellpadding="0" cellspacing="0" style="width: 100%; margin: 0 auto; padding: 10px;">
<tr>
<td>Title:
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtTitle" runat="server" CssClass="form-control" />
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="BtnUpload_Click" />
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Body:
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtHTMLContent" runat="server" TextMode="MultiLine" />
<script>
tinymce.init({
selector: 'textarea#txtHTMLContent',
plugins: 'advlist link image lists',
plugins: 'emoticons',
toolbar: "emoticons",
block_unsupported_drop: false,
plugins: [
"advlist autolink lists link image charmap print preview hr anchor pagebreak",
"searchreplace wordcount visualblocks visualchars code fullscreen",
"insertdatetime media nonbreaking save table contextmenu directionality",
"emoticons template paste textcolor colorpicker textpattern"
],
toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
image_advtab: true,
file_picker_callback: function (callback, value, meta) {
if (meta.filetype == 'image') {
$('#FileUpload1').trigger('click');
$('#FileUpload1').on('change', function () {
var file = this.files[0];
var reader = new FileReader();
reader.onload = function (e) {
callback(e.target.result, {
alt: ''
});
};
reader.readAsDataURL(file);
});
}
},
});
// Prevent Bootstrap dialog from blocking focusin
document.addEventListener('focusin', (e) => {
if (e.target.closest(".tox-tinymce, .tox-tinymce-aux, .moxman-window, .tam-assetmanager-root") !== null) {
e.stopImmediatePropagation();
}
});
</script>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<asp:Button ID="SubmitBtn" Text="Post Article" CssClass="btn btn-primary" runat="server" OnClick="SubmitBtn_Click" />
</td>
</tr>
</table>
my C#
protected void SubmitBtn_Click(object sender, EventArgs e)
{
try
{
if (!string.IsNullOrEmpty(txtTitle.Text) & !string.IsNullOrEmpty(txtHTMLContent.Text))
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string query = "INSERT INTO MyArticles (Title, Body, DatePosted) VALUES (@Title, @Body, @DatePosted)";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@Title", txtTitle.Text);
cmd.Parameters.AddWithValue("@Body", txtHTMLContent.Text);
cmd.Parameters.AddWithValue("@DatePosted", DateTime.UtcNow);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txtHTMLContent.Text = "";
}
}
}
else
{
string message = "alert('Empty Article fields')";
ScriptManager.RegisterClientScriptBlock((sender as Control), this.GetType(), "alert", message, true);
}
}
catch (SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
}
protected void BtnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string FilePath = "imgarticle/" + FileName;
FileUpload1.SaveAs(Server.MapPath(FilePath));
txtHTMLContent.Text += string.Format("<img src = '{0}' alt = '{1}' />", FilePath, FileName);
}
}
When displaying
<div class="col-sm-3 col-md-3" style="width: 100%;">
<nav class="sidebar" style="font-family: 'Graphik', sans-serif;">
<ul class="list-unstyled" style="padding: 10px; margin: 0 auto; margin-top: 1%;">
<li style="margin: 0 7px;">
<asp:Label ID="contenttopics" runat="server" Font-Size="10pt" Text="Contents in this Article"></asp:Label>
</li>
</ul>
<ul class="sidebar-nav" style="padding: 10px; margin: 0 auto; margin-top: 1%;">
<li class="nav-item list-unstyled" id="Navigate" style="margin: 0 7px;">
<asp:Repeater ID="rptPages" runat="server">
<ItemTemplate>
<asp:HyperLink ID="HyperLink2" NavigateUrl='<%# string.Format("/article/{0}/{1}", Eval("ArticleId"), Eval("Slug")) %>' Text='<%# Eval("Title") %>' runat="server" />
</ItemTemplate>
<SeparatorTemplate>
<br />
</SeparatorTemplate>
</asp:Repeater>
</li>
</ul>
<br />
</nav>
</div>
<div class="col-sm-12" style="width: 100%; text-align: start;">
<div class="contenthead" style="width: 100%; margin: 0 auto; color: #011b33; margin-top: 2%;">
<asp:Label ID="TitleLabel" runat="server" Text="Title" />
</div>
<div class="contentbody" style="width: 100%; margin: 0 auto; color: #011b33; margin-top: 0%;">
<asp:Label ID="lblTitle" runat="server" Text="" />
<asp:Label ID="timelbl" runat="server" Font-Size="8pt"></asp:Label>
<hr />
<asp:Label ID="lblBody" runat="server" Font-Size="10pt" />
</div>
</div>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (this.Page.RouteData.Values.Count > 0)
{
string ArticleId = this.Page.RouteData.Values["ArticleId"].ToString();
string ArticleTitle = this.Page.RouteData.Values["Slug"].ToString();
this.PopulateArticle(ArticleId);
}
this.PopulateArticleID();
}
}
private void PopulateArticleID()
{
try
{
string query = "SELECT [ArticleId], [Title], REPLACE([Title], ' ', '-') [SLUG], [Body] FROM [MyArticles]";
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
rptPages.DataSource = dt;
rptPages.DataBind();
}
con.Close();
}
}
}
}
catch (SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
}
private void PopulateArticle(string articleId)
{
try
{
string ArticleId = this.Page.RouteData.Values["ArticleId"].ToString();
if (ArticleId != null)
{
string query = "SELECT [ArticleId], [Title], [Body], [DatePosted] FROM [MyArticles] WHERE [ArticleId] = @ArticleId";
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Parameters.AddWithValue("@ArticleId", ArticleId);
cmd.Connection = con;
sda.SelectCommand = cmd;
con.Open();
using (SqlDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
lblBody.Text = (read["Body"].ToString());
TitleLabel.Text = (read["Title"].ToString());
timelbl.Text = CalCulateTime(Convert.ToDateTime(read["DatePosted"].ToString()));
}
}
con.Close();
}
}
}
}
else
{
}
}
catch (SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
}
public string CalCulateTime(DateTime postDate)
{
string message = "";
DateTime currentDate = DateTime.Now;
TimeSpan timegap = currentDate - postDate;
message = string.Concat("Posted on ", postDate.ToString("MMMM dd, yyyy"), " at ", postDate.ToString("hh:mm tt"));
if (timegap.Days > 365)
{
message = string.Concat("Posted ", (((timegap.Days) / 30) / 12), " year(s) ago");
}
else if (timegap.Days > 31)
{
message = string.Concat("Posted ", ((timegap.Days) / 30), " month(s) ago");
}
else if (timegap.Days > 1)
{
message = string.Concat("Posted ", timegap.Days, " day(s) ago");
}
else if (timegap.Days == 1)
{
message = string.Concat("Posted ", timegap.Days, " day(s) ago");
}
else if (timegap.Hours >= 2)
{
message = string.Concat("Posted ", timegap.Hours, " hour(s) ago");
}
else if (timegap.Hours >= 1)
{
message = string.Concat("Posted ", timegap.Hours, " hour(s) ago");
}
else if (timegap.Minutes >= 60)
{
message = string.Concat("Posted ", timegap.Minutes, " minutes(s) ago");
}
else if (timegap.Minutes >= 5)
{
message = string.Concat("Posted ", timegap.Minutes, " minute(s) ago");
}
else if (timegap.Minutes >= 1)
{
message = string.Concat("Posted ", timegap.Minutes, " minute(s) ago");
}
else if (timegap.Seconds >= 1)
{
message = string.Concat("Posted ", timegap.Seconds, " second(s) ago");
}
else
{
message = "Posted less than a second ago";
}
return message;
}
Then in my Global.asax file, I have this routing
routes.MapPageRoute("articles/article", "article/{ArticleId}/{Slug}", "~/articles/article.aspx");