2 skylinetop skylinetop 于 2014.02.19 11:49 提问

C# ajax不能实现页面部分刷新问题

小菜鸟求助。我想实现点击PostBack按钮,更新Textbox1的值,但是页面其他部分不刷新。测试时,我先点击SetColor按钮设置表格第6行字体为红色,并在Textbox2中输入任意测试值,在点击PostBack按钮后,Textbox1值被更新,Textbox2中测试值也没变,但是页面有明显的闪屏,同时表格第6行设置的字体颜色也被刷掉了,是什么原因呢?请大神们帮帮忙,跪谢了~!

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="tt.aspx.cs" Inherits="ASPX_NewKjbb_tt" %>
<!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">
        function t1() {
            tr5.style.color = "red";
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="PostBack" runat="server" onclick="Button1_Click" Text="PostBack" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <input ID="BT_SetColor" type="button" value="SetColor" onclick="t1();" />
    <div id="Tdiv" runat="server">
    </div>
    </form>
</body>
</html>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ASPX_NewKjbb_tt : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            tt();
    }
    private void tt()
    {
        Tdiv.InnerHtml = "<table id='tb01' border='1'>";
        for (int i = 0; i < 10; i++)
        {
            Tdiv.InnerHtml = Tdiv.InnerHtml + "<tr id='tr"+i+"'><td>NO:" + i + "</td><td>Test Table Row"+i+"</td>";
        }
        Tdiv.InnerHtml = Tdiv.InnerHtml + "</table>";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "PostBack String......";
    }
}

1个回答

wangwu2235
wangwu2235   2014.02.28 13:53

想要局部刷新的页面都需要放到局部刷新的区域里。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!