请教如何在ASP.NET AJAX服务器端中获取DIV(DIV是用户可编辑的)的innerHTML?

eleven1012 2007-11-21 08:32:32
代码如下:

(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可以获得,道理我也明白是怎么回事,但是请高手指点该如何解决。谢谢!
...全文
256 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
西安风影 2007-11-22
  • 打赏
  • 举报
回复
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "script1 ", "document.getElementById( 'HiddenField1 ').value = div1.innerHTML; ", true);

与之对应有一个RegisterStartupScript()方法
你可以使用这个方法
xiaojing7 2007-11-22
  • 打赏
  • 举报
回复
今天早上的目的是把0回复的顶起来

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧