【问题标题】:asp.net checkbox converted to three-state checkbox, not hitting original event handlerasp.net 复选框转换为三态复选框,未命中原始事件处理程序
【发布时间】:2010-07-19 17:39:52
【问题描述】:

我有一个带有 asp.net 复选框的用户控件。我正在尝试编写一个 jquery 脚本来将该普通复选框转换为三态复选框。状态为“已检查”、“未检查”和“中级”。单击中间状态应将状态更改为已选中,单击未选中状态应将状态更改为已选中,单击已选中状态应将状态更改为未选中。

我的想法是能够在页面中插入一个普通的asp.net复选框,然后调用jquery函数使其成为三态。

这是我的 jquery 函数,虽然很粗糙但可以工作:

function SetTriStateCheckBox(checkboxfinder, initialval, originalCkbxUniqueID) {
    var i = 0;
    var chks = $(checkboxfinder);
    if (initialval){ chks.val(initialval); }
    chks.hide().each(function() {
        var img = $('<img class="tristate_image" src="../Images/tristateimages/' + $(this).val() + '.gif" />');
        $(this).attr("checked", ($(this).val()=="unchecked"?"false":"true"));
        $(this).attr("name", "input" + i + "image" + i);
        img.attr("name", "" + i + "image" + i);
        i++;
        $(this).before(img);
    });


    $(".tristate_image").click(function() {
        t = $("[name='input" + $(this).attr("name") + "']");
        var tval = t.val();
        $(this).attr("src", "../Images/tristateimages/" + (tval=="checked" ? "unchecked" : "checked") + ".gif");
        switch (tval) {
            case "unchecked":
                t.val("checked").attr("checked", "true");
                break;
            case "intermediate":
                t.val("checked").attr("checked", "true");
                break;
            case "checked":
                t.val("unchecked").removeAttr("checked");
        }
        setTimeout('__doPostBack(\'' + originalCkbxUniqueID + '\',\'\')', 0);
    });   
}

这是我的 OnPreRender 事件处理程序的简化版本,它注册了一个启动脚本:

protected override void OnPreRender(EventArgs e)
{
    string tristatescript = " SetTriStateCheckBox('input[id$=\"ckbx1\"]', 'intermediate','"
        + ckbx1.UniqueID + "'); ";
    ScriptManager.RegisterStartupScript(this, this.GetType(), "ckbx1_tristate", tristatescript, true);
    base.OnPreRender(e);
}

一切似乎都正常工作,除了我在点击我新插入的图像后回发时没有点击原始复选框的事件处理程序。它确实击中了Page_Load

如果我取出jquery的.hide()部分,点击原来的复选框,Request.Params['__EVENTTARGET']和点击新插入的图片时一样。

我不知道从这里去哪里。有什么建议吗?

【问题讨论】:

  • 一天过去了。有人有什么建议吗?

标签: asp.net jquery event-handling


【解决方案1】:

我决定采取不同的方式。事实证明,使用带有图像的链接按钮来模拟三态复选框要容易得多。它有自己的回发事件,您所要做的就是更改 imageurl。

:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 2021-12-19
    • 1970-01-01
    • 2011-05-16
    • 2019-01-29
    • 1970-01-01
    相关资源
    最近更新 更多