结合ASP:Repeater和JQuery

时间:2012-05-29 11:15:57

标签: jquery asp.net

我有下一个C#代码(可行):

    // Write data into the checkboxes
    RepeaterVocabularyWords.DataSource = new[] { correctWord1, correctWord2, incorrectWord1, incorrectWord2 };
    RepeaterVocabularyWords.DataBind();  

    // Get data from the checkboxes
    protected void ButtonAccept_Click(object sender, EventArgs e)
    {
                    foreach (RepeaterItem item in RepeaterVocabularyWords.Items)
        {
            if (item.ItemType == ListItemType.Item
                || item.ItemType == ListItemType.AlternatingItem)
            {
                CheckBox CheckBoxVocabularyWord = (CheckBox)item.FindControl("CheckBoxVocabularyWord");
                if (CheckBoxVocabularyWord.Checked)
                { 
                }
            }
        }

下一个使用JQuery代码的ASP(可行):

$(document).ready(function () {
    $("input[id$='ButtonAccept']").click(function (e) {
        if ($('span.storeCheck input:checked').length != 2) {
            alert("You have to choose only the 2 words that means the same!");
            e.preventDefault();
}

然后,如果我写行“span class =”storeCheck“..,”上面的转发器代码有效,但不是上面的c#代码:

<asp:Repeater ID="RepeaterVocabularyWords" runat="server" OnItemCommand="Repeater1_ItemCommand">
    <ItemTemplate>            
        <<span class="storeCheck"><input runat="server" type="checkbox" ID="CheckBoxVocabularyWord" title="<%# Container.DataItem %>" /></span>  
    </ItemTemplate>
</asp:Repeater>

相比之下,如果我写“asp:CheckBox ID =”..,“上面的c#代码可以工作,但不是jQuery的东西。

<asp:Repeater ID="RepeaterVocabularyWords" runat="server" OnItemCommand="Repeater1_ItemCommand">
    <ItemTemplate>  
        <asp:CheckBox ID="CheckBoxVocabularyWord" runat="server" Text="<%# Container.DataItem %>"   />    
    </ItemTemplate>
</asp:Repeater>

我怎么能同时工作?

3 个答案:

答案 0 :(得分:0)

当您使用<input type='checkbox' runat='server'>时,您会获得HtmlInputCheckBox,而不是CheckBox。更改您的c#代码以使用正确的类,看看是否有帮助。

答案 1 :(得分:0)

试试这个:

$(document).ready(function () {
    $("input[id$='ButtonAccept']").click(function (e) {
        if ($('span.storeCheck').find('input:checked').length != 2) {
            alert("You have to choose only the 2 words that means the same!");
            e.preventDefault();
}

保持asp复选框。

答案 2 :(得分:0)

雷的方法我觉得可行,但这是我最后做的。

Jquery代码如下:

<script type="text/javascript">
    $(document).ready(function () {
        $("input[id$='ButtonAccept']").click(function (e) {
            if ($('span.storeCheck input:checked').length != 2) {
                alert("You have to choose only the 2 words that means the same!");
                e.preventDefault();
            }
        });
    });
</script>

转发器是:

<asp:Repeater ID="RepeaterVocabularyWords" runat="server" OnItemCommand="RepeaterVocabularyWords_ItemCommand">
    <ItemTemplate>
        <span class="storeCheck">
            <asp:CheckBox ID="CheckBoxVocabularyWord" Font-Size="0px" runat="server" Text="<%# Container.DataItem %>"
                ClientIDMode="Static" CssClass="{labelOn: '<%# Container.DataItem %>', labelOff: '<%# Container.DataItem %>', easing: 'easeOutBounce', duration: 500}" />
        </span>
    </ItemTemplate>
</asp:Repeater>
我正在写一个游戏。如果我最终将它上传到某处,我会把链接放在这里:)