insert mysql content into html?
Hi im trying to figure out a way to write mysql content to html code:
I have an area on my asp page named &开发者_如何学Pythonlt;div id="test1"></div>
(this area is static)
I want to take content from my table and place code inside this div under a different div name (div id=sqlcontent)
so it would look like this:
<div id="test1">
<div id="mysqlcontent">something the user would write which is stored in my db</div>
</div>
is there a way like this:
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'?
}
}
}
Not sure its possible with C# in asp to do that so I have attempted below a way via jquery incase? But im unsure how to write in 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>
For a basic C# implementation, if you tag the test1 div with runat="server"
...
<div id="test1" runat="server" />
... then you can reference it in your code behind ...
protected global::System.Web.UI.HtmlControls.HtmlGenericControl test1;
... and can build up the content using the DataReader before setting the content
// 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();
Alternatively you can add literal control inside div.
<div id="test1">
<div id="mysqlcontent">
<asp:literal id="literal1" runat="server"></asp:literal>
</div>
</div>
After this you can apply any data (including html tags) you want like this
literal1.Text="something";
literal1.Text+="More data";
Of course there are several ways and the most easiest way is to put the runat="server" in the div mysqlcontent then @ the time of postback ur able to access this div.
The Other way is using ajax method or using jQuery.
$("#mysqlcontent").html("sqlthere")
EDIT
To achieve your goal you have to know how ajax works but also -what is ajax?-.
I'm not going to write down a whole course with what I know, but just some straight information. Ajax is a mechanism that allows you to do asynchronous calls. Exemple, your client visits your website, the main page is loaded. And people keep posting for example news, you don't want the user to be forced to refresh for him getting the new news, you just want to keep the main page the way it is, and just query the server and ask it if it's got some news for us.
Neither Javascript nor JQuery can talk to MySQL, or any other RDBM. You have to create a file let's say: getComments.asp
All this file does is query the mysql database and return back the results in any format you'd like (plain text, html, xml, json, ...etc).
Then in the client side you do something like:
1- In case your getComments.asp returns your comments within HTML for example it might return:
<div class="coment">I am a good comment</div>
<div class="coment">I am a short</div>
<div class="coment">I am a looooooooooooooooooong</div>
Then, in jquery you do:
$.ajax({
url: 'getComments.asp',
success: function(data) {
$('#myCommentsDiv').html(data);
}
});
2- Json: Asp returns comments in json (see http://www.google.be/search?sourceid=chrome&ie=UTF-8&q=c%23+json) and in your code you do:
$.ajax({
url: 'getComments.asp',
DataType: 'json',
success: function(data) {
var comments = eval('(' + data + ')');
for (comm in comments){
$('#commentsDiv').prepend("<div class='comment'>" + comm + "</div>");
}
}
});
For more information on jquery/ajax: http://api.jquery.com/jQuery.ajax/
精彩评论