[英]asp.net repeater “the server tag is not well formed”
Before marking me down, or flagging as a duplicate I have looked and cannot find my answer on here although there are similar issues - most are relating to javascript calls on asp.net controls. 在标记我的名字或将其标记为重复项之前,尽管有类似的问题,但我在这里找不到并没有找到答案-大多数问题与asp.net控件上的javascript调用有关。 This is not that.
不是那样的
I am trying to provide an id to a checkbox in a repeater based on the value that comes out of the datasource that is bound to the repeater. 我正在尝试根据绑定到转发器的数据源中的值为转发器中的复选框提供一个ID。
I have had no trouble with elements that are not asp.net controls, however when it comes to providing an it to a checkbox like the example below I get the error 对于没有asp.net控件的元素,我没有遇到任何麻烦,但是在将其提供给复选框(如下面的示例)时,出现了错误
Error creating control - rptPayments
创建控件时出错-rptPayments
The server tag is not well formed.
服务器标签格式不正确。
This is how I am trying to name it at the moment 这就是我现在试图命名的方式
<asp:CheckBox runat="server" ID="cbValidated_<%# Eval("ContactId")%>" />
I have seen that there are a number of questions out there for similar issues but typically they are for javascript \\ OnClientClick(..) funcitons telling people to use single quotes. 我已经看到有很多类似问题的问题,但通常它们是针对javascript \\ OnClientClick(..)函数,告诉人们使用单引号。
if I try single quotes, I get a new error 如果我尝试使用单引号,则会收到新错误
the ID property of a control can only be set using the ID attribute in the tag and a simple value.
控件的ID属性只能使用标签中的ID属性和简单值进行设置。 Example
例
I need this as a server side control so that I can loop through all the checkboxes on the click of a button to see which records have been validated or not, but need to link it with the contactId 我需要将其作为服务器端控件,以便单击按钮即可遍历所有复选框,以查看哪些记录已通过验证,但需要将其与contactId链接
I assume that your bulk-update button is present outside the repeater. 我假设您的批量更新按钮位于转发器之外。 In that case you will have you to loop through the repeater items and proceed like this:-
在这种情况下,您将需要遍历转发器项并按以下步骤进行:
protected void btnUpdate_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in MyRepeater.Items)
{
CheckBox cbValidated = item.FindControl("cbValidated") as CheckBox;
if (chkTest.Checked)
{
//Do Stuff (You can save some custom values in a list and finally update all)
}
}
}
Update: 更新:
As mentioned in the comment the correct way to do this is to store the id of the record is in a hidden variable rather than setting it to the ID
of a control (which is not possible anyways). 如评论中所述,正确的方法是将记录的ID存储在一个隐藏变量中,而不是将其设置为控件的
ID
(无论如何这是不可能的)。 So simply add one Hidden field like this:- 因此,只需添加一个隐藏字段,如下所示:-
<ItemTemplate>
<asp:CheckBox ID="cbValidated" runat="server" Text='<%# Eval("foo") %>' />
<asp:HiddenField ID="hdnID" runat="server" Value='<%# Eval("ID") %>' />
</ItemTemplate>
Here, ID
is the name of the column which will map to your record. 在此,
ID
是将映射到您的记录的列的名称。 Then simply find this value if user has checked the checkbox like this:- 然后,如果用户已选中此复选框,则只需找到该值即可:-
//Rest code same as mentioned above
if (chkTest.Checked)
{
HiddenField hdnID = item.FindControl("hdnID") as HiddenField;
string id = hdnID.Value; //use this id for your logic.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.