JavaScript 传值

hailiu123 2010-03-02 03:57:30
有一个GridView
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Attributes.Add("onclick", "jsFunctionName('" + e.Row.Cells[1].Text.ToString().Trim() + "','" + e.Row.Cells[2].Text.ToString().Trim() + "')");
}
}

在前台的head中
<head id="Head1" runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">

function jsFunctionName(a1,a2)//类别 名称 规格
{ var str1=a1;var str2=a2;
window.document.getElementById("txtA").value=str1;
window.document.getElementById("txtB").value=str2;
}

</script>

</head>

如果在GridView中 e.Row.Cells[1].Text 为空的话 传到前台 a1就变成“ ”了,
除了下面这么办 还有其他方法吗,我的GridView列很多的下面这样写很费时间的。
function jsFunctionName(a1,a2)//类别 名称 规格
{ var str1=a1;var str2=a2;
if(str1!=" "){
window.document.getElementById("txtA").value=str1;}
if(str2!=" "){
window.document.getElementById("txtB").value=str2;}
}


...全文
136 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
gelinbod 2010-03-03
  • 打赏
  • 举报
回复
引用 11 楼 koukoujiayi 的回复:
 产生是字段中null产生的,你可以在GridView绑定的时候将null值改为一个空格!
这样代码无需改动!!
只要加GridView前台代码,如下:
......
<asp:BoundField DataField="字段" HeaderText="xxxx"NullDisplayText=" " >


我赞成这样做
zhongxiutao123 2010-03-03
  • 打赏
  • 举报
回复
自己来!!!!!!!!!!!!!!!!!
我姓区不姓区 2010-03-03
  • 打赏
  • 举报
回复

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Attributes.Add("onclick", "jsFunctionName('" + Server.HtmlDecode(e.Row.Cells[1].Text).Trim() + "','" + Server.HtmlDecode(e.Row.Cells[2].Text).Trim() + "')");
}
}

xray2005 2010-03-03
  • 打赏
  • 举报
回复
引用 5 楼 hzsasheng 的回复:
不想前台写,就后台传数据的时候先replace一下


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Attributes.Add("onclick", "jsFunctionName('" + e.Row.Cells[1].Text.ToString().Trim().Replace(" ", ""); + "','" + e.Row.Cells[2].Text.ToString().Trim().Replace(" ", ""); + "')");
          }
    }


OK
mail_ylei 2010-03-03
  • 打赏
  • 举报
回复
5楼的方法挺好 顶下
koukoujiayi 2010-03-03
  • 打赏
  • 举报
回复
 产生是字段中null产生的,你可以在GridView绑定的时候将null值改为一个空格!
这样代码无需改动!!
只要加GridView前台代码,如下:
......
<asp:BoundField DataField="字段" HeaderText="xxxx" NullDisplayText=" " >
huangbaohua 2010-03-03
  • 打赏
  • 举报
回复
没办法,自己顶~~~~~~
阿非 2010-03-03
  • 打赏
  • 举报
回复
if(str1!=" "){
window.document.getElementById("txtA").value=str1;}

------------------------------------------------------------------

转换后 str1 从 " " 变成 "" 了

按照现有代码的逻辑

根本无意义
加油馒头 2010-03-03
  • 打赏
  • 举报
回复
我ko 这也行、、、CSDN人才真多
hzsasheng 2010-03-02
  • 打赏
  • 举报
回复
不想前台写,就后台传数据的时候先replace一下


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Attributes.Add("onclick", "jsFunctionName('" + e.Row.Cells[1].Text.ToString().Trim().Replace(" ", ""); + "','" + e.Row.Cells[2].Text.ToString().Trim().Replace(" ", ""); + "')");
}
}
丰云 2010-03-02
  • 打赏
  • 举报
回复
改下:
function jsFunctionName(a1,a2)//类别 名称 规格
{
window.document.getElementById("txtA").value=a1;
window.document.getElementById("txtB").value=a2;
}
阿非 2010-03-02
  • 打赏
  • 举报
回复
按照你现在的描述情况,没什么办法
丰云 2010-03-02
  • 打赏
  • 举报
回复
方法很多,不过下面这样没问题,不需要改吧
function jsFunctionName(a1,a2)//类别 名称 规格
{
window.document.getElementById("txtA").value=a1;}
window.document.getElementById("txtB").value=a2;}
}
hailiu123 2010-03-02
  • 打赏
  • 举报
回复
自己来!!!!!!!!!!!!!!!!!

62,025

社区成员

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

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

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

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