简体   繁体   English

我的JavaScript有什么问题? (C#/ ASP.NET)

[英]What's wrong with my JavaScript? (C#/ASP.NET)

Here is my JavaScript: 这是我的JavaScript:

<script type="text/javascript">
function onholdev(index) {
    var chk = document.getElementById('<%=grdCons.Rows[' + index + '].FindControl("chkHold").ClientID %>');
    var txt = document.getElementById('<%=grdCons.Rows[' + index + '].FindControl("txtReason").ClientID %>');


    if (chk.checked == true) {
        txt.disabled = false;
    }
    else {
        txt.disabled = true;
        txt.value = "";
    }
}
</script>

The 'index' variable comes from the RowDataBound event of my GridView, like so: 'index'变量来自我的GridView的RowDataBound事件,如下所示:

  CheckBox chkHold = ((CheckBox)e.Row.FindControl("chkHold"));
  chkHold.Attributes.Add("onchange", "onholdev(" + e.Row.RowIndex + ")");

However, I'm getting 'too many characters in string literal' in the first line of my function (beginning with var chk). 但是,在函数的第一行(从var chk开始),“字符串文字中的字符太多”。 Why is this? 为什么是这样?

You're mixing client and server-side script...you just can't do this. 您正在混合客户端和服务器端脚本...您只是做不到。 This is executed server-side: 这是在服务器端执行的:

grdCons.Rows[' + index + '].FindControl("chkHold").ClientID

But you're calling it client-side and trying to pass an ID, that's just not something you can do, look at your rendered JavaScript function and this will be much clearer. 但是,您在客户端调用它并尝试传递一个ID,这不是您可以做的,请查看呈现的JavaScript函数,这将更加清楚。 Instead just use the ID of the table, then you can find your controls that way, for example: 而是只使用表的ID,然后您可以通过这种方式找到控件,例如:

var row = document.getElementById("grdCons").rows[index];
var inputs = row.getElementsByTagName("input");
//then filter by ID match, class, whatever's easiest and set what you need here

That's probably because ASP.NET throws an error, which is written in the client side call of getElementById . 这可能是因为ASP.NET引发错误,该错误写在getElementById的客户端调用中。 The onholdev function is executed client - side, and so cannot pass the index parameter to ASP.NET which is executed server - side. onholdev函数是在客户端执行的,因此无法将index参数传递给在服务器端执行的ASP.NET。 Try this: 尝试这个:

<script type="text/javascript">
function onholdev(checkbox, textbox) {
    var chk = document.getElementById(checkbox);
    var txt = document.getElementById(textbox);

    if (chk.checked == true) {
        txt.disabled = false;
    }
    else {
        txt.disabled = true;
        txt.value = "";
    }
}
</script>

Replacing your server - side code with this: 以此替换服务器-辅助代码:

CheckBox chkHold = ((CheckBox)e.Row.FindControl("chkHold"));
chkHold.Attributes.Add("onchange", "onholdev('" + 
    e.Row.FindControl("chkHold").ClientID + "','" + 
    e.Row.FindControl("txtReason").ClientID + "')");

The problem is your use of the single quote characters in ' + index + ' . 问题是您在' + index + '使用了单引号字符。 Change those to double qoutes and it should work. 将其更改为双qoutes,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM