简体   繁体   English

服务器标签格式不正确。(databinder.eval)

[英]The server tag is not well formed.(databinder.eval)

I am trying to working the following code. 我正在尝试使用以下代码。

 <asp:DataGrid ID="Grid" runat="server"  DataKeyField="KeyID" CssClass="grid"
...
<asp:CheckBox runat="server"  ID="checkBox-<%#DataBinder.Eval(Container.DataItem,"KeyID")%>" AutoPostBack="false"></asp:CheckBox>

When I run the code,I have got the below error: 当我运行代码时,我得到以下错误:

Error   25  The server tag is not well formed.

Note : This is working without runat="server". 注意:这没有runat =“server”。 What is the remedy for this problem ? 这个问题的补救措施是什么?

You don't need to set the ID of the CheckBox to do what you want to do (change the background color). 您不需要设置CheckBox的ID来执行您想要执行的操作(更改背景颜色)。 Your CheckBox should look like this (I added the KeyID as the text value if you want to display it... or you can just remove that if you only want the checkbox): 你的CheckBox应该是这样的(如果你想显示它,我将KeyID添加为文本值......或者如果你只想要复选框,你可以删除它):

<asp:CheckBox runat="server" ID="checkbox" Text='<%# Eval("KeyID") %>' AutoPostBack="false"></asp:CheckBox>  

Now your checkbox will render something like this: 现在您的复选框将呈现如下内容:

<input id="MainContent_Grid_checkbox_0" type="checkbox" name="ctl00$MainContent$Grid$ctl02$checkbox" /><label for="MainContent_Grid_checkbox_0">Value of KeyID</label> 

Since all the names end with "checkbox", you can apply a function on the change event for those elements whose name ends with "checkbox". 由于所有名称都以“复选框”结尾,因此您可以对名称以“复选框”结尾的元素应用更改事件的函数。 You didn't specify that this was a JavaScript question or if you are using jQuery... this answer uses jQuery : 您没有指定这是一个JavaScript问题,或者如果您正在使用jQuery ...这个答案使用jQuery

<script>
    $('input[name$="checkbox"]').change(function () {
        if ($(this).is(':checked')) {
            $(this).parent().css('background-color', 'yellow');
        }
        else {
            $(this).parent().css('background-color', 'white');
        }
    });
</script> 

That will determine if the checkbox is checked, and if so it will set the background-color of its parent (the <td> that it is in, inside the DataGrid rendered HTML), depending on the value. 这将决定是否选中复选框,如果是,它将设置其父级的背景颜色(它所在的<td> ,在DataGrid渲染的HTML中),具体取决于值。

Likewise, you can go up to the next parent() and highlight the entire row. 同样,您可以转到下一个parent()并突出显示整行。

Resources: jQuery Selectors 资源: jQuery选择器

OnCheckedChanged -- an event that you would process in the code behind, not in JavaScript. OnCheckedChanged - 您将在后面的代码中处理的事件,而不是JavaScript中的事件。

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

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