我的页面中有这个asp.net代码:
<div id="prx">ABC</div>
我希望将值“ABC”更改为某些内容,例如用户在TextBox中键入值。
如何使用Ajax
?
由于
答案 0 :(得分:4)
您不需要AJAX来执行此操作。您只需使用Javascript即可使用INPUT小部件的内容更新DIV标记的内容。请参阅How to set the value of a form element using Javascript。
现在,如果您想在不重新加载页面的情况下使用服务器更新TextBox,那么那是 AJAX。我会在UpdatePanels上使用 jQuery.ajax()函数。这是一个jQuery AJAX Tutorial。
答案 1 :(得分:3)
查看ASP.NET AJAX UpdatePanel控件。它允许您更改页面上的文本和“AJAX-ifies”内部的任何内容,而不是进行完整的回发。 Here是一个很好的教程。
答案 2 :(得分:3)
可能正在使用javascript?)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication11.WebForm2" %>
<!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 id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function setDivContent() {
var textInput = document.getElementById('text1');
var divPrx = document.getElementById('prx');
divPrx.innerHTML = textInput.value;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="prx">ABC</div>
<br />
<input type="text" id="text1" />
<button onclick="javascript:setDivContent(); return false;">Set</button>
</div>
</form>
</body>
</html>