110,566
社区成员
发帖
与我相关
我的任务
分享
private delegate void ShowMsg(string strMsg);
private ShowMsg SM;
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
SM += new ShowMsg(SetLabel);
dt.Columns.Add("yvalue", typeof(string));
dt.Rows.Add();
}
private void SetLabel(string strMsg)
{
gvOpint.DataSource = dt;
gvOpint.DataBind();
}
protected void Button1_Click1(object sender, EventArgs e)
{
Sockelisten();
}
void Sockelisten()
{
IPAddress ips = Dns.GetHostAddresses(Dns.GetHostName())[0];
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.2.59"), 6666);
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Bind(ip);
soc.Listen(10);
Socket hander = soc.Accept();
while (true)
{
byte[] bytes = new byte[1024];
int len = hander.Receive(bytes);
string st = Encoding.Default.GetString(bytes);
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1][0] = st;
GridView.Invoke(SM); //这一句代码在CS中是会报错的
Thread.Sleep(1000);
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="jquery-1.8.3.min.js" type="text/ecmascript"></script>
<title></title>
<script type="text/javascript">
window.setInterval(getAjaxData, 5000);
function getAjaxData(){
$.ajax({
type: "GET",
async: false,
//url: "WebForm2.aspx?abc="+Math.random(),
//dataType: "html",
url: "WebForm1.aspx/SetLabel",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
//返回的数据用data.d获取内容
alert(data.d);
},
error: function(err) {
alert(err);
}
//success:function(html){
// $("#ajaxData").html(html);
//}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="ajaxData"></div><asp:GridView runat="server" ID="GridView1"></asp:GridView>
</form>
</body>
</html>
后台
static DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
dt.Columns.Add("yvalue", typeof(string));
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1][0] = "开始";
GridView1.DataSource = dt;
GridView1.DataBind();
Thread t = new Thread(new ThreadStart(rand));
t.Start();
}
private string SetLabel()
{
GridView1.DataSource = dt;
GridView1.DataBind();
return "cs";
}
void rand()
{
for (int i = 0; i < 100;i++ )
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1][0] = i.ToString()+"开始";
Thread.Sleep(5000);
}
}
}
}
我这个是模拟的SOCKET接收 SOCKET接收也是不断的更新datatable 我这个也是 然后前台AJAX调用绑定函数 结果只弹窗 没刷新AJAX