【问题标题】:How do you use inline code to set the value of a checkbox如何使用内联代码设置复选框的值
【发布时间】:2013-10-16 11:17:27
【问题描述】:

我有一个在循环中动态创建的复选框列表,当我尝试使用内联代码设置值时,它只是给我内联代码而不评估它。这是一个例子:

<ul>
   <%
    string testValue = string.Empty;
    for(int index = 0; index < 5; index++)
    {
        testValue = "blah" + index;
     %>
        <li>
            <input type="checkbox" runat="server" value="<%= testValue %>" />
        </li>
    <%
    }
     %>
</ul>

这是我得到的输出:

<ul>        
<li>
   <input name="ctl00$MainContent$ctl00" type="checkbox" value="&lt;%= testValue %>" />
</li>

<li>
    <input name="ctl00$MainContent$ctl00" type="checkbox" value="&lt;%= testValue %>" />
</li>

<li>
     <input name="ctl00$MainContent$ctl00" type="checkbox" value="&lt;%= testValue %>" />
</li>

<li>
      <input name="ctl00$MainContent$ctl00" type="checkbox" value="&lt;%= testValue %>" />
</li>

<li>
      <input name="ctl00$MainContent$ctl00" type="checkbox" value="&lt;%= testValue %>" />
</li>
</ul>

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 我认为你需要在value="&lt;%= testValue %&gt;"上使用单引号
  • 去掉runat="server",除非你需要复选框作为服务器控件
  • 如果我尝试单引号 value='" 我得到相同的结果:
  • 哎呀没有注意到runat="server",我在最后写了它并且它正在工作,因为我没有那个设置。无论如何,您都不需要runat="server",因为您的控件是动态生成的,您不需要以这种方式访问​​它们。
  • 我实际上需要找出在后面的代码中单击按钮时检查了哪些值,这就是我在服务器上运行复选框的原因。抱歉,示例有点不完整。

标签: html asp.net checkbox inline-code


【解决方案1】:

由于您已经在使用runat="server",我建议您只使用服务器端控件来管理您的动态内容,如下所示:

<ul>
    <asp:Repeater ID="Repeater1" runat="server" 
                  OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <li>
                <asp:CheckBox id="check1" runat="server" />
            </li>
        </ItemTemplate>
    </asp:Repeater>
</ul>

现在,当您绑定中继器 (OnItemDataBound) 时,您可以访问复选框的 .Text 属性,如下所示:

protected void Repeater1_ItemDataBound(Object sender, RepeaterItemEventArgs e) 
{
    // This event is raised for the header, the footer, separators, and items.
    // Execute the following logic for Items and Alternating Items.
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    {
        CheckBox theCheckBox = e.Item.FindControl("check1") as CheckBox;

        // Make sure we found the check box before we try to use it
        if(theCheckBox != null)
        {
            theCheckBox.Text = "Your Text Value Here";
        }
    }
}

注意:使用代码隐藏可让您更轻松地利用 Visual Studio 调试器的强大功能,并使用 IntelliSense 帮助减少拼写错误并在编译时与运行时发现更多问题。

【讨论】:

  • 这看起来很有希望,除了 e.Item.FindControl("check1") as Checkbox;我需要知道每个控件的名称吗?在这一点上,我没有给每个复选框一个 ID,因为它是动态生成的。
  • 是的,我个人使用 javascript/jquery 来处理我的动态复选框列表。您可以使用ControlCollection CC = Repeater.Controls; foreach (RepeaterItem RI in CC) 和另一个foreach 来获取列表中的每个复选框,但识别它们会很麻烦。
  • 我去看看我能不能让它工作,谢谢你们的帮助:)。
【解决方案2】:

你不能这样做,它看起来像 PHP 风格。如果你使用的是 ASP.Net,那么你应该彻底改变你的代码风格

【讨论】:

  • 当然可以使用内联代码。代码隐藏文件有助于分离 html 和服务器代码,但您始终可以混合使用它们以获得一些独特的结果。 See this article 介绍。
  • 你误会了我的意思,我的意思是你不能用这种情况。
【解决方案3】:

[回答已编辑] 尝试如下设置 testValue,使用“&”和“.ToString()”进行字符串连接:

<ul>
   <%
    string testValue = string.Empty;
    for(int index = 0; index < 5; index++)
    {
        testValue = "blah" & index.ToString();
     %>
        <li>
            <input type="checkbox" runat="server" value="<%=testValue %>" />
        </li>
    <%
    }
     %>
</ul>

【讨论】:

  • @Craig 我有“+”,并将它们更改为“&”用于字符串连接。
  • 嗯,如果我尝试这样做,我会收到一个错误:“/”应用程序中的服务器错误。解析器错误描述:解析服务此请求所需的资源时发生错误。请查看以下特定的解析错误详细信息并适当地修改您的源文件。解析器错误消息:服务器标签不能包含 结构。源错误:第 13 行:%> 第 14 行:
  • 第 15 行:" /> 第 16 行:
  • 第 17 行:
  • 你尝试过我的更改,改为“&”吗?如果是这样,错误是什么?
  • 我做了,错误如上,谢谢帮助。我在上面的错误中有一个额外的“+”,但我修复了它,但仍然出现同样的错误:value=""
  • 我很抱歉:输入标签实际上是正确的。问题可能在值设置之前的字符串连接中......现在查看编辑的答案。
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签