<%#%>绑定表达式的语法问题
这个绑定表达式怎么写?我这样写总是原样输出,不解析呢
也换了其他几种写法,也是不解析
我先解释下 我的目的是想在点击GridView的更新按钮时调用JS判断输入的值是否合法.
给CheckValue(string1,string2)传递两个参数值(当前行中两个单元格的值),本来想传入个行号,在JS中取值的,但是也是写不对语法.所以就写了两个方法在后台取值,这样写也是不能解析,直接当字符串输出了.
C# code
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="True" CommandName="Update"
Text="更新" onclientclick='<%#"return CheckValue("+getAmount(Container.ItemIndex))+getDate(Container.ItemIndex)%>+")"'></asp:LinkButton>
------解决方案--------------------
完整的例子
<%@ Page Language="C#" EnableViewState="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void bind()
{
System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser");
System.Data.DataRow dr;
dataTable1.Columns.Add(new System.Data.DataColumn("Count1", typeof(System.Int32)));
dataTable1.Columns.Add(new System.Data.DataColumn("Count2", typeof(System.String)));
Random r = new Random();
for (int i = 0; i < 8; i++)
{
dr = dataTable1.NewRow();
dr[0] = r.Next(0, 100);
dr[1] = "AAAAAAAAAAAAAAAAAAAAA";
dataTable1.Rows.Add(dr);
}
this.GridView1.DataSource = dataTable1;
this.GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bind();
}
}
public String getAmount(int x)
{
return "测试内容" + x.ToString();
}
public String getDate(int x)
{
return "2011-11-" + x.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function CheckValue(a,b) {
alert(a + "\r\n" + b);
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="GridView1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" Text="更新"
onclientclick=<%# "return CheckValue('" + getAmount(Container.ItemIndex) + "','" + getDate(Container.ItemIndex)+ "')" %>>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
------解决方案--------------------
protected void GridViewProject_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//获取行索引
int I=e.Row.RowIndex;
//获取对象
LinkButton LinkButton2 = e.Item.FindControl("LinkButton2") as LinkButton;
//添加 onclientclick 事件
LinkButton2.Attributes.Add("onclientclick ", "return CheckValue('根据行索引获取单元格数据1,根据行索引获取单元格数据2')");
}
}
GridView 的行绑定事件 我刚写的是 ItemDataBound 写错了 好久没用了 就是这个代码了