C#后台给js传值(字符串)只收到第一个字符,求助!

uwjiaoxing 2012-03-12 08:44:52
第一个页面(后台默认):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!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" language="javascript">
//用于弹出b页
function ShowDialogArg(url, width, height) {
var sFeature = "dialogWidth:" + width + "px;dialogHeight:" + height + "px;resizable:no;scroll:yes;center:yes;help:no";
return window.showModalDialog(url, window, sFeature);
}
//获取b页回传得数据
function GetGoods() {
var goodsName = ShowDialogArg('new0.aspx', 800, 600);
if (goodsName != null) {
document.getElementById("txt_goodsname").value = goodsName[0];
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="txt_goodsname" runat="server" ReadOnly="True" Width="217px"/>
<input id="Button1" onclick="javascript:GetGoods()" type="button" />
</div>
</form>
</body>
</html>

第二个页面.即new0.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="new0.aspx.cs" Inherits="new0" %>

<!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" language="javascript">
function getValue(aa){
var ss = new Array();
ss[0] = "eeeere";
window.returnValue = aa;
//window.returnValue = ss;
window.close();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</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;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class new0 : System.Web.UI.Page
{
static string aa;
protected void Page_Load(object sender, EventArgs e)
{
aa = "rwert";
Label1.Attributes.Add("ondblclick", "getValue('" + aa + "')");
}
}


这个所有的代码,然后问题是,第一个页面接收值的"txt_goodsname"只能取到第一个字符'r'.
就这样一个问题, 苦恼了一天...
...全文
152 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
uwjiaoxing 2012-03-12
  • 打赏
  • 举报
回复
呵呵, js确实是一点基础也没有啊,现在做的个作业...做完一定好好学习啊...
上面那个也解决了
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
.....
}

事件绑定错了哈,谢大伙了...
结贴.
EnForGrass 2012-03-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 uwjiaoxing 的回复:]

引用 1 楼 huangwenquan123 的回复:

document.getElementById("txt_goodsname").value = goodsName[0];
===>
document.getElementById("txt_goodsname").value = goodsName;

晕,一个就解决了...上面的代码也是从网上copy下来的啊...不知道……
[/Quote]
貌似你事件用错了,我记得好像是GridView3_RowDataBound
奔跑的老王 2012-03-12
  • 打赏
  • 举报
回复
???我回复的怎么找到不到呢?
奔跑的老王 2012-03-12
  • 打赏
  • 举报
回复
[Quote=引用楼主 uwjiaoxing 的回复:]
第一个页面(后台默认):
C# code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o……
[/Quote]
楼主得到一个字符就不错了,你现在的代码一个字符都得不到,因为本来就是一个string类型的,你一定要按照数组来取它的第一个字符,肯定不行啦,其实返回值是得到了,只是你取值的时候不对,textbox的值应该是undefined。建议楼主去学习一下js的数组吧。基础很重要
uwjiaoxing 2012-03-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huangwenquan123 的回复:]

document.getElementById("txt_goodsname").value = goodsName[0];
===>
document.getElementById("txt_goodsname").value = goodsName;
[/Quote]
晕,一个就解决了...上面的代码也是从网上copy下来的啊...不知道有问题...
上面的代码其实是做了简化的,我想要的是这个:
protected void GridView3_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//光标效果
e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#CCC';this.style.cursor='pointer';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color;");
//事件绑定
e.Row.Attributes.Add("ondblclick", "getValue('" + e.Row.Cells[1].Text + "')");

}
}

就是双击gridview某一行,然后取特定单元格的值,上面的解决了,可这个双击时,什么也取不到....
bdmh 2012-03-12
  • 打赏
  • 举报
回复
goodsName[0],取goodsName的第一个字符
huangwenquan123 2012-03-12
  • 打赏
  • 举报
回复
document.getElementById("txt_goodsname").value = goodsName[0];
===>
document.getElementById("txt_goodsname").value = goodsName;

62,268

社区成员

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

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

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

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