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

时间:2015-08-03 10:42:37

标签: c# asp.net ajax

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

Default.aspx页面

    var newresult = {"_header": {
        "nvPairs": [
                    "status",
                    0,
                    "QTime",
                    3,
                    "params",
                    {
                        "nvPairs": [
                            "hl.fragsize",
                            "100000",
                            "hl.simple.pre",
                            "<em>",
                            "hl.fl",
                            "name",
                            "wt",
                            "javabin",
                            "hl.maxAnalyzedChars",
                            "100000",
                            "hl",
                            "true",
                            "version",
                            "2",
                            "hl.highlightMultiTerm",
                            "true",
                            "hl.snippets",
                            "100",
                            "hl.useFastVectorHighlighter",
                            "true",
                            "q",
                            "harsha",
                            "hl.regex.slop",
                            "0.5",
                            "hl.regex.pattern",
                            "[-\\w ,/\n\\\"']*",
                            "hl.simple.post",
                            "</em>"
                        ]
                    }
                ]
            },
            "_results": [
                {
                    "id": "21",
                    "name": [
                        "asd asda fsdfsdfds harsha sadaks asdasd sad fsddsf dsfdsf dsf sdf dsdsf sdfdsf dsfdsf dsf fdsdfsd fdsfdsf dfdsfdsfds  dsfsdf d f ds fdf df df dsfdsf dfdsf d      fdsdf dfs harsha"
                    ],
                    "_version_": 1508203924863058000
                }
            ],
            "_highlightingInfo": {
                "nvPairs": [
                    "21",
                    {
                        "nvPairs": [
                            "name",
                            [
                                "asd asda fsdfsdfds <em>harsha</em> sadaks asdasd sad fsddsf dsfdsf dsf sdf dsdsf sdfdsf dsfdsf dsf fdsdfsd fdsfdsf dfdsfdsfds  dsfsdf d f ds fdf df df dsfdsf dfdsf d      fdsdf dfs <em>harsha</em>"
                            ]
                        ]
                    }
                ]
            },
            "_highlighting": {
                "21": {
                    "name": [
                        "asd asda fsdfsdfds <em>harsha</em> sadaks asdasd sad fsddsf dsfdsf dsf sdf dsdsf sdfdsf dsfdsf dsf fdsdfsd fdsfdsf dfdsfdsfds  dsfsdf d f ds fdf df df dsfdsf dfdsf d      fdsdf dfs <em>harsha</em>"
                    ]
                }
            },
            "elapsedTime": 19,
            "response": {
                "nvPairs": [
                    "responseHeader",
                    {
                        "nvPairs": [
                            "status",
                            0,
                            "QTime",
                            3,
                            "params",
                            {
                                "nvPairs": [
                                    "hl.fragsize",
                                    "100000",
                                    "hl.simple.pre",
                                    "<em>",
                                    "hl.fl",
                                    "name",
                                    "wt",
                                    "javabin",
                                    "hl.maxAnalyzedChars",
                                    "100000",
                                    "hl",
                                    "true",
                                    "version",
                                    "2",
                                    "hl.highlightMultiTerm",
                                    "true",
                                    "hl.snippets",
                                    "100",
                                    "hl.useFastVectorHighlighter",
                                    "true",
                                    "q",
                                    "harsha",
                                    "hl.regex.slop",
                                    "0.5",
                                    "hl.regex.pattern",
                                    "[-\\w ,/\n\\\"']*",
                                    "hl.simple.post",
                                    "</em>"
                                ]
                            }
                        ]
                    },
                    "response",
                    [
                        {
                            "id": "21",
                            "name": [
                                "asd asda fsdfsdfds harsha sadaks asdasd sad fsddsf dsfdsf dsf sdf dsdsf sdfdsf dsfdsf dsf fdsdfsd fdsfdsf dfdsfdsfds  dsfsdf d f ds fdf df df dsfdsf dfdsf d      fdsdf dfs harsha"
                            ],
                            "_version_": 1508203924863058000
                        }
                    ],
                    "highlighting",
                    {
                        "nvPairs": [
                            "21",
                            {
                                "nvPairs": [
                                    "name",
                                    [
                                        "asd asda fsdfsdfds <em>harsha</em> sadaks asdasd sad fsddsf dsfdsf dsf sdf dsdsf sdfdsf dsfdsf dsf fdsdfsd fdsfdsf dfdsfdsfds  dsfsdf d f ds fdf df df dsfdsf dfdsf d      fdsdf dfs <em>harsha</em>"
                                    ]
                                ]
                            }
                        ]
                    }
                ]
            }
        };

var highlight = {};

$.each(newresult._highlighting, function(i, hitem){
    var match = hitem.name[0].match(/<em>(.*?)<\/em>/);
  // var match = hitem.description[0].match(/elegant/g);
    console.log("match "+match);
    highlight[i] = match[1];


});
var res1 = newresult._highlighting;
var results;
var result;
for(var res2 in res1)
{
    var hlight = res1[res2].name;
    results= highlight[res2];

}

$.each(hlight, function(ix, items){
    var word = results;
    result = items.replace(new RegExp(word, 'g'),word);

});

//var res = newresult.response.nvPairs[5].nvPairs[1].nvPairs[1];
//var word ="harsha";
//var result = res[0].replace(new RegExp(word, 'g'), '<em>harsha</em>');






   $("body").append('<div>' + result + '</div>');



});

Ajax JQuery代码:

<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;"/>

c#c​​ode:

<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>

当我单击按钮(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; } )时,此Ajax JQuery可以正常工作,但在此验证检查之后,我希望按钮应该执行其他事件,例如 CommandArgument CommandName < / strong>, OnCLientClick
但是当我的IF条件转到else部分时,不会触发这些事件。
请建议我在代码中更改哪些内容以使其按预期工作。
请注意,我无法更改按钮上定义的事件。我是asp.net的新人。提前致谢。

1 个答案:

答案 0 :(得分:0)

您永远不会为btnBar定义OnClick服务器端事件。目前它只执行客户端代码,永远不会命中服务器端代码。如果你希望它根据你的jQuery调用的结果执行,那么你将做__doPostBack(“btnBar”);

之类的事情。