为什么服务器button要按两次才能复制表格

wnsre 2016-06-06 02:11:26
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 type="text/javascript" language="JavaScript">
function CopyTable()
{
var txt = document.body.createTextRange();
var r_type = document.getElementById("Table1"); //表格的ID值
txt.moveToElementText(r_type);
txt.select();//选择对象
document.execCommand("Copy"); //执行浏览器复制命令
}

</script>

</head>
<body>
<form id="form1" runat="server">
<div>

</div>

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" clientidmode="Static" />

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Table ID="Table1" runat="server" BorderWidth="1px">
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell ID="TableCell1" runat="server">
</asp:TableCell>
<asp:TableCell ID="TableCell2" runat="server">
</asp:TableCell>
</asp:TableRow>
</asp:Table>

</form>
</body>
</html>

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{

Table1.Rows[0].Cells[0].Text=TextBox1.Text;
Table1.Rows[0].Cells[1].Text =TextBox2.Text;
// Button1.Attributes.Add("onclick", "return CopyTable()");
Button1.Attributes["onclick"] = "return CopyTable()";

}
}

按第一次没反应,每次输入不同的值都要按两次button,为什么?
win7+vs.net2008测试环境
...全文
174 14 打赏 收藏 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wnsre 2016-06-06
  • 打赏
  • 举报
回复
那有没有办法用javascript为服务器控件table赋值?
wnsre 2016-06-06
  • 打赏
  • 举报
回复
12楼的方法行不通
Hertz_liu 2016-06-06
  • 打赏
  • 举报
回复
Button1.Attributes["onclick"] = "return CopyTable()"; 这句放到page_load去 或是换成 Page.ClientScript.RegisterStartupScript(this.GetType(), "script", "return CopyTable();", true);
wnsre 2016-06-06
  • 打赏
  • 举报
回复
第二种方法具体怎么实施?
一枚大帅哥 2016-06-06
  • 打赏
  • 举报
回复
2种解决办法。 1.使用ajax,访问服务端并做了相应操作之后,根据服务端的返回值再执行前端脚本。 ajax使用参见:http://www.w3school.com.cn/jquery/ajax_ajax.asp 2.仍然使用你现在的方法,前端增加一个hidden,后端处理完成后将hidden设置为true.前端加载时判断这个hidden为true时执行指定脚本CopyTable()
wnsre 2016-06-06
  • 打赏
  • 举报
回复
我直接放在服务器的button没反应
wnsre 2016-06-06
  • 打赏
  • 举报
回复
是放在html页面,重新用一个函数包含这三句,然后再用服务器button的Attributes吗?
还想懒够 2016-06-06
  • 打赏
  • 举报
回复
Table1.Rows[0].Cells[0].Text=TextBox1.Text; Table1.Rows[0].Cells[1].Text =TextBox2.Text; Page.ClientScript.RegisterStartupScript(this.GetType(), "ABCDE", "return CopyTable();", true);
wnsre 2016-06-06
  • 打赏
  • 举报
回复
具体怎么用,我是初学者,有代码指导吗
  • 打赏
  • 举报
回复
操作完数据库执行js的话,那你应该用执行js的那个RegisterClientXX方法,而不是将js注册给button
wnsre 2016-06-06
  • 打赏
  • 举报
回复
怎么用纯javascript,另外我的服务器button之前还有其它的数据库操作的,操作完数据库,再copytable
还想懒够 2016-06-06
  • 打赏
  • 举报
回复
真不怕服务器压力大。为何不用纯javascript,而先跑到后台去一次呢? 你这当然是需要按两次才能复制: 第一次:Button的onclick没有东西,你提交到后台,给attributes赋值 第二次:有return CopyTable()的动作 还有,你的return CopyTable()中return了啥?
wnsre 2016-06-06
  • 打赏
  • 举报
回复
不行啊,而且赋值要按了button再进行
  • 打赏
  • 举报
回复
将注册onclick的事情写到pageload里去

62,270

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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