繁体   English   中英

单击asp:Button时调用Ajax JQuery时,不会执行CommandArgument,CommandName

[英]CommandArgument, CommandName are not executed when calling Ajax JQuery on click of asp:Button

以下是我对文本框的按钮单击验证的代码。 即,如果Textbox的IF值已经保存在数据库中,则显示警报,否则正常的按钮事件应该起作用。 为此,我尝试了以下代码:

Default.aspx页

<asp:Label ID="lblGuidId" runat="server" Text='<%# Bind("RequestID") %>'></asp:Label>
<asp:TextBox ID="txtBarcodeNumber" runat="server" MaxLength="11" Width="230px" Text='<%# Bind("BarcodeNo") %>' Display="None"></asp:TextBox>

<asp:Button ID="btnBar" runat="server" Text="Save" CommandName="Update"
OnClientClick="EnableDdlCompany();saveButtonClick();" 
CommandArgument="Save" return false;"/>

Ajax JQuery代码:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("[id*=btnBar]").bind("click", function () {

                var chk = {};
                chk.requestID = $("[id*=lblGuidId]").text();
                alert(chk.requestID);
                chk.barCode = $("[id*=txtBar]").val();
                alert(chk.barCode);

                $.ajax({
                    type: 'POST',
                    url: "ChatBoxDemo.aspx/SaveUser",
                    data: '{chk: ' + JSON.stringify(chk) + '}',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (data) {

                        alert(JSON.stringify(data));

                        var val = data.d;
                        alert(val);

                        if (val == true) {
                            alert("Barcode No. alredy exist");  
                        }
                        else {
                            //normal function
                        }
                    },
                    error: function (data) {
                        alert(data.responseText);
                    },
                });
                return false;
            });
        });       
    </script>

C#代码:

public static bool IsValidBarcodeTest(Guid? requestID, string barCode)
    {
        bool result = false;

        string strcon = ConfigurationManager.ConnectionStrings["MasterDB"].ConnectionString;
        SqlConnection con = new SqlConnection(strcon);

        SqlCommand cmd = new SqlCommand("Invoice.usp_tbl_Request_Select_CheckDuplicateBarcode_Test", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@RequestID", requestID);
        cmd.Parameters.AddWithValue("@BarcodeNo", barCode);
        cmd.Connection = con;
        con.Open();
        result = Convert.ToBoolean(cmd.ExecuteScalar());
        con.Close();

        return result;
    }

    public class Check
    {
        public Check() { }

        private Guid? requestID;
        private string barCode;

        public Guid? RequestId
        {
            get { return requestID; }
            set { requestID = value; }
        }

        public string BarCode
        {
            get { return barCode; }
            set { barCode = value; }
        }
    }

    [WebMethod]
    [ScriptMethod]
    public static bool SaveUser(Check chk)
    {
        bool a = false;
        a = IsValidBarcodeTest(chk.RequestId, chk.BarCode);
        return a;
    }

当我单击按钮( btnBar )时,此Ajax JQuery起作用,但是在进行此验证检查之后,我希望按钮应执行其他事件,例如CommandArgumentCommandNameOnCLientClick
但是,如果我的IF条件进入其他部分,则不会触发这些事件。
请提出我应该在代码中进行哪些更改,以使其按预期工作。
请注意,我无法更改按钮上定义的事件。 我在asp.net中非常新
提前致谢。

您永远不会为btnBar定义OnClick服务器端事件。 目前,它仅执行客户端代码,而永远不会访问服务器端代码。 如果您希望它基于jQuery调用的结果执行,那么您将需要执行类似__doPostBack(“ btnBar”);的操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM