我想绑定asp.net文字控件,所以我用c#代码完成,如下所示..
private void GetMessages()
{
try
{
int cntr = 0; //toggle counter for alternating color
string username = lblUserName.Text;
if (connection.State == ConnectionState.Closed)
connection.Open();
command = new SqlCommand();
command.CommandText = "Get_Messages";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@roomId", lblRoomId.Text);
command.Connection = connection;
da = new SqlDataAdapter(command);
ds = new DataSet();
da.Fill(ds);
if (connection.State == ConnectionState.Open)
connection.Close();
command.Dispose();
if (ds.Tables[0].Rows.Count != 0)
{
StringBuilder sb = new StringBuilder();
int i = (ds.Tables[0].Rows.Count - 1);
for (int j = 0; j <= i; j++)
{
string personName = ds.Tables[0].Rows[j]["Username"] == null ? "" : ds.Tables[0].Rows[j]["Username"].ToString();
string gender = ds.Tables[0].Rows[j]["Sex"] == null ? "" : ds.Tables[0].Rows[j]["Sex"].ToString();
string message = ds.Tables[0].Rows[j]["Text"] == null ? "" : ds.Tables[0].Rows[j]["Text"].ToString();
if (cntr == 0)
{
if (username == personName)
{
sb.Append("<div style='padding: 10px;text-align:right'>");
}
else
{
sb.Append("<div style='padding: 10px;'>");
}
cntr = 1;
}
else
{
if (username == personName)
{
sb.Append("<div style='background-color: #EFEFEF;padding: 10px;text-align:right'>");
}
else
{
sb.Append("<div style='background-color: #EFEFEF;padding: 10px;'>");
}
cntr = 0;
}
lblUserNames.Text = "<span style='color: Blue;'><b line-height:22px>" + personName + "</b></span>";
lblUserNames.Visible = true;
if (gender.ToLower() == "m")
{
if (username == personName)
{
sb.Append(message + "</div>");
}
else
{
sb.Append("<img src='Images/manIcon.gif' style='vertical-align:middle;' alt=''> " + lblUserNames.Text + " " + message + "</div>");
}
}
else
{
if (username == personName)
{
sb.Append(message + "</div>");
}
else
{
sb.Append("<img src='Images/womanIcon.gif' style='vertical-align:middle' alt=''> " + lblUserNames.Text + " " + message + "</div>");
}
}
}
litMessages.Text = sb.ToString();
lblUserNames.Visible = false;
}
else
{
lblUserNames.Visible = false;
}
}
catch (Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Exception Message: " + ex.Message.Replace("'", "").Replace("\"", "") + "');", true);
}
}
但是尝试在javascript和json中做同样的事情但是它没有绑定我成功获取数据的地方。在哪里我得到的错误无法理解,所以请任何人都可以帮我绑定它javascript方..
<script type="text/javascript">
window.setInterval(GetRoomMessages, 10000);
function GetRoomMessages() {
var cntr = 0;
var html = [];
var roomId = getCookie('roomId');
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
var username = getCookie('userName');
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "temp.aspx/BindShopClothInfo",
data: "{'roomId':'" + roomId + "'}",
dataType: "json",
success: function (response) {
var sb = "";
for (var i = 0; i < response.d.length; i++) {
var personName = response.d[i].Username;
var gender = response.d[i].Sex;
var message = response.d[i].Text;
if (cntr == 0) {
if (username == personName) {
sb += ("<div style='padding: 10px;text-align:right'>");
}
else {
sb += ("<div style='padding: 10px;'>");
}
cntr = 1;
}
else {
if (username == personName) {
sb += ("<div style='background-color: #EFEFEF;padding: 10px;text-align:right'>");
}
else {
sb += ("<div style='background-color: #EFEFEF;padding: 10px;'>");
}
cntr = 0;
}
$("#lblUserNames").val("<span style='color: Blue;'><b line-height:22px>" + personName + "</b></span>");
$("#lblUserNames").prop("visibility", "block");
if (gender.toLowerCase() == "m") {
if (username == personName) {
sb += (message + "</div>");
}
else {
var UserNames = $("#lblUserNames").val();
sb += ("<img src='Images/manIcon.gif' style='vertical-align:middle;' alt=''> " + UserNames + " " + message + "</div>");
}
}
else {
if (username == personName) {
sb += (message + "</div>");
}
else {
var UserNames = $("#lblUserNames").val();
sb += ("<img src='Images/womanIcon.gif' style='vertical-align:middle' alt=''> " + UserNames + " " + message + "</div>");
}
}
}
$("#litMessages").append(sb);
alert($("#litMessages").val());
//$("#ContentPlaceHolder1_lblProductName").html(Shopp.d[0].ClothName);
},
error: function (result) {
$("#lblUserNames").prop("visibility", "none");
alert("Error");
}
});
}
</script>
答案 0 :(得分:0)
而不是'literal'控件使用'label'控件。两者只是显示文字。