gpt4 book ai didi

c# - 将mysql内容插入html?

转载 作者:行者123 更新时间:2023-11-29 09:11:36 25 4
gpt4 key购买 nike

嗨,我正在尝试找出一种将 mysql 内容写入 html 代码的方法:

我的 asp 页面上有一个名为 <div id="test1"></div> 的区域(该区域是静态的)

我想从我的表中获取内容,并将代码放置在该 div 内的不同 div 名称下 (div id=sqlcontent)

所以它看起来像这样:

<div id="test1">
<div id="mysqlcontent">something the user would write which is stored in my db</div>
</div>

有没有这样的方法:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using System.Web.UI.HtmlControls.HtmlGenericControl;

public partial class UserProfileWall : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

string theUserId = Session["UserID"].ToString();
//Label1.Text = Convert.ToString(theUserId);

using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=x; Password=x;"))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand("SELECT WallPosting.Wallpostings FROM WallPosting LEFT JOIN WallPosting ON User.UserID = WallPosting.UserID WHERE User.UserID=" + theUserId + "", cn))
//{

// cmd.Parameters.AddWithValue("@UserID", theUserId);


using (OdbcDataReader reader = cmd.ExecuteReader())
{
var divHtml = new System.Text.StringBuilder("<div id=mysqlcontent>");
while (reader.Read())
{
divHtml.Append(String.Format("{0}", reader.GetString(0)));
}

divHtml.Append("</div>");
test1.innerHtml = divHtml.ToString();
// error on this line innerHtml does not contain a definition for'innerHtml'?
}
}
}

不确定是否可以在 asp 中使用 C# 来做到这一点,所以我尝试了以下通过 jquery 的方法以防万一?但我不确定如何用 j 写?

$(function () {

$(page load).**unsure what goes here**(function () {
var x = $(retrieve some data from mysql table unsure if jquery can do this).val();
var newdiv = $("<div></div>").html(x).attr('id', 'mysqlcontent');
$('#test1').append(newdiv);
});
});

ASP html:

</script>
<p>
<asp:TextBox ID="TextBox1" name="TextBox1" runat="server" Rows="3"
Height="47px" Width="638px"></asp:TextBox>
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="Post Message" Width="98px"
onclick="Button1_Click" />
</p>
<p>
<asp:Table ID="Table1" name="Table1" runat="server" Width="488px"></asp:Table>
</p>

<div id="test1" runat="server" />

</asp:Content>

最佳答案

对于基本的 C# 实现,如果使用 runat="server" 标记 test1 div ...

<div id="test1" runat="server" />

...然后您可以在后面的代码中引用它...

protected global::System.Web.UI.HtmlControls.HtmlGenericControl test1;

...并且可以在设置内容之前使用 DataReader 构建内容

// prepare DataReader
var divHtml = new System.Text.StringBuilder("<div id=mysqlcontent>")
while (reader.Read()) {
divHtml.Append("something built from your database text");
}
divHtml.Append("</div>")
test1.InnerHtml = divHtml.ToString();

关于c# - 将mysql内容插入html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5402704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com