普通的AJax應用,結合Javascript對Html元素的動態操控,結合而成一個優美的效果。
還是用AJax.Net,設置與前篇文章一樣,不再累述。
代碼如下:
163photoTest.ASPx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClIEnt;
public partial class _163photoTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
description.Text = GetDescription();
AjaxPro.Utility.RegisterTypeForAJax(typeof(_163photoTest));
}
//取數據庫裡的描述
protected string GetDescription()
{
try
{
string strComm = "select photo_description from tab_photo where photo_id=1";
SqlConnection myConn = new SqlConnection("user id=sa;passWord=123;Database =test;data source=z;");
myConn.Open();
SqlCommand myComm = new SqlCommand(strComm, myConn);
SqlDataReader myReader = myComm.ExecuteReader();
myReader.Read();
return myReader.GetString(0);
}
catch
{
return "";
}
}
[AjaxPro.AJaxMethod]
//將description保存進數據庫 www.knowsky.com
public bool UpdateDesc(string description)
{
try
{
string strComm = "update tab_photo set photo_description='" + description + "' where photo_id=1";
SqlConnection myConn = new SqlConnection("user id=sa;passWord=123;Database =test;data source=z;");
myConn.Open();
SqlCommand myComm = new SqlCommand(strComm, myConn);
myComm.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
}
}
163photoTest.ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="163photoTest.ASPx.cs" Inherits="_163photoTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHtml 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">
<Html XMLns="http://www.w3.org/1999/xHtml" >
<head runat="server">
<title>無標題頁</title>
<script language="Javascript" type="text/Javascript">
function display()
{
document.getElementById('descfIEld').style.display='block';
document.getElementById('descfIEld').value = document.getElementById('description').innerText ;
document.getElementById('description').style.display='none';
document.getElementById('save').style.display='block';
}
var s;
function UpdateDesc()
{
s= document.getElementById("descfIEld").value;
document.getElementById("descfIEld").value="正在保存";
_163photoTest.UpdateDesc(s,UpdateDesc_callback);
}
function UpdateDesc_callback(res)
{
if (res)
{
document.getElementById('description').style.display='block';
document.getElementById("description").innerText =s;
document.getElementById('descfIEld').style.display='none';
document.getElementById('save').style.display='none';
}
}
</script>
</head>
<body onload="document.getElementById('descfIEld').style.display='none';document.getElementById('save').style.display='none';">
<form id="form1" runat="server">
<div id="div1" onmousemove="this.style.backgroundColor='#C0C0FF';" onmouSEOut="this.style.backgroundColor='';" onclick="display()">
<asp:Label ID="description" runat="server" Text="Label"></ASP:Label>
</div>
<input type="text" id ="descfIEld" />
<input type="button" id="update" value="修改描述" onclick="display()"/>
<input type="button" id="save" value="保存修改" onclick="UpdateDesc()"/>
<br />
</form>
</body>
</Html>
出處:Coder at the Doorsill——風中聆聽 BLOG