请教如何在ASP.NET AJAX服务器端中获取DIV(DIV是用户可编辑的)的innerHTML?
代码如下:
(1)index.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>
<!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>
<style type="text/css">
#div1{
width:200px;
height:25px;
border:solid 1px #000000;
}
</style>
</head>
<body>
<div id="div1">QQ:</div>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btn1" runat="server" Text="获得QQ" OnClick="btn1_Click" />
<asp:Label runat="server" ID="Label1"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<input type="hidden" id="HiddenField1" name="HiddenField1" />
</form>
</body>
</html>
<script type="text/javascript">
window.onload = function(){
div1.contentEditable = true;
}
</script>
(2)index.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;
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
{
string str = Request.Form["HiddenField1"].ToString();
Label1.Text = str;
}
}
protected void btn1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "script1", "document.getElementById('HiddenField1').value = div1.innerHTML;", true);
}
}
当点击“获得QQ”按钮后,第一次Label1不能获得用户输入的QQ码号,而第二次Label1可以获得,道理我也明白是怎么回事,但是请高手指点该如何解决。谢谢!