使用onclientclick属性和asp net后面的代码

时间:2017-02-09 12:55:55

标签: c# asp.net .net switch-statement repeater

我的转发器中有这个链接按钮,应该编辑用户。我只是调试,我发现代码永远不会进入代码(aspx.cs),除非我提交,但不是在选择时。我该如何解决这个问题?

ASPX:

 <asp:Repeater runat="server" OnItemCommand="rptList_OnItemCommand" ID="rptList">
       <ItemTemplate>
                <%# OperatorId.HasValue && OperatorId == Convert.ToInt32(Eval("ID"))? "<tr style='background-color: #94C5E1;'>" : "<tr>" %>
                      <td>
                        <asp:LinkButton CommandName="selectBtn" ToolTip="TRNSLTEdit" ID="btnEdit" CssClass="editOperator" runat="server" CommandArgument='<%# Eval("ID")%>' OnClientClick="return PopupEdit(this)">
                            <asp:Image ImageUrl="Images/Icons/Edit-16x16.png" ID="EditVisitor" runat="server" />
                        </asp:LinkButton>
                        <div id="modalEdit" class="modal">
                            <div class="modal-content">
                                <p style="margin-left: 50px;">
                                    <img src="Images/Icons/Sip.png" alt="" />
                                    <%# Eval("FirstName") %> <%# Eval("SurName") %>
                                </p>
                                <br />
                                <asp:DropDownList CssClass="ddlNodeClass" runat="server" ID="ddlNodeEdit" />
                                <asp:DropDownList CssClass="ddlTranslatorClass" runat="server" ID="ddlTranslatorEdit" />
                                <asp:LinkButton ToolTip="TRNSLTCancel" CommandArgument='<%# Eval("ID") %>' ID="btnNo" runat="server" CssClass="btnCancelClass" OnClientClick="return Cancel(this)">
                                    <asp:Image ImageUrl="Images/Icons/Cancel-22x22.png" ID="SaveContact" runat="server" ClientIDMode="Static" />
                                </asp:LinkButton>
                                <asp:LinkButton ToolTip="TRNSLTSave" CommandName="submitBtn" CommandArgument='<%# Eval("ID") %>' ID="btnYes" runat="server" CssClass="btnSaveClass">
                                    <asp:Image ImageUrl="Images/Icons/Check-22x22.png" ID="SaveOperator" runat="server" ClientIDMode="Static" />
                                </asp:LinkButton>
                            </div>
                        </div>
                    </td>
       </ItemTemplate>

C#:

    /// <summary>
    /// Assigning commands to listing repeater.
    /// </summary>
    protected void rptList_OnItemCommand(object source, RepeaterCommandEventArgs e)
    {
        var tellusUserId = TellusUser.UserID;
        var operatorId = Convert.ToInt64(e.CommandArgument);

        var ddlNodeEdit = (DropDownList)e.Item.FindControl("ddlNodeEdit");
        var ddlTranslatorEdit = (DropDownList)e.Item.FindControl("ddlTranslatorEdit");

    switch (e.CommandName)
    {
        case "deleteBtn":
            OperatorId = operatorId;
            break;

        case "noBtn":
            break;

        case "yesBtn":
            _administrationSystem.DeleteOperator(tellusUserId, operatorId);
            Response.Redirect("Attendants.aspx");
            break;

        case "selectBtn": //The code never enters here
            var profile = _administrationSystem.GetOperatorForEdit(tellusUserId, operatorId);

            if (profile != null)
            {
                ViewState["Profile"] = profile;
            }
            break;

        case "submitBtn": //The code enters here though
            _administrationSystem.UpdateOperator(tellusUserId, operatorId, TellusUser.UserID, ddlNodeEdit.SelectedIndex + 1, Convert.ToInt64(ddlTranslatorEdit.SelectedValue), "");
            ViewState["ID"] = operatorId;
            break;
    }
}

JS:

function PopupEdit($this) {
    if ($($this).attr("disabled") === "disabled") {
        return false;
    }
    var module = $($this).parent().find("#modalEdit");
    module.show();
    window.onclick = function (event) {
        if (event.target === module) {
            module.hide();
        }
    };

    return false;
}

1 个答案:

答案 0 :(得分:2)

如果我更正OnClientClick="return PopupEdit(this)"

<asp:LinkButton CommandName="selectBtn" ToolTip="TRNSLTEdit" ID="btnEdit" CssClass="editOperator" runat="server" CommandArgument='<%# Eval("ID")%>' OnClientClick="return PopupEdit(this)">
 <asp:Image ImageUrl="Images/Icons/Edit-16x16.png" ID="EditVisitor" runat="server"/>
</asp:LinkButton>

返回false
function PopupEdit($this) {
    if ($($this).attr("disabled") === "disabled") {
        return false;
    }
    var module = $($this).parent().find("#modalEdit");
    module.show();
    window.onclick = function (event) {
        if (event.target === module) {
            module.hide();
        }
    };

    return false;
}

返回false会停止回发。

希望这会有所帮助