有一个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;}
}
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;}
}
function jsFunctionName(a1,a2)//类别 名称 规格
{
window.document.getElementById("txtA").value=a1;}
window.document.getElementById("txtB").value=a2;}
}
function jsFunctionName(a1,a2)//类别 名称 规格
{
window.document.getElementById("txtA").value=a1;
window.document.getElementById("txtB").value=a2;
}
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(" ", ""); + "')");
}
}
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() + "')");
}
}
window.document.getElementById("txtA").value=str1;} ------------------------------------------------------------------转换后 str1 从 " " 变成 "" 了按照现有代码的逻辑 根本无意义
这样代码无需改动!!
只要加GridView前台代码,如下:
......
<asp:BoundField DataField="字段" HeaderText="xxxx" NullDisplayText=" " >